Thursday, January 26, 2017

PESSIMISTIC_READ/WRITE with JPA, Hibernate and Oracle

In an enterprise application we have used pessimistic locking in some of the entity retrieval and persistence to avoid dirty reads and writes due to the extreme concurrent access. As we know, JPA provide both PESSIMISTIC_READ and PESSIMISTIC_WRITE capabilities which use shared lock and exclusive lock respectively.

If you make the sql query enabled with persistence settings, you can see the underlying queries of both lock types are going to be the same which make use of oracle's FOR UPDATE clause with the respective select queries. When browse through the articles on this, I found that it is allowed for the JPA provider to provide the PESSIMISTIC_WRITE, even the PESSIMISTIC_READ mode explicitly set in the queries. But it is not permitted the other way around.

Below article, provides a good insight into the pessimistic locking with different database providers.

https://vladmihalcea.com/2015/02/24/hibernate-locking-patterns-how-do-pessimistic_read-and-pessimistic_write-work/


1 comment: