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.
試圖分配一個名為name,超時時間為timeout秒的鎖。timeout為負數時表示永不超時。該鎖是獨立的,即當有一個連接在使用名為str的鎖時,其他的連接無法獲得該鎖。
返回值:
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:獲取超時(比如有其他客戶端已經鎖住了該鎖); NULL:發生錯誤(比如內存溢出或者線程被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():
執行RELEASE_LOCK()或者你的連接中斷(正常或非正常情況)都可以直接釋放通過GET_LOCK()獲得的鎖。其他連接使用GET_LOCK()時可能也會釋放。
注意: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中的獲取多重命令鎖的能力,可能會使單個語句獲取很多個鎖,比如:
INSERT INTO ... SELECT GETLOCK(t1.colname) FROM t1;
這列類型的語句可能潛藏不利的因素。比如,如果這個語句執行失敗發生回滾,雖然語句失敗但是鎖依然存在。如果你本來的意圖是行插入成功和鎖獲取成功,這個時候就不滿足不了。比如如果使用鎖來控制一定的命令順序,請注意根據優化器選擇的執行計劃可能會使結果集有所不同。由于這些原因,最好將應用限制為獲取每個語句的單個鎖.
鎖
總結
以上是生活随笔為你收集整理的mysql get lock_MYSQL-GET_LOCK锁的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql1045_win10系统登录m
- 下一篇: linux mysql 5.7 配置_l