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