Semantics JPA 2.0 OPTIMISTIC_FORCE_INCREMENT

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 :)

+4
source share
2 answers

In your example, the question is asked why this is called an OPTIMIZATION lock. This is not ideal, but if this is enough for a large number of situations in the real world, and it uses much less resources (including time) than a hard lock.

When using this type of lock, you trade for performance and often improve your usability by betting that your transactions will work most of the time, and you are sure that in those cases you will be notified (an exception will be thrown) and you You can step back and "do the right thing": try again, refuse, ... however you decided to handle the exception.

Pessimistic blocking can be very inappropriate for a high-performance transactional system where you need a certain level of blocking, but the probability of their collision is minimal: how many millions of xTunes users (name changed to protect the innocent ..) "order" (update) at any time and how much of the whole order (updates) from the same account?

+2
source

What happens if another T2 transaction starts immediately after the version from B increases, B changes, and ends before T1 completes?

Regardless of which RDBMS you intend to use, even if it uses MVCC (Multi-Version Concurrency Control) or 2PL (two-phase locking), when changing a table row, an exclusive locking is performed and only when the current transaction is completed (commit or rollback).

Therefore, as soon as you upgrade version B, no other transaction can change this record until you complete the transaction.

Also worth mentioning is the difference between OPTIMISTIC_FORCE_INCREMENT and PESIMISTIC_FORCE_INCREMENT . OPTIMISTIC_FORCE_INCREMENT increases the version at the end of the transaction, and PESIMISTIC_FORCE_INCREMENT does the version immediately.

If there is a serious contradiction in this particular object, then PESIMISTIC_FORCE_INCREMENT much more attractive, since from the moment of acquiring the lock, no other transaction can change this record and your transaction will not be rolled back due to the optimistic version of the mismatch.

0
source

All Articles