GET_LOCK
Sintassi
GET_LOCK(str, timeout)
Spiegazione
Cerca di acquisire un lock con il nome specificato nella stringa str, che scade dopo timeout secondi. Restituisce 1
se il lock viene acquisito con successo, 0
se il timeout scade (per esempio, perché un altro client ha prenotato quel nome in precedenza), o NULL
se si verifica un errore (ad esempio se la memoria si esaurisce o il thread viene terminato con mysqladmin kill). I lock ottenuti con GET_LOCK()
possono essere rilasciati eseguendo RELEASE_LOCK()
, eseguendo un nuovo GET_LOCK()
o al termine della connessione (sia che avvenga normalmente, sia che avvenga un arresto anomalo). I lock acquisiti con GET_LOCK()
non interagiscono con le transazioni. I commit quindi non rilasciano i lock di questo tipo, anche se sono stati acquisiti durante la transazione corrente.
This function can be used to implement application locks or to simulate record
locks. Names are locked on a server-wide basis. If a name has been locked by
one client, GET_LOCK()
blocks any request by another client
for a lock with the same name. This allows clients that agree on a given lock
name to use the name to perform cooperative advisory locking. But be aware that
it also allows a client that is not among the set of cooperating clients to
lock a name, either inadvertently or deliberately, and thus prevent any of the
cooperating clients from locking that name. One way to reduce the likelihood of
this is to use lock names that are database-specific or application-specific.
For example, use lock names of the form db_name.str
or
app_name.str
.
Examples:
MariaDB [test]> SELECT GET_LOCK('lock1',10)\G *************************** 1. row *************************** GET_LOCK('lock1',10): 1 1 row in set (0.00 sec) MariaDB [test]> SELECT IS_FREE_LOCK('lock2')\G *************************** 1. row *************************** IS_FREE_LOCK('lock2'): 1 1 row in set (0.01 sec) MariaDB [test]> SELECT GET_LOCK('lock2',10)\G *************************** 1. row *************************** GET_LOCK('lock2',10): 1 1 row in set (0.00 sec) MariaDB [test]> SELECT RELEASE_LOCK('lock2')\G *************************** 1. row *************************** RELEASE_LOCK('lock2'): 1 1 row in set (0.00 sec) MariaDB [test]> SELECT RELEASE_LOCK('lock1')\G *************************** 1. row *************************** RELEASE_LOCK('lock1'): NULL 1 row in set (0.00 sec)