mysql get lock_MYSQL-GET_LOCK锁
示例
# CodeIgniter/system/libraries/Session/drivers/CI_Session_database_driver.php
$this->_db->query("SELECT GET_LOCK('".$arg."', 300) AS ci_session_lock")->row()->ci_session_lock);
$this->_db->query("SELECT RELEASE_LOCK('".$this->_lock."') AS ci_session_lock")->row()->ci_session_lock);
?>
簡介
GET_LOCK(str,timeout)
釋義:
Tries to obtain a lock with a name given by the string str, using a timeout of timeout seconds. A negative timeout value means infinite timeout. The lock is exclusive. While held by one session, other sessions cannot obtain a lock of the same name.
試圖分配一個(gè)名為name,超時(shí)時(shí)間為timeout秒的鎖。timeout為負(fù)數(shù)時(shí)表示永不超時(shí)。該鎖是獨(dú)立的,即當(dāng)有一個(gè)連接在使用名為str的鎖時(shí),其他的連接無法獲得該鎖。
返回值:
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).
1:獲得成功; 0:獲取超時(shí)(比如有其他客戶端已經(jīng)鎖住了該鎖); NULL:發(fā)生錯誤(比如內(nèi)存溢出或者線程被mysqladmin殺掉了)。
釋放鎖:
A lock obtained with GET_LOCK() is released explicitly by executing RELEASE_LOCK() or implicitly when your session terminates (either normally or abnormally). Lock release may also occur with another call to GET_LOCK():
執(zhí)行RELEASE_LOCK()或者你的連接中斷(正?;蚍钦G闆r)都可以直接釋放通過GET_LOCK()獲得的鎖。其他連接使用GET_LOCK()時(shí)可能也會釋放。
注意:With the capability of acquiring multiple named locks in MySQL 5.7.5, it is possible for a single statement to acquire a large number of locks. For example:
INSERT INTO ... SELECT GET_LOCK(t1.col_name) FROM t1;
These types of statements may have certain adverse effects. For example, if the statement fails part way through and rolls back, locks acquired up to the point of failure will still exist. If the intent is for there to be a correspondence between rows inserted and locks acquired, that intent will not be satisfied. Also, if it is important that locks are granted in a certain order, be aware that result set order may differ depending on which execution plan the optimizer chooses. For these reasons, it may be best to limit applications to a single lock-acquisition call per statement.
伴隨著MySQL 5.7.5中的獲取多重命令鎖的能力,可能會使單個(gè)語句獲取很多個(gè)鎖,比如:
INSERT INTO ... SELECT GETLOCK(t1.colname) FROM t1;
這列類型的語句可能潛藏不利的因素。比如,如果這個(gè)語句執(zhí)行失敗發(fā)生回滾,雖然語句失敗但是鎖依然存在。如果你本來的意圖是行插入成功和鎖獲取成功,這個(gè)時(shí)候就不滿足不了。比如如果使用鎖來控制一定的命令順序,請注意根據(jù)優(yōu)化器選擇的執(zhí)行計(jì)劃可能會使結(jié)果集有所不同。由于這些原因,最好將應(yīng)用限制為獲取每個(gè)語句的單個(gè)鎖.
鎖
總結(jié)
以上是生活随笔為你收集整理的mysql get lock_MYSQL-GET_LOCK锁的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql1045_win10系统登录m
- 下一篇: linux mysql 5.7 配置_l