文档章节

InnoDB Lock Modes

秋风醉了
 秋风醉了
发布于 2015/11/13 19:39
字数 909
阅读 142
收藏 0

3 月,跳不动了?>>>

 InnoDB Lock Modes

http://dev.mysql.com/doc/refman/5.7/en/innodb-lock-modes.html

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.2.2.4, “InnoDB Record, Gap, and Next-Key Locks”.

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:

  • 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.

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.


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 locks are 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):

Intention shared (IS): Transaction T intends to set S locks on individual rows in table t.

Intention exclusive (IX): Transaction T intends to set X locks on those rows.

For example, SELECT ... LOCK IN SHARE MODE sets an IS lock and SELECT ... FOR UPDATE sets an IX lock.


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.

Before a transaction can acquire an X lock on a row, it must first acquire an IX lock on t.

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.


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.

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:

CREATE TABLE t (i INT) ENGINE = InnoDB;
INSERT INTO t (i) VALUES(1);

client A,

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.00 sec)

Next, client B begins a transaction and attempts to delete the row from the table:

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.

Finally, client A also attempts to delete the row from the table:

mysql> DELETE FROM t WHERE i = 1;
Query OK, 1 row affected (0.00 sec)

while client B,

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:

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.

==============END==============

© 著作权归作者所有

秋风醉了
粉丝 253
博文 532
码字总数 405557
作品 0
朝阳
程序员
私信 提问
加载中

评论(0)

InnoDB Record, Gap, and Next-Key Locks

InnoDB Record, Gap, and Next-Key Locks http://dev.mysql.com/doc/refman/5.7/en/innodb-record-level-locks.html InnoDB has several types of record-level locks including record lock......

秋风醉了
2015/11/13
103
0
MySQL 5.6.17/5.5.37 发布

MySQL产品线更新.5.6.17/5.5.37 2014-03-27 之前版本2013-01-31的5.6.16/5.5.36主要是Bug修正。5.1还是5.1.73。 完全改进: MySQL 5.6.17 改进记录 (2014-03-27) 新特性和各种改进 Incompat...

fei
2014/03/28
5.6K
6
mysql参数Innodb_row_lock_current_waits异常

数据库巡检时遇到Innodb_row_lock_current_waits参数值异常情况,如下: mysql> show status like "Innodb_row_lock%"; +-------------------------------+----------------------+ | Varia......

fofo成长记
2017/02/28
1.8K
0
MySQL 5.5 InnoDB 锁状态解析

目录 目录 一 引子 二 表结构说明 三 INNODB 锁等待模拟 3.1 创建测试表,录入测试数据 3.2 模拟锁等待 3.3 再次模拟锁等待 3.4 查询锁等待 3.4.1 直接查看 innodblockwaits 表 3.4.2 innodb...

java_龙
2018/11/30
51
0
mysql 启动错误日志,导致网站缓慢

InnoDB: The InnoDB memory heap is disabled InnoDB: The InnoDB memory heap is disabled InnoDB: Mutexes and rw_locks use GCC atomic builtins InnoDB: Mutexes and rw_locks use GCC a......

rianbow
2018/09/21
248
1

没有更多内容

加载失败,请刷新页面

加载更多

华为P40发布:搭载HMS硬刚谷歌,未涨价抢全球高端机市场

  文连线 Insight,作者向阳,编辑水笙   3 月 26 日晚,华为消费者业务 CEO 余承东登上台,以熟悉的英文口音开启了华为发布会,他说,“这就是我们的 P40 系列。”   以往华为P系列通...

水果黄瓜
12分钟前
14
0
如何从Java中的字符串值获取枚举值? - How to get an enum value from a string value in Java?

问题: Say I have an enum which is just 说我有一个枚举 public enum Blah { A, B, C, D} and I would like to find the enum value of a string, for example "A" which would be B......

javail
35分钟前
7
0
2048游戏的最佳算法是什么? - What is the optimal algorithm for the game 2048?

问题: I have recently stumbled upon the game 2048 . 我最近偶然发现了2048游戏。 You merge similar tiles by moving them in any of the four directions to make "bigger" tiles. 您可......

技术盛宴
今天
7
0
OSChina 周一乱弹 —— 小姐姐,这tm不是犬耳娘吗!你认错了吧

Osc乱弹歌单(2020)请戳(这里) 【今日歌曲】 @薛定谔的兄弟 :分享洛神有语创建的歌单「我喜欢的音乐」: 《Drip Drip Drip》- 音乐治疗 手机党少年们想听歌,请使劲儿戳(这里) @-Eric- ...

小小编辑
今天
34
0
HTML5 小游戏开发

HTML的基础 HTML称为超文本标记语言,是一种标识性的语言。它包括一系列标签.通过这些标签可以将网络上的文档格式统一,使分散的Internet资源连接为一个逻辑整体。HTML文本是由HTML命令组成...

冯六六
今天
17
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部