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

  • 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 T2 for an S lock can be granted immediately. As a result, both T1 and T2 hold an S lock on r.

  • T2对S锁的请求会被马上授权.因此, T1 T2都对r持有一个共享锁
  • A request by T2 for an X lock 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): Transaction T intends to set S locks on individual rows in table t.

  • 意向共享锁(IS): 事务T打算设置S锁到表t上
  • Intention exclusive (IX): Transaction T intends to set X locks 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 S lock on a row in table t, it must first acquire an IS or stronger lock on t.

  • 在一个事务获取表t的某行的S锁之前, 他必须获取表t的一个IS锁或更强的锁
  • Before a transaction can acquire an X lock on a row, it must first acquire an IX lock on t.

  • 在一个事务获取表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.

在那时, 别的客户端的锁请求可以被授权并且他将会从表中删除行

Note

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 锁模式的更多相关文章

  1. Mysql研磨之InnoDB行锁模式

    事务并发带来的一些问题 (1)更新丢失(LostUpdate):当两个或多个事务选择同一行,然后基于最初选定的值更新该行时,由于每个事务都不知道其他事务的存在,就会发生丢失更新问题最后的更新覆盖了由其 ...

  2. MySQL AutoIncrement--自增锁模式

    自增锁模式 在MYSQL 5.1.22版本前,自增列使用AUTO_INC Locking方式来实现,即采用一种特殊的表锁机制来保证并发插入下自增操作依然是串行操作,为提高插入效率,该锁会在插入语句完成 ...

  3. mysql 开发进阶篇系列 7 锁问题(innodb锁争用情况及锁模式)

    1 .获取innodb行锁争用情况 1.1 通过检查innodb_row_lock状态变量来分析系统上的行锁的争夺情况 SHOW STATUS LIKE 'innodb_row_lock%' 通过in ...

  4. MySQL锁问题

    MySQL的锁机制比较简单,其最显著的特点是不同的存储引擎支持不同的锁机制.比如,MyISAM和MEMORY存储引擎 采用的是表级锁:BDB存储引擎采用的是页面锁,但也支持表级锁:InnoDB存储引擎 ...

  5. mysql锁

    锁是计算机协调多个进程或线程并发访问某一资源的机制.在数据库中,除传统的计算资源(如CPU.RAM.I/O等)的争用以外,数据也是一种供许多用户共享的资源.如何保证数据并发访问的一致性.有效性是所有数 ...

  6. MySQL锁系列3 MDL锁

    http://www.cnblogs.com/xpchild/p/3790139.html   MySQL为了保护数据字典元数据,使用了metadata lock,即MDL锁,保证在并发的情况下,结构 ...

  7. MySQL锁机制

    一.概况MySQL的锁机制比较简单,其最显著的特点是不同的存储引擎支持不同的锁机制.比如,MyISAM和MEMORY存储引擎采用的是表级锁(table-level locking):BDB存储引擎采用 ...

  8. Mysql锁机制介绍

    Mysql锁机制介绍 一.概况MySQL的锁机制比较简单,其最显著的特点是不同的存储引擎支持不同的锁机制.比如,MyISAM和MEMORY存储引擎采用的是表级锁(table-level locking ...

  9. MySQL锁等待分析【2】

    MySQL锁等待分析[1]中对锁等待的分析是一步一步来的.虽然最后是分析出来了,可是用时是比较长的:理清各个表之间的关系后,得到如下SQL语句,方便以后使用 select block_trx.trx_ ...

随机推荐

  1. 【Java】 大话数据结构(12) 查找算法(3) (平衡二叉树(AVL树))

    本文根据<大话数据结构>一书及网络资料,实现了Java版的平衡二叉树(AVL树). 平衡二叉树介绍 在上篇博客中所实现的二叉排序树(二叉搜索树),其查找性能取决于二叉排序树的形状,当二叉排 ...

  2. P1757 通天之分组背包

    P1757 通天之分组背包背包中的经典问题,我竟然不知道.分组背包就是每个物品有一个所属的小组,小组内的物品会冲突.就是把01背包中的两个for换一下位置01:for(i,1,kind) for(j, ...

  3. EntityManager的使用

    1.最基础的查询 CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<User> cq = cb. ...

  4. ABP单元测试

    一.介绍 在本文中,我将介绍如何为基于ASP.NET Boilerplate的项目创建单元测试. 我将使用本文开发的相同的应用程序(使用AngularJs,ASP.NET MVC,Web API和En ...

  5. Android-序列化-Serializable/Parcelable

    Android-序列化-Serializable/Parcelable 学习自 <Android开发艺术探索> 序列化漫谈 IPC的首要目的是传输数据,当然不能仅仅是传输一些基础数据了,毕 ...

  6. Oracle数据库11gR2的卸载 - deinstall

    从Oracle 11gR2开始,Oracle推荐使用deinstall来卸载Oracle数据库.使用Oracle Universal Install(OUI) 的图形方式来卸载Oracle数据库软件了 ...

  7. linux安装project lemon测评机

    (写下备用) 机子:xubuntu 16.04 LTS 1.下载lemon github地址:https://github.com/Sojiv/Project_lemon 这里download zip ...

  8. 「WC 2019」数树

    「WC 2019」数树 一道涨姿势的EGF好题,官方题解我并没有完全看懂,尝试用指数型生成函数和组合意义的角度推了一波.考场上只得了 44 分也暴露了我在数数的一些基本套路上的不足,后面的 \(\ex ...

  9. POJ.1379.Run Away(模拟退火)

    题目链接 POJ输出不能用%lf! mmp从4:30改到6:00,把4:30交的一改输出也过了. 于是就有了两份代码.. //392K 500MS //用两点构成的矩形更新,就不需要管边界了 #inc ...

  10. Shape流动效果

    <Window x:Class="MvvmLight1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/ ...