I am new to JPA and reading this article about JPA2.0 lock modes, which left me with a question about LockModeType.OPTIMISTIC_FORCE_INCREMENT.
Here is an image with an example from an article: http://i.stack.imgur.com/dFjhZ.jpg
Until now, I understand that a clear optimistic lock on transaction T1 is only necessary when my update of object A depends on the state of another object B. that has just been read.
I also understand that a lock using OPTIMISTIC_FORCE_INCREMENT forces B to update its version attribute, which will throw an OptimisticLockException in all transactions that try to update B and read it before the lock is released (i.e. with the value of the old version).
My question is: What happens if another T2 transaction starts immediately after version B is upgraded, changes to B end before T1 completes?
As I understand it, T1 should get an OptimisticLockException. If so, what is the point of this lock, since it slightly reduced the vulnerable time window T1? That would mean: if I want to be sure that B does not change until T1 ends, I need a pessimistic castle, right?
Thanks in advance for this :)
Sheba source share