MySQL 锁模式
InnoDB implements standard row-level locking where there are two types of locks, shared (S) locks and exclusive (X) locks. For information about record, gap, and next-key lock types, see Section 14.3.5.6, “InnoDB Record, Gap, and Next-Key Locks”.
InnoDB 实现了标准行级锁, 他又两种锁, 共享(S)锁和排他(X)锁. 需要看record, gap, next-key锁类型, 参照 xxx
A shared (
S) lock permits the transaction that holds the lock to read a row.- 一个共享锁允许事务获取锁来读取一行
An exclusive (
X) lock permits the transaction that holds the lock to update or delete a row.- 一个排他锁允许事务获取锁来更新或删除一行
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:
如果事务T1持有对行 r 的 S 锁, 那么另外一个事务T2对行 r 的锁需求会如下处理
A request by
T2for anSlock can be granted immediately. As a result, bothT1andT2hold anSlock onr.- T2对S锁的请求会被马上授权.因此, T1 T2都对r持有一个共享锁
A request by
T2for anXlock cannot be granted immediately.- T2对X锁的请求不会被马上授权
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的X锁, 那么T2对r的任何锁类型都无法被马上授权. 替代的是T2必须等待T1释放他在r上的锁
Intention Locks
意向锁
Additionally, InnoDB supports multiple granularity locking which permits coexistence of record locks and locks on entire tables. To make locking at multiple granularity levels practical, additional types of locks called intention locksare used. Intention locks are table locks in InnoDB that indicate which type of lock (shared or exclusive) a transaction will require later for a row in that table. There are two types of intention locks used in InnoDB (assume that transaction T has requested a lock of the indicated type on table t):
另外, InnoDB支持多重粒度加锁, 这允许行锁和表所共存. 为了让多重粒度锁定具有实用性, 另外一种叫做意向锁的锁会被使用. 意向锁在InnoDB中是表锁, 他表明S或X锁将会在一个事务中对某一行使用. InnoDB有两种意向锁(假设事务T已经请求了表t的一个锁)
Intention shared (
IS): TransactionTintends to setSlocks on individual rows in tablet.- 意向共享锁(IS): 事务T打算设置S锁到表t上
Intention exclusive (
IX): TransactionTintends to setXlocks on those rows.- 意向排他锁(IX): 事务T打算设置X锁到行上
For example, SELECT ... LOCK IN SHARE MODE sets an IS lock and SELECT ... FOR UPDATE sets an IXlock.
例如, SELECT ... LOCK IN SHARE_MODE 设置一个 IS 锁, 而 SELECT ... FOR UPDATE 设置一个 IX 锁
The intention locking protocol is as follows:
意向锁协议如下
Before a transaction can acquire an
Slock on a row in tablet, it must first acquire anISor stronger lock ont.- 在一个事务获取表t的某行的S锁之前, 他必须获取表t的一个IS锁或更强的锁
Before a transaction can acquire an
Xlock on a row, it must first acquire anIXlock ont.- 在一个事务获取表t某行的X锁之前, 他必须获取一个t的IX锁
These rules can be conveniently summarized by means of the following lock type compatibility 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.
一个锁如果和已经存在的锁兼容, 就可以授权给请求他的事务, 但如果和已存在的锁不兼容则不行.一个事务必须等待直到冲突的锁被释放.如果一个锁请求和一个已经存在的锁冲突, 并且一直不能被授权, 就会造成死锁.
Thus, intention locks do not block anything except full table requests (for example, LOCK TABLES ... WRITE). The main purpose of IX and IS locks is to show that someone is locking a row, or going to lock a row in the table.
因此, 意向锁并不会阻塞任何事情, 除非是对全表的请求(例如, LOCK TABLES ... WRITE). IX和IS锁的主要目的是表示有人正在锁定一行, 或者准备锁定一行.
Deadlock Example
死锁示例
The following example illustrates how an error can occur when a lock request would cause a deadlock. The example involves two clients, A and B.
下面的实例举例说明了当一个锁请求造成死锁时, 发生错误的情形. 示例包括两个客户端 A, B
First, client A creates a table containing one row, and then begins a transaction. Within the transaction, A obtains an S lock on the row by selecting it in share mode:
首先客户端A创造一个包含一行的表, 并且开始一个事务.在事务内, A通过select in share mode获得了一个S锁
mysql>CREATE TABLE t (i INT) ENGINE = InnoDB;
Query OK, 0 rows affected (1.07 sec) mysql>INSERT INTO t (i) VALUES(1);
Query OK, 1 row affected (0.09 sec) mysql>START TRANSACTION;
Query OK, 0 rows affected (0.00 sec) mysql>SELECT * FROM t WHERE i = 1 LOCK IN SHARE MODE;
+------+
| i |
+------+
| 1 |
+------+
1 row in set (0.10 sec)
Next, client B begins a transaction and attempts to delete the row from the table:
接下来B开始一个事务, 并试图删除那行
mysql>START TRANSACTION;
Query OK, 0 rows affected (0.00 sec) mysql>DELETE FROM t WHERE i = 1;
The delete operation requires an X lock. The lock cannot be granted because it is incompatible with the S lock that client A holds, so the request goes on the queue of lock requests for the row and client B blocks.
删除操作会获取一个X锁, 但是锁并不能被授权, 因为他和A持有的S锁不兼容, 所以请求会继续并且B被阻塞
Finally, client A also attempts to delete the row from the table:
最后, A也尝试从表中删除这行
mysql>DELETE FROM t WHERE i = 1;
ERROR 1213 (40001): Deadlock found when trying to get lock;
try restarting transaction
Deadlock occurs here because client A needs an X lock to delete the row. However, that lock request cannot be granted because client B already has a request for an X lock and is waiting for client A to release its S lock. Nor can the S lock held by A be upgraded to an X lock because of the prior request by B for an X lock. As a result,InnoDB generates an error for one of the clients and releases its locks. The client returns this error:
死锁就在这里发生了, 因为A需要X锁来删除这行.然而, 它无法获取X锁, 因为B已经请求了一个X锁, 并且正在等待A释放他的S锁. 同样A的S锁也不能升级为X锁, 因为B的前一个请求请求了一个X锁.因此, InnoDB为其中一个客户端生成了一个错误并释放了他的锁. 错误如下
ERROR 1213 (40001): Deadlock found when trying to get lock;
try restarting transaction
At that point, the lock request for the other client can be granted and it deletes the row from the table.
在那时, 别的客户端的锁请求可以被授权并且他将会从表中删除行
If the LATEST DETECTED DEADLOCK section of InnoDB Monitor output includes a message stating, “TOO DEEP OR LONG SEARCH IN THE LOCK TABLE WAITS-FOR GRAPH, WE WILL ROLL BACK FOLLOWING TRANSACTION,”this indicates that the number of transactions on the wait-for list has reached a limit of 200, which is defined byLOCK_MAX_DEPTH_IN_DEADLOCK_CHECK. A wait-for list that exceeds 200 transactions is treated as a deadlock and the transaction attempting to check the wait-for list is rolled back.
如果 LATEST DETECTED DEADLOCK 监视器输出包括这样一个消息 "TOO DEEP OR LONG SEARCH IN THE LOCK TABLE WAITS-FOR GRAPH, WE WILL ROLL BACK FOLLOWING TRANSACION" 这说明等待列表中的事务数量超过了200个, 这是有LOCK_MAX_DEPTH_IN_DEADLOCK_CHECK定义的. 一个等待列表达到200个事务, 会被认为是一次死锁, 并且尝试检查等待列表的事务会回滚
The same error may also occur if the locking thread must look at more than 1,000,000 locks owned by the transactions on the wait-for list. The limit of 1,000,000 locks is defined byLOCK_MAX_N_STEPS_IN_DEADLOCK_CHECK.
MySQL 锁模式的更多相关文章
- Mysql研磨之InnoDB行锁模式
事务并发带来的一些问题 (1)更新丢失(LostUpdate):当两个或多个事务选择同一行,然后基于最初选定的值更新该行时,由于每个事务都不知道其他事务的存在,就会发生丢失更新问题最后的更新覆盖了由其 ...
- MySQL AutoIncrement--自增锁模式
自增锁模式 在MYSQL 5.1.22版本前,自增列使用AUTO_INC Locking方式来实现,即采用一种特殊的表锁机制来保证并发插入下自增操作依然是串行操作,为提高插入效率,该锁会在插入语句完成 ...
- mysql 开发进阶篇系列 7 锁问题(innodb锁争用情况及锁模式)
1 .获取innodb行锁争用情况 1.1 通过检查innodb_row_lock状态变量来分析系统上的行锁的争夺情况 SHOW STATUS LIKE 'innodb_row_lock%' 通过in ...
- MySQL锁问题
MySQL的锁机制比较简单,其最显著的特点是不同的存储引擎支持不同的锁机制.比如,MyISAM和MEMORY存储引擎 采用的是表级锁:BDB存储引擎采用的是页面锁,但也支持表级锁:InnoDB存储引擎 ...
- mysql锁
锁是计算机协调多个进程或线程并发访问某一资源的机制.在数据库中,除传统的计算资源(如CPU.RAM.I/O等)的争用以外,数据也是一种供许多用户共享的资源.如何保证数据并发访问的一致性.有效性是所有数 ...
- MySQL锁系列3 MDL锁
http://www.cnblogs.com/xpchild/p/3790139.html MySQL为了保护数据字典元数据,使用了metadata lock,即MDL锁,保证在并发的情况下,结构 ...
- MySQL锁机制
一.概况MySQL的锁机制比较简单,其最显著的特点是不同的存储引擎支持不同的锁机制.比如,MyISAM和MEMORY存储引擎采用的是表级锁(table-level locking):BDB存储引擎采用 ...
- Mysql锁机制介绍
Mysql锁机制介绍 一.概况MySQL的锁机制比较简单,其最显著的特点是不同的存储引擎支持不同的锁机制.比如,MyISAM和MEMORY存储引擎采用的是表级锁(table-level locking ...
- MySQL锁等待分析【2】
MySQL锁等待分析[1]中对锁等待的分析是一步一步来的.虽然最后是分析出来了,可是用时是比较长的:理清各个表之间的关系后,得到如下SQL语句,方便以后使用 select block_trx.trx_ ...
随机推荐
- 【Java】 大话数据结构(5) 线性表之双向链表
本文根据<大话数据结构>一书,实现了Java版的双向链表. 在每个数据结点中都有两个指针,分别指向直接后继和直接前驱,这样的链表称为双向链表. 双向链表的结构如图所示: 查找元素可以根据元 ...
- 025 Spark中的广播变量原理以及测试(共享变量是spark中第二个抽象)
一:来源 1.说明 为啥要有这个广播变量呢. 一些常亮在Driver中定义,然后Task在Executor上执行. 如果,有多个任务在执行,每个任务需要,就会造成浪费. 二:共享变量的官网 1.官网 ...
- [C编码笔记] 空串与NULL是不一样的
int main() { char *str = NULL; printf("%p \n", str); printf("%p \n", &str); ...
- 021.Zabbix的邮件告警-01
一 创建Media Administration---->Media types---->Create Media Type 选项 描述 Name 媒介名称,看着起名 Type 选择 ...
- Python异常处理回顾与总结
1 引言 在我们调试程序时,经常不可避免地出现意料之外的情况,导致程序不得不停止运行,然后提示大堆提示信息,大多是这种情况都是由异常引起的.异常的出现一方面是因为写代码时粗心导致的语法错误,这种错误在 ...
- 【转】Mapped Statements collection does not contain value for解决
最近一直在弄springMVC+mybatis的整合,因为接触到这个框架之后发现这个框架确实要比ssh好得多所以我自己也在配置这个框架.但是在配置的过程中我遇到了一些问题,这些问题当我配置完成之后访问 ...
- spring boot 集成 shiro
写在前面 1.Shiro是Apache下的一个开源项目,我们称之为Apache Shiro.它是一个很易用与Java项目的的安全框架,提供了认证.授权.加密.会话管理,与spring Security ...
- 从客户端浏览器直传文件到Storage
关于上传文件到Azure Storage没有什么可讲的,不论我们使用哪种平台.语言,上传流程都如下图所示: 从上图我们可以了解到从客户端上传文件到Storage,是需要先将文件上传到应用服务上,然后再 ...
- asp.net core中的razor页面
Razor 页面(Razor Pages)是 ASP.NET Core 2.0 中新增的一种Web页面模型,相对MVC形式更加简单易用,可以说是一个服务端的MVVM模型,本文简单的介绍一下它的用法. ...
- OpAmp Voltage Follower/Regulator
LDO Regulator High accuracy voltage regulator Vout = 2.5V * (1 + ( 5.6 / 6.8 ) ) = 4.55V Recently th ...