docs.hibernate.org

All Implemented Interfaces:
FindOption, RefreshOption, Serializable, Comparable<LockMode>, Constable

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.

A partial order of lock modes is defined such that every optimistic lock mode is considered a weaker sort of lock than every pessimistic lock mode. If a session already holds a stronger lock level on a given entity when a weaker lock level is requested, then the request for the weaker lock level has no effect.

Note that, in particular, a request for the optimistic lock level OPTIMISTIC_FORCE_INCREMENT on an entity for which any pessimistic lock is already held has no effect, and does not force a version increment.

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

See Also:
  • Nested Class Summary

  • Enum Constant Summary

    Enum Constants

    No lock required.

    A shared optimistic lock.

    A kind of exclusive optimistic lock.

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

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

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

    A shared lock.

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

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

    An exclusive write lock.

  • Method Summary

    boolean

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

    boolean

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

    boolean

    Does this lock mode require a version?

    Deprecated, for removal: This API element is subject to removal in a future version.

    Returns the enum constant of this class with the specified name.

    values()

    Returns an array containing the constants of this enum class, in the order they are declared.

  • Enum Constant Details

    • NONE

      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.find(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:
    • READ

      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, despite the similar names this lock mode is not the same as the JPA-defined mode LockModeType.READ.

    • OPTIMISTIC

      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.

      Only legal for versioned entity types.

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

      See Also:
    • OPTIMISTIC_FORCE_INCREMENT

      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.

      Only legal for versioned entity types.

      See Also:
    • WRITE

    • PESSIMISTIC_READ

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

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

      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.

      Only legal for versioned entity types.

      See Also:
    • 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.

      API Note:
      To be removed in a future version. A different approach to specifying handling for locked rows will be introduced.
    • 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.

      API Note:
      To be removed in a future version. A different approach to specifying handling for locked rows will be introduced.
  • Method Details

    • values

      public static LockMode[] 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 name
      NullPointerException - if the argument is null
    • fromJpaLockMode

      Returns:
      an instance with the same semantics as the given JPA LockModeType.
    • toJpaLockMode

      Returns:
      an instance of the JPA-defined LockModeType with similar semantics to the given LockMode.
    • toJpaLockMode

      Returns:
      an instance of the JPA-defined LockModeType with similar semantics to this LockMode.
    • greaterThan

      public boolean greaterThan (LockMode mode)

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

      Parameters:
      mode - LockMode to check
      Returns:
      true if this lock mode is more restrictive than given lock mode
    • lessThan

      public boolean lessThan (LockMode mode)

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

      Parameters:
      mode - LockMode to check
      Returns:
      true if this lock mode is less restrictive than given lock mode
    • requiresVersion

      public boolean requiresVersion()

      Does this lock mode require a version?

      Returns:
      true if this lock mode only applies to versioned entities
    • toExternalForm

      public String toExternalForm()

    • fromExternalForm

      public static LockMode fromExternalForm (String externalForm)

    • toLockOptions

      Deprecated, for removal: This API element is subject to removal in a future version.

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

Read the original on docs.hibernate.org ↗