Locking - rafalkieras/jpa-kss GitHub Wiki

#Locking

Lock types

All supported lock types are specified in the LockModeType enum.

Pessimistic

  • PESSIMISTIC_READ
  • PESSIMISTIC_WRITE
  • PESSIMISTIC_FORCE_INCREMENT

Optimistic

  • OPTIMISTIC, same as READ
  • OPTIMISTIC_FORCE_INCREMENT, same as WRITE

In order to use optimistic locking the entity needs to have @Version attribute. Only a single Version property or field should be used per class; applications that use more than one Version property or field will not be portable.

Acquiring a lock

Lock modes can be specified by means of passing a LockModeType argument to one of the EntityManager methods that take locks (lock, find, or refresh) or to the Query.setLockMode() or TypedQuery.setLockMode() method.