Top Description Fields Constructors Methods
org.hibernate

public Class LockOptions

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Imports
java.io.Serializable, java.util.HashMap, .Iterator, .LinkedHashMap, .Map, .Objects, .Set, jakarta.persistence.PessimisticLockScope, org.hibernate.query.Query, org.hibernate.query.spi.QueryOptions

Contains a set of options describing how a row of a database table mapped by an entity should be locked. For Session#lock(Object, LockOptions), Session#get(Class, Object, LockOptions), or Session#refresh(Object, LockOptions), the relevant options are:

Timeout and lock scope are ignored if the specified LockMode represents a flavor of optimistic locking.

In HQL and criteria queries, lock modes can be defined in an even more granular fashion, with the option to specify a lock mode that applies only to a certain query alias.

Finally, the use of follow-on locking may be force enabled or disabled, overriding the default behavior of the SQL dialect by passing a non-null argument to setFollowOnLocking(Boolean).

Author
Scott Marlow

Field Summary

Modifier and TypeField and Description
private Map<String, LockMode>
private Boolean
private final boolean
private LockMode
public static final int
NO_WAIT

Indicates that the database should not wait at all to acquire a pessimistic lock which is not immediately available.

public static final LockOptions
NONE

Represents LockMode#NONE, to which timeout and scope are not applicable.

pack-priv static final LockOptions
pack-priv static final LockOptions
OPTIMISTIC_FORCE_INCREMENT

Represents LockMode#OPTIMISTIC_FORCE_INCREMENT, to which timeout and scope are not applicable.

pack-priv static final LockOptions
pack-priv static final LockOptions
pack-priv static final LockOptions
public static final LockOptions
READ

Represents LockMode#READ, to which timeout and scope are not applicable.

private boolean
public static final int
SKIP_LOCKED

Indicates that rows which are already locked should be skipped.

private int
public static final LockOptions
pack-priv static final LockOptions
pack-priv static final LockOptions
public static final int
WAIT_FOREVER

Indicates that there is no timeout for the lock acquisition, that is, that the database should in principle wait forever to obtain the lock.

Constructor Summary

AccessConstructor and Description
public
LockOptions()

Construct an instance with mode LockMode#NONE and timeout WAIT_FOREVER.

public
LockOptions(LockMode
The initial lock mode
lockMode
)

Construct an instance with the given mode and WAIT_FOREVER.

public
LockOptions(LockMode
The initial lock mode
lockMode
,
int
The initial timeout
timeout
)

Construct an instance with the given mode and timeout.

public
LockOptions(LockMode
The initial lock mode
lockMode
,
int
The initial timeout
timeout
,
PessimisticLockScope
The initial lock scope
scope
)

Construct an instance with the given mode, timeout, and scope.

private
LockOptions(boolean immutable, LockMode lockMode)

Internal operation used to create immutable global instances.

Method Summary

Modifier and TypeMethod and Description
public static LockOptions

Returns:

destination
copy
(LockOptions
Source for the copy (copied from)
source
,
LockOptions
Destination for the copy (copied to)
destination
)

Copy the options in the first given instance of LockOptions to the second given instance.

public boolean
equals(Object
the reference object with which to compare.
object
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public LockMode

Returns:

The greatest of all requested lock modes.
findGreatestLockMode
()

Currently needed for follow-on locking.

public int

Returns:

the number of explicitly defined alias lock modes.
getAliasLockCount
()

The number of aliases that have alias-specific lock modes specified.

public Iterator<Map.Entry<String, LockMode>>

Returns:

an iterator over the Map.Entrys
getAliasLockIterator
()

Deprecated use getAliasSpecificLocks()
Iterator over Map.Entrys, each containing an alias and its LockMode.
public LockMode

Returns:

The explicit lock mode for that alias.
getAliasSpecificLockMode
(String
The alias for which to locate the explicit lock mode.
alias
)

Get the LockMode explicitly specified for the given alias via setAliasSpecificLockMode(String, LockMode).

public Set<Map.Entry<String, LockMode>>

Returns:

an iterable with the Map.Entrys
getAliasSpecificLocks
()

Set of Map.Entrys, each associating an alias with its specified alias-specific LockMode.

public LockMode

Returns:

The effective lock mode.
getEffectiveLockMode
(String
The alias for which to locate the effective lock mode.
alias
)

Determine the LockMode to apply to the given alias.

public Boolean

Returns:

true if follow-on locking was force enabled, false if follow-on locking was force disabled, or null if the default behavior of the dialect has not been overridden.
getFollowOnLocking
()

Returns a value indicating if follow-on locking was force enabled or disabled, overriding the default behavior of the SQL dialect.

public LockMode

Returns:

the overall lock mode
getLockMode
()

Retrieve the overall lock mode in effect for this set of options.

public PessimisticLockScope

Returns:

the current PessimisticLockScope
getLockScope
()

The current lock scope:

public boolean

Returns:

true if the lock extends to owned associations
getScope
()

Deprecated since 6.2. use getLockScope()
The current lock scope setting:
  • true means the lock extends to rows of owned collections, but
  • false means only the entity table and secondary tables are locked.
public int

Returns:

a timeout in milliseconds, NO_WAIT, WAIT_FOREVER, or SKIP_LOCKED
getTimeOut
()

The current timeout, a maximum amount of time in milliseconds that the database should wait to obtain a pessimistic lock before returning an error to the client.

public boolean

Returns:

true if this object defines alias-specific lock modes; false otherwise.
hasAliasSpecificLockModes
()

Does this LockOptions instance define alias-specific lock modes?

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
public boolean

Returns:

true if the lock options are equivalent to LockOptions#NONE.
isEmpty
()

Determine of the lock options are empty.

public LockOptions

Returns:

The copy
makeCopy
()

Make a copy.

public LockOptions

Returns:

The copy, or this if it was immutable.
makeDefensiveCopy
()

Make a copy, unless this is an immutable instance.

public void
overlay(LockOptions lockOptions)

Copy the given lock options into this instance, merging the alias-specific lock modes.

public LockOptions

Returns:

this for method chaining
setAliasSpecificLockMode
(String
the query alias to which the lock mode applies
alias
,
LockMode
the lock mode to apply to the given alias
lockMode
)

Specify the LockMode to be used for the given query alias.

public LockOptions

Returns:

this for method chaining
setFollowOnLocking
(Boolean
The new follow-on locking setting
followOnLocking
)

Force enable or disable the use of follow-on locking, overriding the default behavior of the SQL dialect.

public LockOptions

Returns:

this for method chaining
setLockMode
(LockMode
the new overall lock mode
lockMode
)

Set the overall lock mode.

public LockOptions

Returns:

this for method chaining
setLockScope
(PessimisticLockScope scope)

Set the lock scope:

public LockOptions

Returns:

this for method chaining
setScope
(boolean
the new scope setting
scope
)

Deprecated since 6.2. use setLockScope(PessimisticLockScope)
Set the lock scope setting:
  • true means the lock extends to rows of owned collections, but
  • false means only the entity table and secondary tables are locked.
public LockOptions

Returns:

this for method chaining
setTimeOut
(int
the new timeout setting, in milliseconds
timeout
)

Set the timeout, that is, the maximum amount of time in milliseconds that the database should wait to obtain a pessimistic lock before returning an error to the client.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

aliasSpecificLockModesback to summary
private Map<String, LockMode> aliasSpecificLockModes
followOnLockingback to summary
private Boolean followOnLocking
immutableback to summary
private final boolean immutable
lockModeback to summary
private LockMode lockMode
NO_WAITback to summary
public static final int NO_WAIT

Indicates that the database should not wait at all to acquire a pessimistic lock which is not immediately available. This has the same effect as LockMode#UPGRADE_NOWAIT.

See Also
getTimeOut
NONEback to summary
public static final LockOptions NONE

Represents LockMode#NONE, to which timeout and scope are not applicable.

OPTIMISTICback to summary
pack-priv static final LockOptions OPTIMISTIC

Represents LockMode#OPTIMISTIC.

OPTIMISTIC_FORCE_INCREMENTback to summary
pack-priv static final LockOptions OPTIMISTIC_FORCE_INCREMENT

Represents LockMode#OPTIMISTIC_FORCE_INCREMENT, to which timeout and scope are not applicable.

PESSIMISTIC_FORCE_INCREMENTback to summary
pack-priv static final LockOptions PESSIMISTIC_FORCE_INCREMENT

Represents LockMode#PESSIMISTIC_FORCE_INCREMENT.

PESSIMISTIC_READback to summary
pack-priv static final LockOptions PESSIMISTIC_READ

Represents LockMode#PESSIMISTIC_READ.

PESSIMISTIC_WRITEback to summary
pack-priv static final LockOptions PESSIMISTIC_WRITE

Represents LockMode#PESSIMISTIC_WRITE.

READback to summary
public static final LockOptions READ

Represents LockMode#READ, to which timeout and scope are not applicable.

scopeback to summary
private boolean scope
SKIP_LOCKEDback to summary
public static final int SKIP_LOCKED

Indicates that rows which are already locked should be skipped.

Annotations
@Deprecated
See Also
getTimeOut()
timeoutback to summary
private int timeout
UPGRADEback to summary
public static final LockOptions UPGRADE

Represents LockMode#PESSIMISTIC_WRITE with no timeout, and no extension of the lock to owned collections.

UPGRADE_NOWAITback to summary
pack-priv static final LockOptions UPGRADE_NOWAIT

Represents LockMode#UPGRADE_NOWAIT.

UPGRADE_SKIPLOCKEDback to summary
pack-priv static final LockOptions UPGRADE_SKIPLOCKED

Represents LockMode#UPGRADE_SKIPLOCKED.

WAIT_FOREVERback to summary
public static final int WAIT_FOREVER

Indicates that there is no timeout for the lock acquisition, that is, that the database should in principle wait forever to obtain the lock.

See Also
getTimeOut

Constructor Detail

LockOptionsback to summary
public LockOptions()

Construct an instance with mode LockMode#NONE and timeout WAIT_FOREVER.

LockOptionsback to summary
public LockOptions(LockMode lockMode)

Construct an instance with the given mode and WAIT_FOREVER.

Parameters
lockMode:LockMode

The initial lock mode

LockOptionsback to summary
public LockOptions(LockMode lockMode, int timeout)

Construct an instance with the given mode and timeout.

Parameters
lockMode:LockMode

The initial lock mode

timeout:int

The initial timeout

LockOptionsback to summary
public LockOptions(LockMode lockMode, int timeout, PessimisticLockScope scope)

Construct an instance with the given mode, timeout, and scope.

Parameters
lockMode:LockMode

The initial lock mode

timeout:int

The initial timeout

scope:PessimisticLockScope

The initial lock scope

LockOptionsback to summary
private LockOptions(boolean immutable, LockMode lockMode)

Internal operation used to create immutable global instances.

Method Detail

copyback to summary
public static LockOptions copy(LockOptions source, LockOptions destination)

Copy the options in the first given instance of LockOptions to the second given instance.

Parameters
source:LockOptions

Source for the copy (copied from)

destination:LockOptions

Destination for the copy (copied to)

Returns:LockOptions

destination

equalsback to summary
public boolean equals(Object object)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
object:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
findGreatestLockModeback to summary
public LockMode findGreatestLockMode()

Currently needed for follow-on locking.

Returns:LockMode

The greatest of all requested lock modes.

getAliasLockCountback to summary
public int getAliasLockCount()

The number of aliases that have alias-specific lock modes specified.

Returns:int

the number of explicitly defined alias lock modes.

getAliasLockIteratorback to summary
public Iterator<Map.Entry<String, LockMode>> getAliasLockIterator()

Deprecated

use getAliasSpecificLocks()

Iterator over Map.Entrys, each containing an alias and its LockMode.

Returns:Iterator<Map.Entry<String, LockMode>>

an iterator over the Map.Entrys

Annotations
@Deprecated
getAliasSpecificLockModeback to summary
public LockMode getAliasSpecificLockMode(String alias)

Get the LockMode explicitly specified for the given alias via setAliasSpecificLockMode(String, LockMode).

Differs from getEffectiveLockMode(String) in that here we only return an explicitly specified alias-specific lock mode.

Parameters
alias:String

The alias for which to locate the explicit lock mode.

Returns:LockMode

The explicit lock mode for that alias.

getAliasSpecificLocksback to summary
public Set<Map.Entry<String, LockMode>> getAliasSpecificLocks()

Set of Map.Entrys, each associating an alias with its specified alias-specific LockMode.

Returns:Set<Map.Entry<String, LockMode>>

an iterable with the Map.Entrys

getEffectiveLockModeback to summary
public LockMode getEffectiveLockMode(String alias)

Determine the LockMode to apply to the given alias. If no mode was setAliasSpecificLockMode(String, LockMode) explicitly set}, the getLockMode() overall mode} is returned. If the overall lock mode is also null, LockMode#NONE is returned.

Differs from getAliasSpecificLockMode(String) in that here we fall back to only returning the overall lock mode.

Parameters
alias:String

The alias for which to locate the effective lock mode.

Returns:LockMode

The effective lock mode.

getFollowOnLockingback to summary
public Boolean getFollowOnLocking()

Returns a value indicating if follow-on locking was force enabled or disabled, overriding the default behavior of the SQL dialect.

Returns:Boolean

true if follow-on locking was force enabled, false if follow-on locking was force disabled, or null if the default behavior of the dialect has not been overridden.

See Also
org.hibernate.jpa.HibernateHints#HINT_FOLLOW_ON_LOCKING, org.hibernate.dialect.Dialect#useFollowOnLocking(String, QueryOptions)
getLockModeback to summary
public LockMode getLockMode()

Retrieve the overall lock mode in effect for this set of options.

Returns:LockMode

the overall lock mode

getLockScopeback to summary
public PessimisticLockScope getLockScope()

The current lock scope:

Returns:PessimisticLockScope

the current PessimisticLockScope

getScopeback to summary
public boolean getScope()

Deprecated

since 6.2.

use getLockScope()

The current lock scope setting:

  • true means the lock extends to rows of owned collections, but
  • false means only the entity table and secondary tables are locked.
Returns:boolean

true if the lock extends to owned associations

Annotations
@Deprecated
since:6.2
getTimeOutback to summary
public int getTimeOut()

The current timeout, a maximum amount of time in milliseconds that the database should wait to obtain a pessimistic lock before returning an error to the client.

NO_WAIT, WAIT_FOREVER, or SKIP_LOCKED represent 3 "magic" values.

Returns:int

a timeout in milliseconds, NO_WAIT, WAIT_FOREVER, or SKIP_LOCKED

hasAliasSpecificLockModesback to summary
public boolean hasAliasSpecificLockModes()

Does this LockOptions instance define alias-specific lock modes?

Returns:boolean

true if this object defines alias-specific lock modes; false otherwise.

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

Annotations
@Override
isEmptyback to summary
public boolean isEmpty()

Determine of the lock options are empty.

Returns:boolean

true if the lock options are equivalent to LockOptions#NONE.

makeCopyback to summary
public LockOptions makeCopy()

Make a copy. The new copy will be mutable even if the original wasn't.

Returns:LockOptions

The copy

makeDefensiveCopyback to summary
public LockOptions makeDefensiveCopy()

Make a copy, unless this is an immutable instance.

Returns:LockOptions

The copy, or this if it was immutable.

overlayback to summary
public void overlay(LockOptions lockOptions)

Copy the given lock options into this instance, merging the alias-specific lock modes.

setAliasSpecificLockModeback to summary
public LockOptions setAliasSpecificLockMode(String alias, LockMode lockMode)

Specify the LockMode to be used for the given query alias.

Parameters
alias:String

the query alias to which the lock mode applies

lockMode:LockMode

the lock mode to apply to the given alias

Returns:LockOptions

this for method chaining

See Also
Query#setLockMode(String, LockMode)
setFollowOnLockingback to summary
public LockOptions setFollowOnLocking(Boolean followOnLocking)

Force enable or disable the use of follow-on locking, overriding the default behavior of the SQL dialect.

Parameters
followOnLocking:Boolean

The new follow-on locking setting

Returns:LockOptions

this for method chaining

See Also
org.hibernate.jpa.HibernateHints#HINT_FOLLOW_ON_LOCKING, org.hibernate.dialect.Dialect#useFollowOnLocking(String, QueryOptions)
setLockModeback to summary
public LockOptions setLockMode(LockMode lockMode)

Set the overall lock mode. The default is LockMode#NONE, that is, no locking at all.

Parameters
lockMode:LockMode

the new overall lock mode

Returns:LockOptions

this for method chaining

setLockScopeback to summary
public LockOptions setLockScope(PessimisticLockScope scope)

Set the lock scope:

Parameters
scope:PessimisticLockScope

the new PessimisticLockScope

Returns:LockOptions

this for method chaining

setScopeback to summary
public LockOptions setScope(boolean scope)

Deprecated

since 6.2.

use setLockScope(PessimisticLockScope)

Set the lock scope setting:

  • true means the lock extends to rows of owned collections, but
  • false means only the entity table and secondary tables are locked.
Parameters
scope:boolean

the new scope setting

Returns:LockOptions

this for method chaining

Annotations
@Deprecated
since:6.2
setTimeOutback to summary
public LockOptions setTimeOut(int timeout)

Set the timeout, that is, the maximum amount of time in milliseconds that the database should wait to obtain a pessimistic lock before returning an error to the client.

NO_WAIT, WAIT_FOREVER, or SKIP_LOCKED represent 3 "magic" values.

Parameters
timeout:int

the new timeout setting, in milliseconds

Returns:LockOptions

this for method chaining

See Also
getTimeOut