IS_USED_LOCK

Returns the connection ID of the session that holds the named user-level advisory lock, or NULL if the lock is free (not held by any session). Lock names are case-insensitive and must not exceed 64 characters.

Syntax

IS_USED_LOCK(str)

Arguments

Parameter

Description

str

The name of the user-level advisory lock to check. Case-insensitive, maximum 64 characters. Must not contain NUL bytes.

Return Value

  • Returns the connection ID (BIGINT UNSIGNED) of the session currently holding the lock.

  • Returns NULL if the lock is free (not held by any session).

  • Returns NULL if the lock name is NULL.

Usage Notes

  • Lock names are normalized to lowercase before lookup.

  • If the lock holder cannot be determined (e.g., the lock was acquired by an older version of MatrixOne), the function returns NULL.

  • This function works with locks created by GET_LOCK() in the same MatrixOne instance.

Examples

DROP DATABASE IF EXISTS is_used_lock_demo;
CREATE DATABASE is_used_lock_demo;
USE is_used_lock_demo;

-- Create a table to record the lock holder connection ID
CREATE TABLE lock_holder (conn_id BIGINT UNSIGNED);
INSERT INTO lock_holder VALUES (CONNECTION_ID());

-- Acquire a lock
SELECT GET_LOCK('demo_lock', 0);
-- Returns 1 (lock acquired)

-- Check who holds the lock
SELECT IS_USED_LOCK('demo_lock') = (SELECT conn_id FROM lock_holder);
-- Returns 1 (current connection is the holder)

-- Release the lock
SELECT RELEASE_LOCK('demo_lock');
-- Returns 1 (lock released)

-- Lock is now free
SELECT IS_USED_LOCK('demo_lock');
-- Returns NULL

-- Acquire the lock again and release all
SELECT GET_LOCK('demo_lock', 0);
SELECT IS_USED_LOCK('demo_lock') IS NOT NULL;
-- Returns 1 (lock is held)

SELECT RELEASE_ALL_LOCKS();

SELECT IS_USED_LOCK('demo_lock');
-- Returns NULL (lock released)

-- Clean up
DROP TABLE lock_holder;
DROP DATABASE is_used_lock_demo;

See Also