I'm having trouble understanding how I should avoid lost updates.
I have two applications:
App 1 is a tomcat stateless web service app, using the session-per-request pattern.
App 2 is a Grizzly socket-server based application that users stay connected for longer sessions, using the session-per-conversation pattern.
Both are using JPA/Hibernate for database access, and the shared database is MySQL community 5.1.40 with InnoDB tables, and REPEATABLE_READ isolation level.
Suppose a user has a session open on App Two, and posts to a web-service in App One which modifies their customer record. When App Two modifies its instance of the customer record (usually not the same properties as App One), and EntityManager.merge() is called and the transaction is committed, the updates made in App One are lost. I can see them if I do EntityManager.refresh() before the merge(), but that undoes the changes the user made in App Two and I have to re-apply the changes.
What I want is basically Optimistic Concurrency Control, but without modifying my schema to add a version property. I see that if I was using the Hibernate Annotations I could achieve this by setting:
Code:
@org.hibernate.annotations.Entity(optimisticLock = org.hibernate.annotations.OptimisticLockType.ALL)
But we are using JPA annotations everywhere else.
I also read that optimistic locking is not possible with your DB using REPEATABLE_READ? Is this true? Should I lower it to READ_COMMITTED ?
Any help is greatly appreciated!
Best,
Ryan