<?php
# 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)php
釋義
: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.html
試圖分配一個名爲name,超時時間爲timeout秒的鎖。timeout爲負數時表示永不超時。該鎖是獨立的,即當有一個鏈接在使用名爲str的鎖時,其餘的鏈接沒法得到該鎖。mysql
返回值
: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).sql
1:得到成功; 0:獲取超時(好比有其餘客戶端已經鎖住了該鎖); NULL:發生錯誤(好比內存溢出或者線程被mysqladmin殺掉了)。bash
釋放鎖
: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():session
執行RELEASE_LOCK()或者你的鏈接中斷(正常或非正常狀況)均可以直接釋放經過GET_LOCK()得到的鎖。其餘鏈接使用GET_LOCK()時可能也會釋放。app
注意: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.ui
伴隨着MySQL 5.7.5中的獲取多重命令鎖的能力,可能會使單個語句獲取不少個鎖,好比:this
INSERT INTO ... SELECT GETLOCK(t1.colname) FROM t1;
複製代碼
這列類型的語句可能潛藏不利的因素。好比,若是這個語句執行失敗發生回滾,雖然語句失敗可是鎖依然存在。若是你原本的意圖是行插入成功和鎖獲取成功,這個時候就不知足不了。好比若是使用鎖來控制必定的命令順序,請注意根據優化器選擇的執行計劃可能會使結果集有所不一樣。因爲這些緣由,最好將應用限制爲獲取每一個語句的單個鎖。