Difference between LockModeType Jpa
I am confused about the working of LockModeTypes in JPA:
-
LockModeType.Optimistic
- it increments the version while committing.
- Question here is : If I have version column in my entity and if I don't specify this lock mode then also it works similarly then what is the use of it?
-
LockModeType.OPTIMISTIC_FORCE_INCREMENT
- Here it increments the version column even though the entity is not updated.
- but what is the use of it if any other process updated the same row before this transaction is committed? this transaction is anyways going to fail. so what is the use of this
LockModeType
.
-
LockModeType.PESSIMISTIC_READ
- This lock mode issues a
select for update nowait
(if no hint timeout specified).. - so basically this means that no other transaction can update this row until this transaction is committed, then its basically a write lock, why its named a
Read
lock?
- This lock mode issues a
-
LockModeType.PESSIMISTIC_WRITE
- This lock mode also issues a
select for update nowait
(if no hint timeout specified). - Question here is what is the difference between this lock mode and
LockModeType.PESSIMISTIC_READ
as I see both fires same queries?
- This lock mode also issues a
-
LockModeType.PESSIMISTIC_FORCE_INCREMENT
- this does
select for update nowait
(if no hint timeout specified) and also increments the version number. - I totally didn't get the use of it.
- why a version increment is required if
for update no wait
is there?
- this does
Solution 1:
I would first differentiate between optimistic and pessimistic locks, because they are different in their underlying mechanism.
Optimistic locking is fully controlled by JPA and only requires additional version column in DB tables. It is completely independent of underlying DB engine used to store relational data.
On the other hand, pessimistic locking uses locking mechanism provided by underlying database to lock existing records in tables. JPA needs to know how to trigger these locks and some databases do not support them or only partially.
Now to the list of lock types:
-
LockModeType.Optimistic
- If entities specify a version field, this is the default. For entities without a version column, using this type of lock isn't guaranteed to work on any JPA implementation. This mode is usually ignored as stated by ObjectDB. In my opinion it only exists so that you may compute lock mode dynamically and pass it further even if the lock would be OPTIMISTIC in the end. Not very probable usecase though, but it is always good API design to provide an option to reference even the default value.
-
Example:
`LockModeType lockMode = resolveLockMode(); A a = em.find(A.class, 1, lockMode);`
LockModeType.OPTIMISTIC_FORCE_INCREMENT
- This is a rarely used option. But it could be reasonable, if you want to lock referencing this entity by another entity. In other words you want to lock working with an entity even if it is not modified, but other entities may be modified in relation to this entity.
- Example: We have entity Book and Shelf. It is possible to add Book to Shelf, but book does not have any reference to its shelf. It is reasonable to lock the action of moving a book to a shelf, so that a book does not end up in another shelf (due to another transaction) before end of this transaction. To lock this action, it is not sufficient to lock current book shelf entity, as the book does not have to be on a shelf yet. It also does not make sense to lock all target bookshelves, as they would be probably different in different transactions. The only thing that makes sense is to lock the book entity itself, even if in our case it does not get changed (it does not hold reference to its bookshelf).
LockModeType.PESSIMISTIC_READ
- this mode is similar to
LockModeType.PESSIMISTIC_WRITE
, but different in one thing: until write lock is in place on the same entity by some transaction, it should not block reading the entity. It also allows other transactions to lock usingLockModeType.PESSIMISTIC_READ
. The differences between WRITE and READ locks are well explained here (ObjectDB) and here (OpenJPA). If an entity is already locked by another transaction, any attempt to lock it will throw an exception. This behavior can be modified to waiting for some time for the lock to be released before throwing an exception and roll back the transaction. In order to do that, specify thejavax.persistence.lock.timeout
hint with the number of milliseconds to wait before throwing the exception. There are multiple ways to do this on multiple levels, as described in the Java EE tutorial.
LockModeType.PESSIMISTIC_WRITE
- this is a stronger version of
LockModeType.PESSIMISTIC_READ
. WhenWRITE
lock is in place, JPA with the help of the database will prevent any other transaction to read the entity, not only to write as withREAD
lock. - The way how this is implemented in a JPA provider in cooperation with underlying DB is not prescribed. In your case with Oracle, I would say that Oracle does not provide something close to a
READ
lock.SELECT...FOR UPDATE
is really rather aWRITE
lock. It may be a bug in hibernate or just a decision that, instead of implementing custom "softer"READ
lock, the "harder"WRITE
lock is used instead. This mostly does not break consistency, but does not hold all rules withREAD
locks. You could run some simple tests withREAD
locks and long running transactions to find out if more transactions are able to acquireREAD
locks on the same entity. This should be possible, whereas not withWRITE
locks.
- LockModeType.PESSIMISTIC_FORCE_INCREMENT
- this is another rarely used lock mode. However, it is an option where you need to combine
PESSIMISTIC
andOPTIMISTIC
mechanisms. Using plainPESSIMISTIC_WRITE
would fail in following scenario:- transaction A uses optimistic locking and reads entity E
- transaction B acquires WRITE lock on entity E
- transaction B commits and releases lock of E
- transaction A updates E and commits
- in step 4, if version column is not incremented by transaction B, nothing prevents A from overwriting changes of B. Lock mode
LockModeType.PESSIMISTIC_FORCE_INCREMENT
will force transaction B to update version number and causing transaction A to fail withOptimisticLockException
, even though B was using pessimistic locking.
- LockModeType.NONE
- this is the default if entities don't provide a version field. It means that no locking is enabled conflicts will be resolved on best effort basis and will not be detected. This is the only lock mode allowed outside of a transaction