Package org.hibernate

Class LockOptions

java.lang.Object
org.hibernate.LockOptions
All Implemented Interfaces:
FindOption, RefreshOption, Serializable

public class LockOptions extends Object implements FindOption, RefreshOption, Serializable
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).

See Also:
  • Field Details

  • Constructor Details

    • LockOptions

      public LockOptions()
      Construct an instance with mode LockMode.NONE and timeout WAIT_FOREVER.
    • LockOptions

      public LockOptions(LockMode lockMode)
      Construct an instance with the given mode and WAIT_FOREVER.
      Parameters:
      lockMode - The initial lock mode
    • LockOptions

      public LockOptions(LockMode lockMode, int timeout)
      Construct an instance with the given mode and timeout.
      Parameters:
      lockMode - The initial lock mode
      timeout - The initial timeout
    • LockOptions

      public LockOptions(LockMode lockMode, int timeout, PessimisticLockScope scope)
      Construct an instance with the given mode, timeout, and scope.
      Parameters:
      lockMode - The initial lock mode
      timeout - The initial timeout
      scope - The initial lock scope
  • Method Details

    • isEmpty

      public boolean isEmpty()
      Determine of the lock options are empty.
      Returns:
      true if the lock options are equivalent to NONE.
    • getLockMode

      public LockMode getLockMode()
      Retrieve the overall lock mode in effect for this set of options.
      Returns:
      the overall lock mode
    • setLockMode

      public LockOptions setLockMode(LockMode lockMode)
      Set the overall lock mode. The default is LockMode.NONE, that is, no locking at all.
      Parameters:
      lockMode - the new overall lock mode
      Returns:
      this for method chaining
    • setAliasSpecificLockMode

      public LockOptions setAliasSpecificLockMode(String alias, LockMode lockMode)
      Specify the LockMode to be used for the given query alias.
      Parameters:
      alias - the query alias to which the lock mode applies
      lockMode - the lock mode to apply to the given alias
      Returns:
      this for method chaining
      See Also:
    • getAliasSpecificLockMode

      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 - The alias for which to locate the explicit lock mode.
      Returns:
      The explicit lock mode for that alias.
    • getEffectiveLockMode

      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 - The alias for which to locate the effective lock mode.
      Returns:
      The effective lock mode.
    • hasAliasSpecificLockModes

      public boolean hasAliasSpecificLockModes()
      Does this LockOptions instance define alias-specific lock modes?
      Returns:
      true if this object defines alias-specific lock modes; false otherwise.
    • getAliasLockCount

      public int getAliasLockCount()
      The number of aliases that have alias-specific lock modes specified.
      Returns:
      the number of explicitly defined alias lock modes.
    • getAliasLockIterator

      @Deprecated public Iterator<Map.Entry<String,LockMode>> getAliasLockIterator()
      Deprecated.
      Iterator over Map.Entrys, each containing an alias and its LockMode.
      Returns:
      an iterator over the Map.Entrys
    • getAliasSpecificLocks

      public Set<Map.Entry<String,LockMode>> getAliasSpecificLocks()
      Set of Map.Entrys, each associating an alias with its specified alias-specific LockMode.
      Returns:
      an iterable with the Map.Entrys
    • findGreatestLockMode

      public LockMode findGreatestLockMode()
      Currently needed for follow-on locking.
      Returns:
      The greatest of all requested lock modes.
    • getTimeOut

      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:
      a timeout in milliseconds, NO_WAIT, WAIT_FOREVER, or SKIP_LOCKED
    • setTimeOut

      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 - the new timeout setting, in milliseconds
      Returns:
      this for method chaining
      See Also:
    • getLockScope

      public PessimisticLockScope getLockScope()
      The current lock scope:
      Returns:
      the current PessimisticLockScope
    • setLockScope

      public LockOptions setLockScope(PessimisticLockScope scope)
      Set the lock scope:
      Parameters:
      scope - the new PessimisticLockScope
      Returns:
      this for method chaining
    • getFollowOnLocking

      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:
      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:
    • setFollowOnLocking

      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 - The new follow-on locking setting
      Returns:
      this for method chaining
      See Also:
    • makeCopy

      public LockOptions makeCopy()
      Make a copy. The new copy will be mutable even if the original wasn't.
      Returns:
      The copy
    • makeDefensiveCopy

      public LockOptions makeDefensiveCopy()
      Make a copy, unless this is an immutable instance.
      Returns:
      The copy, or this if it was immutable.
    • overlay

      public void overlay(LockOptions lockOptions)
      Copy the given lock options into this instance, merging the alias-specific lock modes.
    • copy

      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 - Source for the copy (copied from)
      destination - Destination for the copy (copied to)
      Returns:
      destination
    • equals

      public boolean equals(Object object)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object