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
Le vendredi 27 juillet 2012 13:53:47 UTC+2, Michael Hunger a écrit :
>
> 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);
> }
> }
>
> Am 27.07.2012 um 13:46 schrieb gokulakanna balakrishnan:
>
> > 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
> >
> >
>
>
--
You received this message because you are subscribed to the Google Groups
"Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.