Shared locks in MySQL

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.