- All Implemented Interfaces:
FindOption
,RefreshOption
,Serializable
,Comparable<LockModeType>
,Constable
A specific lock mode may be requested by passing an explicit
LockModeType
as an argument to:
- one of the methods of
EntityManager
which obtains locks (lock()
,find()
, orrefresh()
), or - to
Query.setLockMode(LockModeType)
orTypedQuery.setLockMode(LockModeType)
.
Lock modes other than NONE
prevent dirty reads and
non-repeatable reads of locked entity data within the current
transaction.
An optimistic lock is requested by specifying the lock mode
type OPTIMISTIC
or OPTIMISTIC_FORCE_INCREMENT
.
The lock mode types READ
and WRITE
are synonyms
for OPTIMISTIC
and OPTIMISTIC_FORCE_INCREMENT
respectively. The latter are preferred for new applications.
The persistence implementation is not required to accept
requests for optimistic locks on non-versioned entities. When
a provider does not support such a lock request, it must throw
PersistenceException
.
An immediate pessimistic database-level lock is requested by
specifying one of the lock mode types PESSIMISTIC_READ
,
PESSIMISTIC_WRITE
, or PESSIMISTIC_FORCE_INCREMENT
.
- A lock with type
PESSIMISTIC_WRITE
can be obtained on an entity instance to force serialization among transactions attempting to update the entity data. - A lock with type
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 type
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 accept requests for locks
of type PESSIMISTIC_READ
and PESSIMISTIC_WRITE
on
both versioned and non-versioned entities.
- When the lock cannot be obtained, and the database locking
failure results in transaction-level rollback, the provider
must throw
PessimisticLockException
and ensure that the JTA transaction orEntityTransaction
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).
The persistence implementation is not required to accept
requests for locks of type PESSIMISTIC_FORCE_INCREMENT
on
non-versioned entities. When a provider does not support such a
lock request, it must throw PersistenceException
.
- Since:
- 1.0
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Enum
Enum.EnumDesc<E extends Enum<E>>
-
Enum Constant Summary
Enum ConstantDescriptionNo lock.An optimistic lock.Optimistic lock, with version update, which prevents the following phenomena:Pessimistic write lock, with version update.Pessimistic read lock.Pessimistic write lock.Synonymous withOPTIMISTIC
.Synonymous withOPTIMISTIC_FORCE_INCREMENT
. -
Method Summary
Modifier and TypeMethodDescriptionstatic LockModeType
Returns the enum constant of this class with the specified name.static LockModeType[]
values()
Returns an array containing the constants of this enum class, in the order they are declared.
-
Enum Constant Details
-
READ
Synonymous withOPTIMISTIC
.OPTIMISTIC
is preferred for new applications. -
WRITE
Synonymous withOPTIMISTIC_FORCE_INCREMENT
.OPTIMISTIC_FORCE_INCREMENT
is preferred for new applications. -
OPTIMISTIC
An optimistic lock.If transaction T1 calls for a lock of type
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.
The persistence implementation is not required to accept requests for
OPTIMISTIC
locks on non-versioned entities.- Since:
- 2.0
-
OPTIMISTIC_FORCE_INCREMENT
Optimistic lock, with version update, which prevents the following phenomena:If transaction T1 calls for a lock of type
OPTIMISTIC_FORCE_INCREMENT
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.
In addition, obtaining a lock of type
OPTIMISTIC_FORCE_INCREMENT
on a versioned entity will also force an update (increment) to the version column.The persistence implementation is not required to accept requests for
OPTIMISTIC_FORCE_INCREMENT
locks on non-versioned entities.- Since:
- 2.0
-
PESSIMISTIC_READ
Pessimistic read lock.If transaction T1 calls for a lock of type
PESSIMISTIC_READ
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.
- Since:
- 2.0
-
PESSIMISTIC_WRITE
Pessimistic write lock.If transaction T1 calls for a lock of type
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.
- Since:
- 2.0
-
PESSIMISTIC_FORCE_INCREMENT
Pessimistic write lock, with version update.If transaction T1 calls for a lock of type
PESSIMISTIC_FORCE_INCREMENT
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.
In addition, obtaining a lock of type
PESSIMISTIC_FORCE_INCREMENT
on a versioned entity will also force an update (increment) to the version column.The persistence implementation is not required to accept requests for
PESSIMISTIC_FORCE_INCREMENT
locks on non-versioned entities.- Since:
- 2.0
-
NONE
No lock.- Since:
- 2.0
-
-
Method Details
-
values
Returns an array containing the constants of this enum class, in the order they are declared.- Returns:
- an array containing the constants of this enum class, in the order they are declared
-
valueOf
Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)- Parameters:
name
- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException
- if this enum class has no constant with the specified nameNullPointerException
- if the argument is null
-