Regarding Optimistic locking in spring data graph

267 views
Skip to first unread message

gokulakanna balakrishnan

unread,
Jul 27, 2012, 7:46:04 AM7/27/12
to ne...@googlegroups.com
Hi All,
 
     How to implement optimistic locking mechanisam in spring data graph?. I have to handle this suitation in my project.
 
     Please suggest me handle this scenario.
 
Thanks & Regards,
Gokul
 
    

Michael Hunger

unread,
Jul 27, 2012, 7:53:47 AM7/27/12
to ne...@googlegroups.com
It already does optimistic locking by checking properties if they have been change by someone else in between (for reattach of advanced mapping entities at least).



Otherwise you can still use locking like this:


@Override
public void doSomething(....) {
Node lockNode = ... (use-case specific granularity, e.g. template.getReferenceNode()
try {
lock(lockNode);
... business - code
} finally {
unlock(lockNode);
}
}

private void lock(Node node) {
LockManager lockManager = graphDatabaseAPI.getLockManager();
lockManager.getWriteLock(node);
}

private void unlock(Node node) {
try {
LockManager lockManager = graphDatabaseAPI.getLockManager();
lockManager.releaseWriteLock(node, graphDatabaseAPI.getTxManager().getTransaction());
} catch (SystemException e) {
throw new RuntimeException(e);

gokulakanna balakrishnan

unread,
Jul 27, 2012, 8:15:30 AM7/27/12
to ne...@googlegroups.com
Thanks alot Michael..

gokulakanna balakrishnan

unread,
Jul 27, 2012, 8:26:25 AM7/27/12
to ne...@googlegroups.com
Hi Michal,

How to verify object is already modified by someone. If modified
by some one I have to through custome message "Already updated by some
other user".

Is it way to check like version tag in hibernate?

Thanks & Regards,
Gokul

Michael Hunger

unread,
Jul 28, 2012, 5:15:54 PM7/28/12
to ne...@googlegroups.com
Right now there is no support for version properties, feel free to raise a JIRA issue about it.

Michael

gokulakanna balakrishnan

unread,
Jul 30, 2012, 3:18:02 AM7/30/12
to ne...@googlegroups.com
Sure Michael,

Can I have JIRA url.

Thanks & Regards,
Gokul

Denis Moret

unread,
Feb 24, 2015, 8:58:06 AM2/24/15
to ne...@googlegroups.com, michael...@neotechnology.com, stephan...@nagra.com, Denis Moret
Hi Micheal,

Using 

Spring data neo4j 3.3.0.M1
Spring 4.1.4
Neo4j 2.16
Java 1.7

I've tried to use the advance mapping to have the optimistic locking working without success.

My source is something like:

public void updateMainChannel(String channelId, String shortName) {
 
Transaction tx = graphDatabase.beginTx();
 
try {
   
Channel c = channelService.findChannelById(channelId);
   
//c = c.persist(); // this line makes no difference
   c
.setShortName(shortName);
   scheduleService
.saveChannel(c);
   tx
.success();
 
} catch (Exception ex) {
   tx
.failure();
 
} finally {
   tx
.close();
 
}
}
 
 
@Transactional
public class ChannelServiceImpl implements ChannelService{
 
 
@Autowired
 
private ChannelRepository channelRepository;
 
 
public Channel findChannelById(String id){
 
return channelRepository.findById(id);
 
}
 
 
public Channel saveChannel(Channel channel){
 
return channelRepository.save(channel);
 
}
}




@Repository
public interface ChannelRepository extends GraphRepository<Channel> {
 
public Channel findById(String id);
}

What I tried is:

- client A calls the method updateChannel and get paused (debug mode and breakpoint) right after fetching the channel using the channelService
- client B calls the method updateChannel without being stopped to update the same channel as client A
- client A continues

The result is right after client B has updated the channel, client B's value are saved in the database. But once client A finishes, its values are saved. 

I was expected an exception telling that the entity was modified since it was last fetched. 

Did I do something wrong or am I expecting too much ?

I noticed that the Channel entity returned by the "findChannelById" method has a "DetachedEntityState", does it mean that the entity is detached ? I ask this because event if the name is explicit, this class has a isDetached() method which made me think that a DetachedEntityState can be attached. Finally I tried to "attach" it using persist() on it but it does not have any effect. 

Thanks in advance,

Best Regards,

Denis
Reply all
Reply to author
Forward
0 new messages