Top Description Fields Constructors Methods
org.hibernate

public final Enum LockMode

extends Enum<LockMode>
Class Inheritance
Imports
org.hibernate.jpa.internal.util.LockModeTypeHelper, jakarta.persistence.LockModeType

Instances represent a lock mode for a row of a relational database table. It is not intended that users spend much time worrying about locking since Hibernate usually obtains exactly the right lock level automatically. Some "advanced" users may wish to explicitly specify lock levels.

This enumeration of lock modes competes with the JPA-defined LockModeType, but offers additional options, including UPGRADE_NOWAIT and UPGRADE_SKIPLOCKED.

Author
Gavin King
See Also
Session#lock(Object, LockMode), LockModeType, LockOptions, org.hibernate.annotations.OptimisticLocking

Field Summary

Modifier and TypeField and Description
private final String
private final int
public static final LockMode
NONE

No lock required.

public static final LockMode
OPTIMISTIC

A shared optimistic lock.

public static final LockMode
OPTIMISTIC_FORCE_INCREMENT

A kind of exclusive optimistic lock.

public static final LockMode
PESSIMISTIC_FORCE_INCREMENT

A pessimistic write lock which immediately increments the version of the locked object.

public static final LockMode
PESSIMISTIC_READ

A pessimistic shared lock, which prevents concurrent transactions from writing the locked object.

public static final LockMode
PESSIMISTIC_WRITE

A pessimistic upgrade lock, which prevents concurrent transactions from reading or writing the locked object.

public static final LockMode
READ

A shared lock.

public static final LockMode
UPGRADE_NOWAIT

A pessimistic upgrade lock, obtained using an Oracle-style select for update nowait.

public static final LockMode
UPGRADE_SKIPLOCKED

A pessimistic upgrade lock, obtained using an Oracle-style select for update skip locked.

public static final LockMode
WRITE

An exclusive write lock.

Constructor Summary

AccessConstructor and Description
private
LockMode(int level, String externalForm)

Method Summary

Modifier and TypeMethod and Description
public static LockMode
fromExternalForm(String externalForm)

public static LockMode
public boolean

Returns:

true if this lock mode is more restrictive than given lock mode
greaterThan
(LockMode
LockMode to check
mode
)

Check if this lock mode is more restrictive than the given lock mode.

public boolean

Returns:

true if this lock mode is less restrictive than given lock mode
lessThan
(LockMode
LockMode to check
mode
)

Check if this lock mode is less restrictive than the given lock mode.

public String
public static LockModeType
public LockModeType
public LockOptions

Returns:

an instance of LockOptions with this lock mode, and all other settings defaulted.
toLockOptions
()

public static LockMode
public static LockMode[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

externalFormback to summary
private final String externalForm
levelback to summary
private final int level
NONEback to summary
public static final LockMode NONE

No lock required. If an object is requested with this lock mode, a READ lock will be obtained if it turns out to be necessary to actually read the state from the database, rather than pull it from a cache.

This is the "default" lock mode, the mode requested by calling Session#get(Class, Object) without passing an explicit mode. It permits the state of an object to be retrieved from the cache without the cost of database access.

See Also
LockModeType#NONE
OPTIMISTICback to summary
public static final LockMode OPTIMISTIC

A shared optimistic lock. Assumes that the current transaction will not experience contention for the state of an entity. The version will be checked near the end of the transaction, to verify that this was indeed the case.

Note that, despite the similar names this lock mode is not the same as the JPA-defined mode LockModeType#OPTIMISTIC.

OPTIMISTIC_FORCE_INCREMENTback to summary
public static final LockMode OPTIMISTIC_FORCE_INCREMENT

A kind of exclusive optimistic lock. Assumes that the current transaction will not experience contention for the state of an entity. The version will be checked and incremented near the end of the transaction, to verify that this was indeed the case, and to signal to concurrent optimistic readers that their optimistic locks have failed.

See Also
LockModeType#OPTIMISTIC_FORCE_INCREMENT
PESSIMISTIC_FORCE_INCREMENTback to summary
public static final LockMode PESSIMISTIC_FORCE_INCREMENT

A pessimistic write lock which immediately increments the version of the locked object. Obtained by immediate execution of an update statement.

See Also
LockModeType#PESSIMISTIC_FORCE_INCREMENT
PESSIMISTIC_READback to summary
public static final LockMode PESSIMISTIC_READ

A pessimistic shared lock, which prevents concurrent transactions from writing the locked object. Obtained via a select for share statement in dialects where this syntax is supported, and via select for update in other dialects.

On databases which do not support for share, this lock mode is equivalent to PESSIMISTIC_WRITE.

See Also
LockModeType#PESSIMISTIC_READ
PESSIMISTIC_WRITEback to summary
public static final LockMode PESSIMISTIC_WRITE

A pessimistic upgrade lock, which prevents concurrent transactions from reading or writing the locked object. Obtained via a select for update statement.

See Also
LockModeType#PESSIMISTIC_WRITE
READback to summary
public static final LockMode READ

A shared lock. Objects in this lock mode were read from the database in the current transaction, rather than being pulled from a cache.

Note that this lock mode is the same as the JPA-defined modes LockModeType#READ and LockModeType#OPTIMISTIC.

See Also
LockModeType#OPTIMISTIC
UPGRADE_NOWAITback to summary
public static final LockMode UPGRADE_NOWAIT

A pessimistic upgrade lock, obtained using an Oracle-style select for update nowait. The semantics of this lock mode, if the lock is successfully obtained, are the same as PESSIMISTIC_WRITE. If the lock is not immediately available, an exception occurs.

UPGRADE_SKIPLOCKEDback to summary
public static final LockMode UPGRADE_SKIPLOCKED

A pessimistic upgrade lock, obtained using an Oracle-style select for update skip locked. The semantics of this lock mode, if the lock is successfully obtained, are the same as PESSIMISTIC_WRITE. But if the lock is not immediately available, no exception occurs, but the locked row is not returned from the database.

WRITEback to summary
public static final LockMode WRITE

An exclusive write lock. Objects in this lock mode were updated or inserted in the database in the current transaction.

This lock mode is for internal use only and is not a legal argument to Session#get(Class, Object, LockMode), Session#refresh(Object, LockMode), or Session#lock(Object, LockMode). These methods throw an exception if WRITE is given as an argument.

Note that, despite the similar names, this lock mode is not the same as the JPA-defined mode LockModeType#WRITE.

Annotations
@Internal

Constructor Detail

LockModeback to summary
private LockMode(int level, String externalForm)

Method Detail

fromExternalFormback to summary
public static LockMode fromExternalForm(String externalForm)
fromJpaLockModeback to summary
public static LockMode fromJpaLockMode(LockModeType lockMode)
greaterThanback to summary
public boolean greaterThan(LockMode mode)

Check if this lock mode is more restrictive than the given lock mode.

Parameters
mode:LockMode

LockMode to check

Returns:boolean

true if this lock mode is more restrictive than given lock mode

lessThanback to summary
public boolean lessThan(LockMode mode)

Check if this lock mode is less restrictive than the given lock mode.

Parameters
mode:LockMode

LockMode to check

Returns:boolean

true if this lock mode is less restrictive than given lock mode

toExternalFormback to summary
public String toExternalForm()
toJpaLockModeback to summary
public static LockModeType toJpaLockMode(LockMode lockMode)
toJpaLockModeback to summary
public LockModeType toJpaLockMode()
toLockOptionsback to summary
public LockOptions toLockOptions()
Returns:LockOptions

an instance of LockOptions with this lock mode, and all other settings defaulted.

valueOfback to summary
public static LockMode valueOf(String name)
valuesback to summary
public static LockMode[] values()