Bobcares

MySQL get_lock

by | May 10, 2022

mysql get_lock is a locking function using which we can get a named lock.

As part of our Server administration services, Bobcares provides queries.

Let’s delve into the specifics of the mysql get_lock function.

MySQL get_lock

The syntax of MySQL get_lock locking function is GET_LOCK(str,timeout). It attempts to obtain a lock with the string str as the name, with a timeout of timeout seconds. It returns 1 if the lock was successfully obtained, returns 0 if it timed out, and NULL if there was an error. For GET_LOCK() and related functions, str is case insensitive. If str is null or an empty string, GET_LOCK() returns NULL and does nothing.

We can release the lock obtained with GET_LOCK() explicitly by executing RELEASE_LOCK(). It will also be released implicitly when the session ends. When a transaction commits or rolls back, the locks obtained with GET_LOCK() are not released.

The metadata locking (MDL) subsystem was used to reimplement  GET_LOCK() in the most recent version of MySQL, and its capabilities were expanded. Multiple locks can be acquired at the same time, and GET_LOCK() does not release any existing locks. It is also possible for a single session to obtain multiple locks with the same name.

Result of MDL reimplementation

The Performance Schema metadata_locks table now contains uniquely named locks acquired with GET_LOCK() as a result of the MDL reimplementation. USER LEVEL LOCK is the OBJECT_TYPE column, and the OBJECT_NAME column is the lock name.

Only the first lock for a name registers a row in the metadata locks table if multiple locks are acquired for the same name. The counter in the lock is incremented by subsequent locks for the name, but no additional metadata locks are acquired. When the last lock instance on the name is released, the metadata_locks row for the lock is deleted.

Locks acquired with this function appear in the Information Schema METADATA_LOCK_INFO table if the metadata_lock_info plugin is installed. For statement-based replication, statements that use the GET_LOCK() function are not safe.

Only one simultaneous lock can be acquired before MySQL 5.7, and GET_LOCK() releases any existing lock. The following example demonstrates the difference in mysql get_lock acquisition behaviour in MySQL 5.7. Assume we run the following commands:


SELECT GET_LOCK('lock1',10);
SELECT GET_LOCK('lock2',10);
SELECT RELEASE_LOCK('lock2');
SELECT RELEASE_LOCK('lock1');

The second GET_LOCK() in MySQL 5.7 or later acquires a second lock, and both RELEASE LOCK() calls return 1. (success). Because there is no ‘lock1’ to release before MySQL 5.7, the second GET_LOCK() releases the first lock (‘lock1’), and the second RELEASE LOCK() returns NULL (failure).

[Looking for a solution to another query? We are just a click away.]

Conclusion

To sum up, we have looked into the details of the mysql get_lock functions.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF