This section describes functions used to manipulate user-level locks.本节介绍用于操作用户级锁的函数。
Table 12.19 Locking Functions锁定函数
GET_LOCK() | |
IS_FREE_LOCK() | |
IS_USED_LOCK() | true ) |
RELEASE_ALL_LOCKS() | |
RELEASE_LOCK() |
Tries to obtain a lock with a name given by the string 尝试使用字符串str
, using a timeout of timeout
seconds. str
指定的名称获取锁,超时时间为timeout
。A negative 负timeout
value means infinite timeout. timeout
值表示无限超时。The lock is exclusive. 锁是独享的。While held by one session, other sessions cannot obtain a lock of the same name.当由一个会话持有时,其他会话无法获得相同名称的锁。
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 kill终止)。
A lock obtained with 使用GET_LOCK()
is released explicitly by executing RELEASE_LOCK()
or implicitly when your session terminates (either normally or abnormally). GET_lock()
获得的锁通过执行RELEASE_lock()
显式释放,或者在会话终止(正常或异常)时隐式释放。Locks obtained with 当事务提交或回滚时,不会释放使用GET_LOCK()
are not released when transactions commit or roll back.GET_LOCK()
获得的锁。
GET_LOCK()
is implemented using the metadata locking (MDL) subsystem. GET_LOCK()
是使用元数据锁定(MDL)子系统实现的。Multiple simultaneous locks can be acquired and 可以同时获取多个锁,GET_LOCK()
does not release any existing locks. GET_LOCK()
不会释放任何现有锁。For example, suppose that you execute these statements:例如,假设您执行以下语句:
SELECT GET_LOCK('lock1',10); SELECT GET_LOCK('lock2',10); SELECT RELEASE_LOCK('lock2'); SELECT RELEASE_LOCK('lock1');
The second 第二个GET_LOCK()
acquires a second lock and both RELEASE_LOCK()
calls return 1 (success).GET_LOCK()
获取第二个锁,两个RELEASE_LOCK()
调用都返回1(成功)。
It is even possible for a given session to acquire multiple locks for the same name. 一个给定的会话甚至可以为同一个名称获取多个锁。Other sessions cannot acquire a lock with that name until the acquiring session releases all its locks for the name.在获取会话释放该名称的所有锁之前,其他会话无法获取具有该名称的锁。
Uniquely named locks acquired with 使用GET_LOCK()
appear in the Performance Schema metadata_locks
table. GET_LOCK()
获取的唯一命名锁将显示在性能架构metadata_locks
表中。The OBJECT_TYPE
column says USER LEVEL LOCK
and the OBJECT_NAME
column indicates the lock name. OBJECT_TYPE
列表示用户级锁,OBJECT_NAME
列表示锁名。In the case that multiple locks are acquired for the same name, only the first lock for the name registers a row in the 在为同一个名称获取多个锁的情况下,只有该名称的第一个锁在metadata_locks
table. metadata_locks
表中注册一行。Subsequent locks for the name increment a counter in the lock but do not acquire additional metadata locks. 名称的后续锁会在锁中增加一个计数器,但不会获取额外的元数据锁。The 释放名称上的最后一个锁实例时,将删除该锁的metadata_locks
row for the lock is deleted when the last lock instance on the name is released.metadata_locks
行。
The capability of acquiring multiple locks means there is the possibility of deadlock among clients. 获取多个锁的能力意味着客户端之间存在死锁的可能性。When this happens, the server chooses a caller and terminates its lock-acquisition request with an 当这种情况发生时,服务器选择一个调用者并终止其锁获取请求,并出现ER_USER_LOCK_DEADLOCK
error. ER_USER_LOCK_DEADLOCK
错误。This error does not cause transactions to roll back.此错误不会导致事务回滚。
MySQL enforces a maximum length on lock names of 64 characters.MySQL对锁名强制规定了64个字符的最大长度。
GET_LOCK()
can be used to implement application locks or to simulate record locks. GET_LOCK()
可用于实现应用程序锁或模拟记录锁。Names are locked on a server-wide basis. 在服务器范围内锁定名称。If a name has been locked within one session, 如果一个名称在一个会话中被锁定,GET_LOCK()
blocks any request by another session for a lock with the same name. GET_LOCK()
将阻止另一个会话对同名锁的任何请求。This enables clients that agree on a given lock name to use the name to perform cooperative advisory locking. 这使同意给定锁名称的客户机能够使用该名称执行协作建议锁定。But be aware that it also enables a client that is not among the set of cooperating clients to lock a name, either inadvertently or deliberately, and thus prevent any of the cooperating clients from locking that name. 但是请注意,它还使不在协作客户机集合中的客户机能够无意地或有意地锁定名称,从而防止任何协作客户机锁定该名称。One way to reduce the likelihood of this is to use lock names that are database-specific or application-specific. 降低这种可能性的一种方法是使用特定于数据库或特定于应用程序的锁名。For example, use lock names of the form 例如,使用db_name.str
or app_name.str
.db_name.str
或app_name.str
形式的锁名。
If multiple clients are waiting for a lock, the order in which they acquire it is undefined. 如果多个客户端正在等待一个锁,那么它们获取锁的顺序是未定义的。Applications should not assume that clients acquire the lock in the same order that they issued the lock requests.应用程序不应假定客户端获取锁的顺序与它们发出锁请求的顺序相同。
GET_LOCK()
is unsafe for statement-based replication. GET_LOCK()
对于基于语句的复制不安全。A warning is logged if you use this function when 如果在binlog_format设置为binlog_format
is set to STATEMENT
.STATEMENT
时使用此函数,则会记录警告。
With the capability of acquiring multiple named locks, 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 still exist. 例如,如果语句部分失败并回滚,则在失败点之前获取的锁仍然存在。If the intent is for there to be a correspondence between rows inserted and locks acquired, that intent is not 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.出于这些原因,最好将应用程序限制为每个语句一个锁获取调用。
A different locking interface is available as either a plugin service or a set of loadable functions. 不同的锁定接口可以作为插件服务或一组可加载函数使用。This interface provides lock namespaces and distinct read and write locks, unlike the interface provided by 此接口提供锁名称空间和不同的读写锁,这与GET_LOCK()
and related functions. GET_lock()
和相关函数提供的接口不同。For details, see Section 5.6.9.1, “The Locking Service”.有关详细信息,请参阅第5.6.9.1节,“锁定服务”。
Checks whether the lock named 检查名为str
is free to use (that is, not locked). str
的锁是否可以自由使用(即未锁定)。Returns 如果锁是空闲的(没有人使用该锁),则返回1
if the lock is free (no one is using the lock), 0
if the lock is in use, and NULL
if an error occurs (such as an incorrect argument).1
;如果锁正在使用,则返回0
;如果发生错误(如参数不正确),则返回NULL
。
This function is unsafe for statement-based replication. 此函数对于基于语句的复制不安全。A warning is logged if you use this function when 如果在binlog_format设置为binlog_format
is set to STATEMENT
.STATEMENT
时使用此函数,则会记录警告。
Checks whether the lock named 检查名为str
is in use (that is, locked). str
的锁是否正在使用(即已锁定)。If so, it returns the connection identifier of the client session that holds the lock. Otherwise, it returns 如果是这样,它将返回持有锁的客户端会话的连接标识符。否则,返回NULL
.NULL
。
This function is unsafe for statement-based replication. 此函数对于基于语句的复制不安全。A warning is logged if you use this function when 如果在binlog_format
is set to STATEMENT
.binlog_format
设置为STATEMENT
时使用此函数,则会记录警告。
Releases all named locks held by the current session and returns the number of locks released (0 if there were none)释放当前会话持有的所有命名锁,并返回释放的锁数(如果没有,则返回0)
This function is unsafe for statement-based replication. 此函数对于基于语句的复制不安全。A warning is logged if you use this function when 如果在binlog_format
is set to STATEMENT
.binlog_format
设置为STATEMENT
时使用此函数,则会记录警告。
Releases the lock named by the string 释放用str
that was obtained with GET_LOCK()
. GET_lock()
获得的字符串str
命名的锁。Returns 如果锁已释放,则返回1
if the lock was released, 0
if the lock was not established by this thread (in which case the lock is not released), and NULL
if the named lock did not exist. 1
;如果该线程未建立锁(在这种情况下,锁未释放),则返回0
;如果指定的锁不存在,则返回NULL
。The lock does not exist if it was never obtained by a call to 如果从未通过调用GET_LOCK()
or if it has previously been released.GET_lock()
获取过锁,或者以前已释放过锁,则该锁不存在。
The DO
statement is convenient to use with RELEASE_LOCK()
. DO
语句与RELEASE_LOCK()
一起使用很方便。See Section 13.2.3, “DO Statement”.请参阅第13.2.3节,“DO语句”。
This function is unsafe for statement-based replication. 此函数对于基于语句的复制不安全。A warning is logged if you use this function when 如果在binlog_format
is set to STATEMENT
.binlog_format
设置为STATEMENT
时使用此函数,则会记录警告。