GET_LOCK()
This page is part of MariaDB's Documentation.
The parent of this page is: Functions for MariaDB Xpand
Topics on this page:
Overview
Attempt to create a user-level lock and return the success status as a boolean.
USAGE
GET_LOCK(lock_name, timeout)
Argument Name | Description |
---|---|
| Required. The string argument that identifies a lock to acquire |
| Required. The timeout in seconds for which the named lock is tried to be acquired |
DETAILS
GET_LOCK()
is a locking function that acquires the named user-level lock and returns a boolean (1
or 0
) to indicate success or failure.
The lock names are determined by the user, and can be used in any desired programming logic.
The lock only remains locked as long as the calling user's session remains connected to the database. It can also be released using RELEASE_
If the calling user already holds a user level lock, calling GET_LOCK()
again succeeds.
If anyone else is holding the lock, the function waits for the given number of seconds for the lock to become available and returns 0
if time ran out before acquiring the lock.
Other related locking functions include IS_
An error is generated if the argument is NULL
.
EXAMPLES
Simple Locking Idiom
An example use of GET_LOCK()
is to implement an agreed upon restriction, such as serializing the use of several related tables using a single lock name.
Let user1 acquire the user lock contacts_lck
with the GET_LOCK()
function:
SELECT GET_LOCK("contacts_lck", 10);
+------------------------------+
| GET_LOCK("contacts_lck", 10) |
+------------------------------+
| 1 |
+------------------------------+
After user1 has acquired the contacts_lck
user level lock, if user2 tries to acquire the contacts_lck
lock it is not able to acquire the contacts_lck
lock:
SELECT GET_LOCK("contacts_lck", 10);
+------------------------------+
| GET_LOCK("contacts_lck", 10) |
+------------------------------+
| 0 |
+------------------------------+
ERROR 1 (HY000): [24576] Unknown prepared param type: "t_null"