Lock functions

Updated at: 2024-05-23 09:28

PolarDB-X 2.4.0_5.4.19-20240426 and later versions support all lock functions of MySQL 5.7. The following table lists the lock functions. For more information, see Locking Functions.

Function name

Description

GET_LOCK(str, timeout)

Obtains a lock. Replace str with a lock name and timeout with a timeout period for the lock. Return values:

  • 1: The lock is obtained.

  • 0: The attempt timed out.

  • NULL: An error occurred.

IS_FREE_LOCK(str)

Determines whether a lock is in use. Replace str with a lock name. Return values:

  • 1: The lock is not in use.

  • 0: The lock is in use.

  • NULL: An error occurred.

IS_USED_LOCK(str)

Queries the client session that uses a lock. Replace str with a lock name. If the lock is in use, the connection identifier of the client session that is using the lock is returned. If the lock is not in use, NULL is returned.

RELEASE_LOCK(str)

Releases a lock. Replace str with a lock name. Return values:

  • 1: The lock is released.

  • 0: The lock is not released because it is not created by the current thread.

  • NULL: The lock is not released because it does not exist.

RELEASE_ALL_LOCK()

Releases all named locks held by the current session. The number of released locks is returned. If no lock is released, 0 is returned.

Related functions

PolarDB-X also supports function IS_MY_LOCK(str), which is used to check whether the current session holds a named lock. If the session holds the lock, 1 is returned. If the session does not hold the lock, 0 is returned.

Suggestions

We recommend that you run the IS_MY_LOCK function every 60 seconds to check lock holding. This prevents lock failures caused by partition split-brain issues that may occur at slight possibilities when locks are held for extended periods of time. The following pseudocode provides an example on checking lock holding:

get_lock(lock_name);// Obtain the lock.

while(is_my_lock(lock_name)){ // Check whether the lock is valid.
    do_work_with_timeout(60); // Execute the business operation. The time limit is 60s.
    if(work_finished){ // Check whether the execution has ended.
        break;
    }
}

release_lock(lock_name);// Release the lock.
  • On this page (1, T)
  • Related functions
  • Suggestions
Feedback
phone Contact Us

Chat now with Alibaba Cloud Customer Service to assist you in finding the right products and services to meet your needs.

alicare alicarealicarealicare