The DBMS_LOCK package provides interfaces for OceanBase Database lock management.
The DBMS_LOCK package provides the following main functions:
Mutual exclusion: grants exclusive access to certain devices, such as a terminal.
The application can detect lock release: After the application releases or clears the lock, others can detect it.
Synchronization: Coordination and synchronization of different applications or processes.
Applicability
This topic applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition is available only in MySQL-compatible mode.
Privilege requirements
The database system has a limit on the total number of available locks. When using locks or granting this package to other users, consider granting the EXECUTE privilege only to specific users or roles.
DBMS_LOCK Constants
The DBMS_LOCK package includes the following constants, which can be used when you specify parameter values:
| Name | Property Name | Type | Value | OEM Abbreviation | Description |
|---|---|---|---|---|---|
| NL_MODE | Nul1 | INTEGER | 1 | No | NUL mode |
| SS_MODE | Sub Shared | INTEGER | 2 | ULRS | Exclusive Read Lock |
| SX_MODE | Sub eXclusive/Row Exclusive Mode | INTEGER | 3 | ULRX | Intention write lock |
| S_MODE | Shared/Row Exclusive Mode/Intended Exclusive | INTEGER | 4 | ULRSX | Read lock |
| SSX_MODE | Shared Sub eXclusive/Share Row Exclusive Mode | INTEGER | 5 | none | shared intent to write lock |
| X_MODE | Exclusive | INTEGER | 6 | ULX | Write lock |
Rules and limitations of DBMS_LOCK
The following table describes the lock compatibility matrix. Y indicates a successful request, and N indicates a failed request.
| Lock mode | NL lock | SS lock | SX lock | S lock | SSX lock | X lock |
|---|---|---|---|---|---|---|
| NL | Y | Y | Y | Y | Y | Y |
| SS | Y | Y | Y | Y | Y | N |
| SX | Y | Y | Y | N | N | N |
| S | Y | Y | N | Y | N | N |
| SSX | Y | Y | N | N | N | N |
| X | Y | N | N | N | N | N |
You can set the default wait time maxwait when you request a lock.
maxwait constant integer := 32767;
Considerations for using DBMS_LOCK
- User locks have the "UL" prefix and do not conflict with table locks.
- When a session disconnects, the lock is released.
- The lock ID can be in the range of (0, 1073741823).
- A user lock behaves the same as a table lock and has all the capabilities of a table lock, such as deadlock detection.
- Ensure that user locks used in a distributed transaction are released when the transaction is committed. Otherwise, a deadlock might occur.
- For each session, the best practice when using
DBMS_LOCKis to keep the number of locks to less than a few hundred.
Overview of DBMS_LOCK subprograms
The following table describes the DBMS_LOCK subprograms and their purposes in OceanBase Database.
| Subroutine | Purpose |
|---|---|
| ALLOCATE_UNIQUE | Requests a unique lock ID for the lock name, where the lock ID ranges from 1073741824 to 1999999999. |
| ALLOCATE_UNIQUE_AUTONOMOUS | Allocates a unique lock ID for a lock name. |
| REQUEST | Acquires the lock. |
| SLEEP | Blocks the current session for the specified duration. |
| RELEASE | Explicitly release a lock obtained by using the REQUEST function. |