您好,登录后才能下订单哦!
这篇文章主要讲解了“MySQL共享锁和排他锁的介绍”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“MySQL共享锁和排他锁的介绍”吧!
InnoDB
implements standard row-level locking where there are two types of locks, shared (S
) locks and exclusive (X
) locks.
InnoDB实现了两种标准的 行级锁 ,分别是 共享锁(S锁) 和 排他锁(X锁):
A shared (S
) lock permits the transaction that holds the lock to read a row. 共享锁(S锁)允许(当前)持有这个锁的事务去读这个行。
An exclusive (X
) lock permits the transaction that holds the lock to update or delete a row. 排他锁(X锁)允许(当前)持有这个锁的事务去更新或删除这个行。
If transaction T1
holds a shared (S
) lock on row r
, then requests from some distinct transaction T2
for a lock on row r
are handled as follows:
A request by T2
for an S
lock can be granted immediately. As a result, both T1
and T2
hold an S
lock on r
.
A request by T2
for an X
lock cannot be granted immediately.
如果事务T1带有一个在r行记录上的共享锁,那么另一个事务T2在请求对r行的锁时,会被处理如下:
T2请求共享锁将会被立刻放行。这样,T1和T2事务都有r行的共享锁。
T2请求排他锁,将不会被立即放行。
If a transaction T1
holds an exclusive (X
) lock on row r
, a request from some distinct transaction T2
for a lock of either type on r
cannot be granted immediately. Instead, transaction T2
has to wait for transaction T1
to release its lock on row r
.
如果事务T1在r行上带有排他锁,那么另一个事务T2不管请求对r行的哪种锁,都不会被立即放行。相反,事务T2必须等待事务T1释放在r行上的锁。
排他锁和共享锁的兼容性如下
X | S | |
---|---|---|
X | Conflict | Conflict |
S | Conflict | Compatible |
InnoDB
supports multiple granularity locking which permits coexistence of row locks and table locks. For example, a statement such asLOCK TABLES ... WRITE
takes an exclusive lock (an X
lock) on the specified table. To make locking at multiple granularity levels practical, InnoDB
uses intention locks. Intention locks are table-level locks that indicate which type of lock (shared or exclusive) a transaction requires later for a row in a table. There are two types of intention locks:
An intention shared lock (IS
) indicates that a transaction intends to set a shared lock on individual rows in a table.
An intention exclusive lock (IX
) indicates that a transaction intends to set an exclusive lock on individual rows in a table.
InnoDB 支持 多粒度锁,它允许 行锁 和 表锁的同时存在。例如,‘LOCK TABLES ... WRITE’ 声明语句 给明确的表加上一个排他锁(X锁)。为了锁在多粒度级别的实践,InnoDB 使用 意向锁。意向锁是表级锁,它表明一个事务在(申请表级锁之后)接下来对表中的某一行请求何种类型的锁(共享还是排他)。意向锁有两种:
意向共享锁(IS锁) 表明一个事务打算(在获得表锁之后)对表中的个别行设置一个共享锁(S锁)。
意向排他锁(IX锁) 表明一个事务打算(在获得表锁之后)对表中的个别行设置一个排他锁(X锁)。
For example, SELECT ... FOR SHARE
sets an IS
lock, and SELECT ... FOR UPDATE
sets an IX
lock.
例如,SELECT ... FOR SHARE 会设置一个 意向共享锁(IS锁),SELECT ... FOR UPDATE 会设置一个意向排他锁。
The intention locking protocol is as follows:
Before a transaction can acquire a shared lock on a row in a table, it must first acquire an IS
lock or stronger on the table.
Before a transaction can acquire an exclusive lock on a row in a table, it must first acquire an IX
lock on the table.
意向锁的协议如下:
在一个事务获取表中某行的共享锁之前,它必须首先获取这个表的一个意向共享锁或者一个更强的锁。
在一个事务获取表中某行的排他锁之前,它必须首先获取这个表的一个意向排他锁。
Table-level lock type compatibility is summarized in the following matrix.
表级锁的兼容性总结如下:
X | IX | S | IS | |
---|---|---|---|---|
X | Conflict | Conflict | Conflict | Conflict |
IX | Conflict | Compatible | Conflict | Compatible |
S | Conflict | Conflict | Compatible | Compatible |
IS | Conflict | Compatible | Compatible | Compatible |
A lock is granted to a requesting transaction if it is compatible with existing locks, but not if it conflicts with existing locks. A transaction waits until the conflicting existing lock is released. If a lock request conflicts with an existing lock and cannot be granted because it would cause deadlock, an error occurs.
如果某个事务请求锁,而这个锁与已存在的锁是兼容的,那么这个锁将会被放行。但是如果这个锁与已有的锁冲突,则不会被放行。事务将会等待,直到那个已存在的引起冲突的锁被释放。如果一个锁请求和一个已存在的锁发生冲突,且不会被放行,因为如果放行,会造成死锁,会发生错误。
Intention locks do not block anything except full table requests (for example, LOCK TABLES ... WRITE
). The main purpose of intention locks is to show that someone is locking a row, or going to lock a row in the table.
意向锁不会阻塞任何东西,除了全表扫描(比如 LOCK TABLES ... WRITE)。意向锁的主要目的是为了展示有人正在锁住表中的某一行,或者将要锁住那一行。
Transaction data for an intention lock appears similar to the following in SHOW ENGINE INNODB STATUS
and InnoDB monitor output:
TABLE LOCK table `test`.`t` trx id 10080 lock mode IX
感谢各位的阅读,以上就是“MySQL共享锁和排他锁的介绍”的内容了,经过本文的学习后,相信大家对MySQL共享锁和排他锁的介绍这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。