Wednesday, November 14, 2018

Hibernate locking

Hibernate supports all JPA locking modes and some additional specific locking options. As with JPA, explicit locking can be configured for the following operations:
The LockModeConverter takes care of mapping JPA and Hibernate lock modes as follows:
Hibernate LockModeJPA LockModeType
NONENONE
OPTIMISTIC
READ
OPTIMISTIC
OPTIMISTIC_FORCE_INCREMENT
WRITE
OPTIMISTIC_FORCE_INCREMENT
PESSIMISTIC_READPESSIMISTIC_READ
PESSIMISTIC_WRITE
UPGRADE
UPGRADE_NOWAIT
UPGRADE_SKIPLOCKED
PESSIMISTIC_WRITE
PESSIMISTIC_FORCE_INCREMENT
FORCE
PESSIMISTIC_FORCE_INCREMENT
The UPGRADE and FORCE lock modes are deprecated in favor ofPESSIMISTIC_WRITE.
UPGRADE_NOWAIT and UPGRADE_SKIPLOCKED use an Oracle-style select for update nowait or select for update skip locked syntax respectively.

Lock scope and timeouts

Hibernate also defines scope and timeout locking options:
  • scope
    The lock scope allows explicit locking cascade to owned associations.
  • timeout
    A timeout interval may prevent a locking request from waiting indefinitely.