<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-Hans-CN">
	<id>http://wiki.eijux.com/index.php?action=history&amp;feed=atom&amp;title=MySQL_%E5%87%BD%E6%95%B0%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6%EF%BC%9A%E9%94%81%E5%87%BD%E6%95%B0</id>
	<title>MySQL 函数和运算符：锁函数 - 版本历史</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.eijux.com/index.php?action=history&amp;feed=atom&amp;title=MySQL_%E5%87%BD%E6%95%B0%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6%EF%BC%9A%E9%94%81%E5%87%BD%E6%95%B0"/>
	<link rel="alternate" type="text/html" href="http://wiki.eijux.com/index.php?title=MySQL_%E5%87%BD%E6%95%B0%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6%EF%BC%9A%E9%94%81%E5%87%BD%E6%95%B0&amp;action=history"/>
	<updated>2026-05-15T13:16:18Z</updated>
	<subtitle>本wiki上该页面的版本历史</subtitle>
	<generator>MediaWiki 1.38.2</generator>
	<entry>
		<id>http://wiki.eijux.com/index.php?title=MySQL_%E5%87%BD%E6%95%B0%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6%EF%BC%9A%E9%94%81%E5%87%BD%E6%95%B0&amp;diff=6529&amp;oldid=prev</id>
		<title>2023年3月31日 (五) 15:10 Eijux</title>
		<link rel="alternate" type="text/html" href="http://wiki.eijux.com/index.php?title=MySQL_%E5%87%BD%E6%95%B0%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6%EF%BC%9A%E9%94%81%E5%87%BD%E6%95%B0&amp;diff=6529&amp;oldid=prev"/>
		<updated>2023-03-31T15:10:29Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;zh-Hans-CN&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←上一版本&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2023年3月31日 (五) 23:10的版本&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot;&gt;第1行：&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;第1行：&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[category:&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;MySQL&lt;/del&gt;]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[category:&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;MySQL文档&lt;/ins&gt;]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Eijux</name></author>
	</entry>
	<entry>
		<id>http://wiki.eijux.com/index.php?title=MySQL_%E5%87%BD%E6%95%B0%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6%EF%BC%9A%E9%94%81%E5%87%BD%E6%95%B0&amp;diff=2233&amp;oldid=prev</id>
		<title>Eijux：​建立内容为“category:MySQL   锁函数： {| class=&quot;wikitable&quot; ! Name !! Description  |- | '''GET_LOCK()''' || 获取命名锁 “GET_LOCK(str,timeout)”：尝试使用…”的新页面</title>
		<link rel="alternate" type="text/html" href="http://wiki.eijux.com/index.php?title=MySQL_%E5%87%BD%E6%95%B0%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6%EF%BC%9A%E9%94%81%E5%87%BD%E6%95%B0&amp;diff=2233&amp;oldid=prev"/>
		<updated>2021-04-02T06:01:27Z</updated>

		<summary type="html">&lt;p&gt;建立内容为“&lt;a href=&quot;/%E5%88%86%E7%B1%BB:MySQL&quot; title=&quot;分类:MySQL&quot;&gt;category:MySQL&lt;/a&gt;   锁函数： {| class=&amp;quot;wikitable&amp;quot; ! Name !! Description  |- | &amp;#039;&amp;#039;&amp;#039;GET_LOCK()&amp;#039;&amp;#039;&amp;#039; || 获取命名锁 “GET_LOCK(str,timeout)”：尝试使用…”的新页面&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[category:MySQL]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
锁函数：&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Name !! Description &lt;br /&gt;
|-&lt;br /&gt;
| '''GET_LOCK()''' || 获取命名锁&lt;br /&gt;
“GET_LOCK(str,timeout)”：尝试使用 timeout 秒的超时时间来获得具有由字符串 str 给出的名称的锁。timeout 为负值表示无限超时。锁是排他的。当一个会话举行时，其他会话无法获得相同名称的锁：&lt;br /&gt;
: 如果成功获得了锁定，则返回'''1'''；如果尝试超时(例如，因为另一个 Client 端先前已锁定该名称)，则返回'''0'''；如果发生错误(例如内存不足或线程被mysqladmin kill杀死)，则返回'''NULL'''。&lt;br /&gt;
* 通过“RELEASE_LOCK(str)”显式地释放通过“GET_LOCK(str,timeout)”获得的锁，或者在会话终止(正常或异常)时隐式释放。&lt;br /&gt;
* 提交或回滚事务时，不会释放通过“GET_LOCK(str,timeout)”获得的锁。&lt;br /&gt;
* 此功能对于基于语句的复制不安全。如果在 binlog_format 设置为 STATEMENT 时使用此功能，则会记录一条警告。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
在 MySQL 5.7 中，使用'''元数据锁定(MDL)'''子系统重新实现了“GET_LOCK(str,timeout)”，并扩展了其功能。'''可以同时获取多个锁'''，并且“GET_LOCK(str,timeout)”不会释放任何现有锁。&lt;br /&gt;
* 给定的会话甚至有可能获得相同名称的多个锁。其他会话无法获取具有该名称的锁，直到获取会话释放该名称的所有锁。&lt;br /&gt;
重新实现 MDL 的结果是，使用GET_LOCK()获得的唯一命名的锁出现在“'''Performance Schema metadata_locks'''”表中： “'''OBJECT_TYPE'''”列表示“USER LEVEL LOCK”，而“'''OBJECT_NAME'''”列表示锁名称。&lt;br /&gt;
在为相同名称获取多个锁的情况下，仅该名称的第一个锁在“metadata_locks”表中注册一行。该名称的后续锁会在该锁中增加一个计数器，但不会获取其他元数据锁。释放名称上的最后一个锁实例后，将删除该锁的“metadata_locks”行。&lt;br /&gt;
* 获取多个锁的能力意味着 Client 端之间可能会出现死锁。发生这种情况时，服务器选择一个呼叫者，并以“'''ER_USER_LOCK_DEADLOCK'''”错误终止其锁定获取请求。此错误不会导致事务回滚。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
在 MySQL 5.7 之前，只能获取一个同时锁定，并且“RELEASE_ALL_LOCKS()”释放任何现有的锁定。通过以下示例可以看出，自 MySQL 5.7 起，锁获取行为有所不同。假设您执行以下语句：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
SELECT GET_LOCK('lock1',10);&lt;br /&gt;
SELECT GET_LOCK('lock2',10);&lt;br /&gt;
SELECT RELEASE_LOCK('lock2');&lt;br /&gt;
SELECT RELEASE_LOCK('lock1');&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
# 在 MySQL 5.7 或更高版本中，第二个“GET_LOCK()”获得第二个锁，并且两个“RELEASE_LOCK()”调用均返回 1(成功)。&lt;br /&gt;
# 在 MySQL 5.7 之前，第一个“RELEASE_LOCK()”释放第一个锁('lock1')，第二个“RELEASE_LOCK()”返回NULL(失败)，因为没有'lock1'要释放。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* 如果多个 Client 端正在 await 锁，则它们'''获取锁的顺序是不确定的'''。应用程序不应假定 Client 端将按照发出锁定请求的顺序来获得锁定。&lt;br /&gt;
|-&lt;br /&gt;
| '''IS_FREE_LOCK()''' || 命名锁是否空闲&lt;br /&gt;
“IS_FREE_LOCK(str)”：检查名为 str 的锁是否空闲可用(即未锁定)。如果锁是空闲的(没有人正在使用锁)，则返回'''1'''；如果正在使用锁，则返回'''0'''；如果发生错误(例如不正确的参数)，则返回'''NULL'''。&lt;br /&gt;
* 此功能对于基于语句的复制不安全。如果在 binlog_format 设置为 STATEMENT 时使用此功能，则会记录一条警告。&lt;br /&gt;
|-&lt;br /&gt;
| '''IS_USED_LOCK()''' || 是否使用了命名锁；如果为 true，则返回连接标识符&lt;br /&gt;
“IS_USED_LOCK(str)”：检查名为 str 的锁是否正在使用(即已锁定)。如果是这样，它将返回持有该锁的 Client 端会话的连接标识符。否则，它返回NULL。&lt;br /&gt;
* 此功能对于基于语句的复制不安全。如果在 binlog_format 设置为 STATEMENT 时使用此功能，则会记录一条警告。&lt;br /&gt;
|-&lt;br /&gt;
| '''RELEASE_ALL_LOCKS()''' || 释放所有当前命名的锁&lt;br /&gt;
“RELEASE_ALL_LOCKS()”：释放当前会话持有的所有命名锁，并返回释放的锁数(如果没有锁，则返回 0)&lt;br /&gt;
* 此功能对于基于语句的复制不安全。如果在 binlog_format 设置为 STATEMENT 时使用此功能，则会记录一条警告。&lt;br /&gt;
|-&lt;br /&gt;
| '''RELEASE_LOCK()''' || 释放命名的锁&lt;br /&gt;
“RELEASE_LOCK(str)”：释放由“GET_LOCK()”获得的字符串 str 命名的锁。如果释放了锁，则返回'''1'''；如果不是由该线程构建的锁(在这种情况下不释放锁)，则返回'''0'''；如果命名的锁不存在，则返回'''NULL'''。&lt;br /&gt;
* 如果从未通过调用“GET_LOCK()”获得该锁，或者该锁先前已被释放，则该锁不存在。&lt;br /&gt;
* DO语句 与 RELEASE_LOCK() 结合使用非常方便。【？？？】&lt;br /&gt;
* 此功能对于基于语句的复制不安全。如果在 binlog_format 设置为 STATEMENT 时使用此功能，则会记录一条警告。&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Eijux</name></author>
	</entry>
</feed>