Chapter 17. DBMS_LOCK

Table of Contents

17.1. Overview
17.2. Procedures
17.2.1. ALLOCATE_UNIQUE
17.2.2. CONVERT
17.2.3. RELEASE
17.2.4. REQUEST
17.2.5. SLEEP

This chapter briefly introduces the DBMS_LOCK package, and describes how to use the procedures of the package.

17.1. Overview

DBMS_LOCK provides lock management and SLEEP functions for users. By default, only administrators can use this package.

DBMS_LOCK uses the following defined constants.

NameAliasTypeValue
NS_MODENullINTEGER1
SS_MODESub sharedINTEGER2
SX_MODESub eXclusive or Row Exclusive ModeINTEGER3
S_MODEShared or Row Exclusive Mode or Intended ExclusiveINTEGER4
SSX_MODEShared Sub eXclusive or Shared Row Exclusive ModeINTEGER5
X_MODEExclusiveINTEGER6

A lock request for an already occupied lock is processed according to the following table.

ModeNL RequestSS RequestSX RequestS RequestSSX RequestX Request
NLSuccessSuccessSuccessSuccessSuccessSuccess
SSSuccessSuccessSuccessSuccessSuccessFailure
SXSuccessSuccessSuccessSuccessFailureFailure
SSuccessSuccessFailureSuccessFailureFailure
SSXSuccessSuccessFailureFailureFailureFailure
XSuccessFailureFailureFailureFailureFailure

If set to the constant MAXWAIT, the session waits forever for the lock.

maxwait constant pls_integer := 32767; 

17.2. Procedures

This section describes the procedures provided by the DBMS_LOCK package, in alphabetical order.

17.2.1. ALLOCATE_UNIQUE

Allocates a lock ID to the specified lockname parameter. A lock ID can range from 1073741824 to 1999999999. A user can easily manage a lock using its name, instead of its number.

Details about the ALLOCATE_UNIQUE procedure are as follows:

  • Prototype

    DBMS_LOCK.ALLOCATE_UNIQUE
    (
        lockname            IN          VARCHAR2,                                                      
        lockhandle          OUT         VARCHAR2,                                                      
        expiration_secs     IN          PLS_INTEGER     DEFAULT 864000
    )
  • Parameter

    ParameterDescription
    locknameLock name to use to create a unique lockhandle value.
    lockhandle

    Handle returned for the created lock ID. REQUEST, CONVERT, and RELEASE procedures are called through this handle.

    To protect the lock ID from misuse, the handle value set in a VARCHAR2 (128) variable set to the handle value is returned instead of the lock ID. Since a handle is valid only within the session, it must not be used by another session.

    expiration_secs

    Allowed time to reuse the DBMS_LOCK_ALLOCATED table. (Unit: second, default value: 10 days)

    Time is calculated starting from when the last ALLOCATE_UNIQUE procedure is called.

    A lock must not be removed directly from the DBMS_LOCK_ALLOCATED table. This parameter is valid only when the _DBMS_LOCK_REUSE initialization parameter is set to Y.

  • Example

    DECLARE
        lock_handle VARCHAR2(128);
    BEGIN
        DBMS_LOCK.ALLOCATE_UNIQUE('my_lock', lock_handle);
    END;
    /

17.2.2. CONVERT

Changes the lock mode.

Details about the CONVERT procedure are as follows:

  • Prototype

    DBMS_LOCK.CONVERT
    (
        id         IN INTEGER || 
        lockhandle IN VARCHAR2,
        lockmode   IN INTEGER,
        timeout    IN NUMBER DEFAULT MAXWAIT
    ) 
    RETURN INTEGER;
  • Parameter

    ParameterDescription
    id or lockhandleLock ID or handle used to change the lock mode.
    lockmodeNew lock mode.
    timeout

    Waiting time to change the lock mode. (Unit: second)

    A timeout error is returned if the lock cannot be obtained within this time.

  • Return Value

    ValueDescription
    0Success
    1Timeout
    2Deadlock
    3Parameter error
    4Owner error
    5Illegal lock handle
  • Example

    BEGIN
        DBMS_LOCK.CONVERT(lock_handle, DBMS_LOCK.X_MODE, 600);
    END;
    /

17.2.3. RELEASE

Explicitly releases a lock. By default, a lock is automatically released when the session ends.

Details about the RELEASE procedure are as follows:

  • Prototype

    DBMS_LOCK.RELEASE
    (
        id         IN INTEGER || 
        lockhandle IN VARCHAR2
    ) 
    RETURN INTEGER;
  • Parameter

    ParameterDescription
    id or lockhandleLock ID or handle used to release the lock.
  • Return Value

    ValueDescription
    0Success
    3Parameter error
    4Owner error
    5Illegal lock handle
  • Example

    BEGIN
        DBMS_LOCK.RELEASE(lock_handle);
    END;
    /

17.2.4. REQUEST

Requests a lock by specifying the lock mode.

Details about the REQUEST procedure are as follows:

  • Prototype

    DBMS_LOCK.REQUEST
    (
        id                 IN  INTEGER ||
        lockhandle         IN  VARCHAR2,
        lockmode           IN  INTEGER DEFAULT X_MODE,
        timeout            IN  INTEGER DEFAULT MAXWAIT,
        release_on_commit  IN  BOOLEAN DEFAULT FALSE
    ) 
    RETURN INTEGER;
  • Parameter

    ParameterDescription
    id or lockhandleLock ID or handle used to request the lock.
    lockmodeRequested lock mode.
    timeout

    Waiting time to get the lock. (Unit: second)

    A timeout error is returned if the lock cannot be obtained within this time.

    release_on_commit
    • TRUE: release the lock during commit or rollback.

    • FALSE: do not release the lock until an explicit request is received or the session ends.

  • Return Value

    ValueDescription
    0Success
    1Timeout
    2Deadlock
    3Parameter error
    4Already owned by id or lock handle
    5Illegal lock handle
  • Example

    BEGIN
        DBMS_LOCK.REQUEST(lock_handle, DBMS_LOCK.S_MODE, DBMS_LOCK.MAXWAIT, TRUE);
    END;
    /

17.2.5. SLEEP

Suspends the current session for the specified time period.

Details about the SLEEP procedure are as follows:

  • Prototype

    DBMS_LOCK.SLEEP
    (
        seconds        IN         NUMBER
    )
  • Parameter

    ParameterDescription
    secondsPeriod of time for which the session is suspended. (Unit: second)
  • Example

    DECLARE
       second NUMBER;
    BEGIN
       second := 0.11;
       DBMS_LOCK.SLEEP(second);
    END;
    /