ObjectDB ObjectDB

javax.persistence.LockModeType - JPA enum

javax.persistence
Enum LockModeType

java.lang.Object
    java.lang.Enum<javax.persistence.LockModeType>
        javax.persistence.LockModeType
Implemented Interfaces:
Serializable, Comparable<LockModeType>
Lock modes can be specified by means of passing a LockModeType argument to one of the javax.persistence.EntityManager methods that take locks ( lock, find, or refresh) or to the Query.setLockMode() or TypedQuery.setLockMode() method.

Lock modes can be used to specify either optimistic or pessimistic locks.

Optimistic locks are specified using LockModeType.OPTIMISTIC and LockModeType.OPTIMISTIC_FORCE_INCREMENT. The lock mode type values LockModeType.READ and LockModeType.WRITE are synonyms of OPTIMISTIC and OPTIMISTIC_FORCE_INCREMENT respectively. The latter are to be preferred for new applications.

The semantics of requesting locks of type LockModeType.OPTIMISTIC and LockModeType.OPTIMISTIC_FORCE_INCREMENT are the following.

If transaction T1 calls for a lock of type LockModeType.OPTIMISTIC on a versioned object, the entity manager must ensure that neither of the following phenomena can occur:

  • P1 (Dirty read): Transaction T1 modifies a row. Another transaction T2 then reads that row and obtains the modified value, before T1 has committed or rolled back. Transaction T2 eventually commits successfully; it does not matter whether T1 commits or rolls back and whether it does so before or after T2 commits.
  • P2 (Non-repeatable read): Transaction T1 reads a row. Another transaction T2 then modifies or deletes that row, before T1 has committed. Both transactions eventually commit successfully.

Lock modes must always prevent the phenomena P1 and P2.

In addition, calling a lock of type LockModeType.OPTIMISTIC_FORCE_INCREMENT on a versioned object, will also force an update (increment) to the entity's version column.

The persistence implementation is not required to support the use of optimistic lock modes on non-versioned objects. When it cannot support a such lock call, it must throw the PersistenceException.

The lock modes LockModeType.PESSIMISTIC_READ, LockModeType.PESSIMISTIC_WRITE, and LockModeType.PESSIMISTIC_FORCE_INCREMENT are used to immediately obtain long-term database locks.

The semantics of requesting locks of type LockModeType.PESSIMISTIC_READ, LockModeType.PESSIMISTIC_WRITE, and LockModeType.PESSIMISTIC_FORCE_INCREMENT are the following.

If transaction T1 calls for a lock of type LockModeType.PESSIMISTIC_READ or LockModeType.PESSIMISTIC_WRITE on an object, the entity manager must ensure that neither of the following phenomena can occur:

  • P1 (Dirty read): Transaction T1 modifies a row. Another transaction T2 then reads that row and obtains the modified value, before T1 has committed or rolled back.
  • P2 (Non-repeatable read): Transaction T1 reads a row. Another transaction T2 then modifies or deletes that row, before T1 has committed or rolled back.

A lock with LockModeType.PESSIMISTIC_WRITE can be obtained on an entity instance to force serialization among transactions attempting to update the entity data. A lock with LockModeType.PESSIMISTIC_READ can be used to query data using repeatable-read semantics without the need to reread the data at the end of the transaction to obtain a lock, and without blocking other transactions reading the data. A lock with LockModeType.PESSIMISTIC_WRITE can be used when querying data and there is a high likelihood of deadlock or update failure among concurrent updating transactions.

The persistence implementation must support use of locks of type LockModeType.PESSIMISTIC_READ LockModeType.PESSIMISTIC_WRITE on a non-versioned entity as well as on a versioned entity.

When the lock cannot be obtained, and the database locking failure results in transaction-level rollback, the provider must throw the PessimisticLockException and ensure that the JTA transaction or EntityTransaction has been marked for rollback.

When the lock cannot be obtained, and the database locking failure results in only statement-level rollback, the provider must throw the LockTimeoutException (and must not mark the transaction for rollback).

Since:
JPA 1.0
No lock.
No lock.
Since:
JPA 2.0
Optimistic lock.
Optimistic lock.
Since:
JPA 2.0
Optimistic lock, with version update.
Optimistic lock, with version update.
Since:
JPA 2.0
Pessimistic write lock, with version update.
Pessimistic write lock, with version update.
Since:
JPA 2.0
Pessimistic read lock.
Pessimistic read lock.
Since:
JPA 2.0
Pessimistic write lock.
Pessimistic write lock.
Since:
JPA 2.0
Synonymous with OPTIMISTIC.
Synonymous with OPTIMISTIC. OPTIMISTIC is to be preferred for new applications.
Since:
JPA 1.0
Synonymous with OPTIMISTIC_FORCE_INCREMENT.
Synonymous with OPTIMISTIC_FORCE_INCREMENT. OPTIMISTIC_FORCE_IMCREMENT is to be preferred for new applications.
Since:
JPA 1.0
LockModeType valueOf(String name)

Returns the enum constant of this type with the specified name.

Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type.

Parameters:
name - the name of the enum constant to be returned.
Returns:
the enum constant with the specified name
Throws:
java.lang.IllegalArgumentException - if this enum type has no constant with the specified name
java.lang.NullPointerException - if the argument is null

Returns an array containing the constants of this enum type.

Returns an array containing the constants of this enum type.

This method may be used to iterate over the constants as follows:

for (LockModeType c : LockModeType.values())
    System.out.println(c)
Returns:
an array containing the constants of this enum type, in the order they are declared