2.10.1.8.1. Locking Hints

To avoid deadlock and optimistic update exceptions among multiple updaters, use a pessimistic LockManager, specified in the persistence unit definition, and use a hint name of openjpa.FetchPlan.ReadLockMode on queries for entities that must be locked for serialization. The value of ReadLockMode can be either READ or WRITE. This results in a database-specific locking keyword (usually FOR UPDATE) to be emitted into the SQL.

Using a ReadLockMode hint with JPA optimistic locking (i.e. specifying LockManager = version) will result in the entity version field either being reread at end of transaction in the case of a value of READ or the version field updated at end of transaction in the case of WRITE. You must define a version field in the entity mapping when using a version LockManager and using ReadLockMode.

Table 2. Interaction of ReadLockMode hint and LockManager
ReadLockMode LockManager=pessimistic LockManager=version

READ

SQL with FOR UPDATE

SQL without FOR UPDATE;

reread version field at the end of transaction and check for no change.

WRITE

SQL with FOR UPDATE

SQL without FOR UPDATE;

force update version field at the end of transaction

not specified

SQL without FOR UPDATE

SQL without FOR UPDATE