ID EN
Locking Functions

GET_LOCK(str,timeout)

MySQL 8.4 🇮🇩 Bahasa Indonesia

Mencoba mendapatkan kunci dengan nama yang diberikan oleh string str, menggunakan batas waktu habis detik. Nilai batas waktu negatif berarti batas waktu tidak terbatas. Kuncinya eksklusif. Saat diadakan oleh satu sesi, sesi lainnya tidak dapat memperoleh kunci dengan nama yang sama.

Syntax

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

Contoh

Example

Returns 1 if the lock was obtained successfully, 0 if the attempt timed out (for example, because another client has previously locked the name), or NULL if an error occurred (such as running out of memory or the thread was killed with mysqladmin kill).

MYSQL
SELECT GET_LOCK('lock1',10);
SELECT GET_LOCK('lock2',10);
SELECT RELEASE_LOCK('lock2');
SELECT RELEASE_LOCK('lock1');
Example 2

A lock obtained with GET_LOCK() is released explicitly by executing RELEASE_LOCK() or implicitly when your session terminates (either normally or abnormally). Locks obtained with GET_LOCK() are not released when transactions commit or roll back.

MYSQL
INSERT INTO ... SELECT GET_LOCK(t1.col_name) FROM t1;