MySQL provides an application-level, shared lock that allows applications on top of the database to implement mutually exclusive functions. This shared lock is implemented through a set of MySQL built-in functions.
GET_LOCK(str
,timeout
) 这个函数的作用是获取共享锁,其中str是应用程序通信双方协商好的一个字符串,timeout是等待该锁的超时时间。如果能在timeout时间内获取到锁,则返回1;如果在timeout时间后仍然获取不到锁,则返回0;如果发生错误,则返回NULL。
once an application acquires a lock, it can RELEASE_LOCK(str
)
be、执行新的GET_LOCK(str,timeout)、或者mysql连接被释放时(不管是正常释放还是异常断开)这三种方式释放锁。
If more than one client wants to get a lock through Get_lock (str, timeout) at the same time, the order in which they acquire the lock is unknown.
RELEASE_LOCK(str
)
这个函数的作用是释放名为str的共享锁。如果锁被成功释放,返回1;如果这个进程没有占有该锁,则返回0;如果这个名为str的锁不存在,则返回NULL。
There are also two functions that don't feel very useful:
IS_USED_LOCK(str
)
Checks whether a lock named Str is in use (locked), returns the client identity that owns the lock if it is locked, or returns NULL.
Is_free_lock (str)
Checks whether a lock named Str is available (not locked), returns 1 if it is available, returns 0 if it is used, or returns NULL if there is an error.
Reference: http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html