- java.lang.Object
-
- java.lang.Enum<LockModeType>
-
- jakarta.persistence.LockModeType
-
- All Implemented Interfaces:
FindOption,RefreshOption,java.io.Serializable,java.lang.Comparable<LockModeType>
public enum LockModeType extends java.lang.Enum<LockModeType> implements FindOption, RefreshOption
Enumerates the kinds of optimistic or pessimistic lock which may be obtained on an entity instance.A specific lock mode may be requested by passing an explicit
LockModeTypeas an argument to:- one of the methods of
EntityManagerwhich obtains locks (lock(),find(), orrefresh()), or - to
Query.setLockMode(LockModeType)orTypedQuery.setLockMode(LockModeType).
Optimistic locks are specified using
LockModeType.OPTIMISTICandOPTIMISTIC_FORCE_INCREMENT. The lock mode typesREADandWRITEare synonyms forOPTIMISTICandOPTIMISTIC_FORCE_INCREMENTrespectively. The latter are preferred for new applications.The semantics of requesting locks of type
LockModeType.OPTIMISTICandLockModeType.OPTIMISTIC_FORCE_INCREMENTare the following.If transaction T1 calls for a lock of type
LockModeType.OPTIMISTICon 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, obtaining a lock of type
LockModeType.OPTIMISTIC_FORCE_INCREMENTon 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
PESSIMISTIC_READ,PESSIMISTIC_WRITE, andPESSIMISTIC_FORCE_INCREMENTare used to immediately obtain long-term database locks.The semantics of requesting locks of type
LockModeType.PESSIMISTIC_READ,LockModeType.PESSIMISTIC_WRITE, andLockModeType.PESSIMISTIC_FORCE_INCREMENTare the following.If transaction T1 calls for a lock of type
LockModeType.PESSIMISTIC_READorLockModeType.PESSIMISTIC_WRITEon 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_WRITEcan be obtained on an entity instance to force serialization among transactions attempting to update the entity data. A lock withLockModeType.PESSIMISTIC_READcan 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 withLockModeType.PESSIMISTIC_WRITEcan 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_READandLockModeType.PESSIMISTIC_WRITEwith 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
PessimisticLockExceptionand ensure that the JTA transaction orEntityTransactionhas 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:
- 1.0
-
-
Enum Constant Summary
Enum Constants Enum Constant Description NONENo lock.OPTIMISTICOptimistic lock.OPTIMISTIC_FORCE_INCREMENTOptimistic lock, with version update.PESSIMISTIC_FORCE_INCREMENTPessimistic write lock, with version update.PESSIMISTIC_READPessimistic read lock.PESSIMISTIC_WRITEPessimistic write lock.READSynonymous withOPTIMISTIC.WRITESynonymous withOPTIMISTIC_FORCE_INCREMENT.
-
Constructor Summary
Constructors Modifier Constructor Description privateLockModeType()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static LockModeTypevalueOf(java.lang.String name)Returns the enum constant of this type with the specified name.static LockModeType[]values()Returns an array containing the constants of this enum type, in the order they are declared.
-
-
-
Enum Constant Detail
-
READ
public static final LockModeType READ
Synonymous withOPTIMISTIC.OPTIMISTICis preferred for new applications.
-
WRITE
public static final LockModeType WRITE
Synonymous withOPTIMISTIC_FORCE_INCREMENT.OPTIMISTIC_FORCE_INCREMENTis preferred for new applications.
-
OPTIMISTIC
public static final LockModeType OPTIMISTIC
Optimistic lock.- Since:
- 2.0
-
OPTIMISTIC_FORCE_INCREMENT
public static final LockModeType OPTIMISTIC_FORCE_INCREMENT
Optimistic lock, with version update.- Since:
- 2.0
-
PESSIMISTIC_READ
public static final LockModeType PESSIMISTIC_READ
Pessimistic read lock.- Since:
- 2.0
-
PESSIMISTIC_WRITE
public static final LockModeType PESSIMISTIC_WRITE
Pessimistic write lock.- Since:
- 2.0
-
PESSIMISTIC_FORCE_INCREMENT
public static final LockModeType PESSIMISTIC_FORCE_INCREMENT
Pessimistic write lock, with version update.- Since:
- 2.0
-
NONE
public static final LockModeType NONE
No lock.- Since:
- 2.0
-
-
Method Detail
-
values
public static LockModeType[] values()
Returns an array containing the constants of this enum type, in the order they are declared. 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
-
valueOf
public static LockModeType valueOf(java.lang.String 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. (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:
java.lang.IllegalArgumentException- if this enum type has no constant with the specified namejava.lang.NullPointerException- if the argument is null
-
-