Inno db隔离级别和锁定 [英] inno db isolation levels and locking

查看:220
本文介绍了Inno db隔离级别和锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关innodb交易的手册,但仍然有很多不清楚的东西.例如,我不太了解以下行为:

I am reading a manual about innodb transactions but still, there is lots of unclear stuff to me. For instance, I don't quite understand to the following behaviour:

-- client 1                             -- client 2
mysql> create table simple (col int) 
       engine=innodb; 

mysql> insert into simple values(1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into simple values(2);
Query OK, 1 row affected (0.00 sec)

mysql> select @@tx_isolation;                                                              
+-----------------+                                                                         
| @@tx_isolation  |
+-----------------+
| REPEATABLE-READ |                                                                         
+-----------------+

mysql> begin;                                    
Query OK, 0 rows affected (0.01 sec)            
                                        mysql> begin;
                                        Query OK, 0 rows affected (0.00 sec)

mysql> update simple set col=10 where col=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

                                         mysql> update simple set col=42 where col=2;
                                         -- blocks

现在,最后一个更新命令(在客户端2中)正在等待.我希望命令能够执行,因为我想只有第1行被锁定了.即使客户端2中的第二个命令为insert,其行为也相同.谁能描述这个示例背后的锁定背景(锁定的位置和原因)?

Now, the last update command (in the client 2) waits. I would expect the command to execute because I would suppose only the row 1 is locked. The behaviour is the same even if the second command in the client 2 is insert. Could anyone describe the locking background behind this example (where and why the locks)?

推荐答案

InnoDB如下设置特定类型的锁.

InnoDB sets specific types of locks as follows.

  • SELECT ... FROM是一致的读取,读取数据库快照并且不设置锁定,除非将事务隔离级别设置为SERIALIZABLE.对于SERIALIZABLE级别,搜索会在遇到的索引记录上设置共享的下一键锁定.

  • SELECT ... FROM is a consistent read, reading a snapshot of the database and setting no locks unless the transaction isolation level is set to SERIALIZABLE. For SERIALIZABLE level, the search sets shared next-key locks on the index records it encounters.

SELECT ... FROM ... LOCK IN SHARE MODE在搜索遇到的所有索引记录上设置共享的下一键锁定.

SELECT ... FROM ... LOCK IN SHARE MODE sets shared next-key locks on all index records the search encounters.

对于索引记录搜索遇到的问题,SELECT ... FROM ... FOR UPDATE阻止其他会话执行SELECT ... FROM ... LOCK IN SHARE MODE或读取某些事务隔离级别.一致的读取将忽略读取视图中存在的记录上设置的任何锁定.

For index records the search encounters, SELECT ... FROM ... FOR UPDATE blocks other sessions from doing SELECT ... FROM ... LOCK IN SHARE MODE or from reading in certain transaction isolation levels. Consistent reads will ignore any locks set on the records that exist in the read view.

UPDATE ... WHERE ...在搜索遇到的每条记录上设置排他的下一键锁定.

UPDATE ... WHERE ... sets an exclusive next-key lock on every record the search encounters.

DELETE FROM ... WHERE ...在搜索遇到的每条记录上设置排他的下一键锁定.

DELETE FROM ... WHERE ... sets an exclusive next-key lock on every record the search encounters.

INSERT在插入的行上设置排他锁.该锁是索引记录锁,不是下一键锁(即没有间隙锁),并且不会阻止其他会话插入到插入行之前的间隙中.

INSERT sets an exclusive lock on the inserted row. This lock is an index-record lock, not a next-key lock (that is, there is no gap lock) and does not prevent other sessions from inserting into the gap before the inserted row.

InnoDB有几种类型的记录级锁:

InnoDB has several types of record-level locks:

  • 记录锁定:这是对索引记录的锁定.

  • Record lock: This is a lock on an index record.

间隙锁定:这是对索引记录之间的间隙的锁定,或者是对第一个或最后一个索引记录之前的间隙的锁定.

Gap lock: This is a lock on a gap between index records, or a lock on the gap before the first or after the last index record.

下一键锁定:这是对索引记录的记录锁定和对索引记录之前的间隙的间隙锁定的组合.

Next-key lock: This is a combination of a record lock on the index record and a gap lock on the gap before the index record.

查看更多:

避免使用下一键锁定的幻影问题

避免死锁

这篇关于Inno db隔离级别和锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆