Hibernate Books

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Optimistic lock and Merge Data in Concurrent Write Access
PostPosted: Thu May 19, 2011 5:45 am 
Newbie

Joined: Thu May 19, 2011 5:27 am
Posts: 4
Hi All

I have a question about concurrent write access to the database with optimistic lock and merging data:
I have the following code part:
Code:
1. ISession session = NHibernateHelper.GetCurrentSession();
2. ITransaction transaction = session.BeginTransaction();
3. Person personpersistent = (Person)session.Merge(person);
4. session.Update(personpersistent);
5. transaction.Commit();

When I use OptimisticLock and a Version field in the person-class and I do a concurrent update with two applications using the same code as above, I get the exception "Row was updated or deleted by another transaction" in line 3 of the code as soon as the 2nd application runs through the code to make the update.
However, this should happen as expected, so that changes of one application are not overwritten by another.

This works well independently of the OptimisticLock option. I tested it with OptimisticLock = Version, OptimisticLock = Dirty and OptimisticLock = All.

But if I do not use a Version field in the person-class, the optimistic lock does not recognize the concurrent change of the other application. I tried it with OptimisticLock = Dirty and OptimisticLock = All (the Version-option is obsolete in this case).
I get the exception only if both applications run through the code the same time and only in line 5 when the second application tries to commit the transaction.

Why should I use a version field, so that OptimisticLock does work correctly? As said in the manual, the Dirty and All options would compare each field before they are changed.
Is it a problem with the merge statement?
I would very appreciate if you can give me a hint.

Thank you
Thomas


Top
 Profile  
 
 Post subject: Re: Optimistic lock and Merge Data in Concurrent Write Access
PostPosted: Wed Dec 14, 2011 8:28 pm 
Newbie

Joined: Fri Dec 09, 2011 12:35 am
Posts: 2
It is obvious that I really want to be part of your site. I really love how you make people to have an interest in your site which helps me a lot. Thank you so much for this.

_________________
door lock
electric strikes


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.