A specific lock mode may be requested by passing an explicit
LockModeType as an argument to:
EntityManager which obtains
locks (lock(),
find(), or
refresh()), or
Query#setLockMode(LockModeType) or
TypedQuery#setLockMode(LockModeType).
Optimistic locks are specified using
LockModeType. and
LockModeType#OPTIMISTIC_FORCE_INCREMENT. The lock mode
types LockModeType#READ and LockModeType#WRITE are
synonyms for OPTIMISTIC and OPTIMISTIC_FORCE_INCREMENT
respectively. The latter are 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:
Lock modes must always prevent the phenomena P1 and P2.
In addition, obtaining 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 such a lock request, 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:
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 the use of locks
of type LockModeType.PESSIMISTIC_READ and
LockModeType.PESSIMISTIC_WRITE with non-versioned entities
as well as with versioned entities.
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).
| Modifier and Type | Field and Description |
|---|---|
| public static final LockModeType | NONE
No lock. |
| public static final LockModeType | OPTIMISTIC
Optimistic lock. |
| public static final LockModeType | OPTIMISTIC_FORCE_INCREMENT
Optimistic lock, with version update. |
| public static final LockModeType | PESSIMISTIC_FORCE_INCREMENT
Pessimistic write lock, with version update. |
| public static final LockModeType | PESSIMISTIC_READ
Pessimistic read lock. |
| public static final LockModeType | PESSIMISTIC_WRITE
Pessimistic write lock. |
| public static final LockModeType | READ
Synonymous with |
| public static final LockModeType | WRITE
Synonymous with |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static LockModeType | |
| public static LockModeType[] |
| NONE | back to summary |
|---|---|
| public static final LockModeType NONE No lock.
| |
| OPTIMISTIC | back to summary |
|---|---|
| public static final LockModeType OPTIMISTIC Optimistic lock.
| |
| OPTIMISTIC_FORCE_INCREMENT | back to summary |
|---|---|
| public static final LockModeType OPTIMISTIC_FORCE_INCREMENT Optimistic lock, with version update.
| |
| PESSIMISTIC_FORCE_INCREMENT | back to summary |
|---|---|
| public static final LockModeType PESSIMISTIC_FORCE_INCREMENT Pessimistic write lock, with version update.
| |
| PESSIMISTIC_READ | back to summary |
|---|---|
| public static final LockModeType PESSIMISTIC_READ Pessimistic read lock.
| |
| PESSIMISTIC_WRITE | back to summary |
|---|---|
| public static final LockModeType PESSIMISTIC_WRITE Pessimistic write lock.
| |
| READ | back to summary |
|---|---|
| public static final LockModeType READ Synonymous with
| |
| WRITE | back to summary |
|---|---|
| public static final LockModeType WRITE Synonymous with
| |
| LockModeType | back to summary |
|---|---|
| private LockModeType() | |
| valueOf | back to summary |
|---|---|
| public static LockModeType valueOf(String name) | |
| values | back to summary |
|---|---|
| public static LockModeType[] values() | |