Mysql innodb 锁定 : IX should not be granted when X is held, 但确实

标签 mysql locking innodb

根据manual , IX 表锁与 X 行锁冲突,因此在持有此类锁时无法授予。引用

A lock is granted to a requesting transaction if it is compatible with existing locks, but not if it conflicts with existing locks.

上表显示IX和X锁冲突。然而,这不是我所经历的。使用 mysql 命令行工具,如果我执行以下命令

drop table if exists test; create table test (A int, B int, primary key (A,B), index (A)) TYPE = InnoDB;  insert into test values (1,1); start transaction; delete from test where a=1;

然后运行 ​​SHOW ENGINE INNODB STATUS,我明白了

------------
TRANSACTIONS
------------
Trx id counter 0 4604459
Purge done for trx's n:o < 0 4604457 undo n:o < 0 0
History list length 13
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 0 4604434, not started, process no 2015, OS thread id 140642069153536
MySQL thread id 55, query id 1689 localhost esben
---TRANSACTION 0 4604256, not started, process no 2015, OS thread id 140642068752128
MySQL thread id 45, query id 1708 localhost root
SHOW ENGINE INNODB STATUS
---TRANSACTION 0 4604458, ACTIVE 15 sec, process no 2015, OS thread id 140642068952832
2 lock struct(s), heap size 368, 2 row lock(s), undo log entries 1
MySQL thread id 56, query id 1707 localhost esben
TABLE LOCK table "esben_mosegris-esben-trunk"."test" trx id 0 4604458 lock mode IX
RECORD LOCKS space id 7800 page no 3 n bits 72 index "PRIMARY" of table "esben_mosegris-esben-trunk"."test" trx id 0 4604458 lock_mode X
Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
 0: len 8; hex 73757072656d756d; asc supremum;;

Record lock, heap no 2 PHYSICAL RECORD: n_fields 4; compact format; info bits 32
 0: len 4; hex 80000001; asc     ;; 1: len 4; hex 80000001; asc     ;; 2: len 6; hex 00000046422a; asc    FB*;; 3: len 7; hex 00000000392350; asc     9#P;;

请注意,事务 4604459 在表中的其中一行上持有 X 锁,因此另一个事务不可能在“测试”表上获取 IX 锁。但是,执行

start transaction; delete from test where a=2;

在另一个 mysql 命令行客户端直接通过,给出以下输出

------------
TRANSACTIONS
------------
Trx id counter 0 4604460
Purge done for trx's n:o < 0 4604457 undo n:o < 0 0
History list length 13
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 0 4604256, not started, process no 2015, OS thread id 140642068752128
MySQL thread id 45, query id 1712 localhost root
SHOW ENGINE INNODB STATUS
---TRANSACTION 0 4604459, ACTIVE 18 sec, process no 2015, OS thread id 140642069153536
2 lock struct(s), heap size 368, 1 row lock(s)
MySQL thread id 55, query id 1710 localhost esben
TABLE LOCK table "esben_mosegris-esben-trunk"."test" trx id 0 4604459 lock mode IX
RECORD LOCKS space id 7800 page no 3 n bits 72 index "PRIMARY" of table "esben_mosegris-esben-trunk"."test" trx id 0 4604459 lock_mode X
Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
 0: len 8; hex 73757072656d756d; asc supremum;;

---TRANSACTION 0 4604458, ACTIVE 55 sec, process no 2015, OS thread id 140642068952832
2 lock struct(s), heap size 368, 2 row lock(s), undo log entries 1
MySQL thread id 56, query id 1707 localhost esben
TABLE LOCK table "esben_mosegris-esben-trunk"."test" trx id 0 4604458 lock mode IX
RECORD LOCKS space id 7800 page no 3 n bits 72 index "PRIMARY" of table "esben_mosegris-esben-trunk"."test" trx id 0 4604458 lock_mode X
Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
 0: len 8; hex 73757072656d756d; asc supremum;;

Record lock, heap no 2 PHYSICAL RECORD: n_fields 4; compact format; info bits 32
 0: len 4; hex 80000001; asc     ;; 1: len 4; hex 80000001; asc     ;; 2: len 6; hex 00000046422a; asc    FB*;; 3: len 7; hex 00000000392350; asc     9#P;;

我错过了什么?我的问题是,因为这个位通过了,所以我进一步陷入了僵局,因为两个事务现在都持有 IX 锁。或者可能是因为两者都试图获取相同的范围锁。我可以解决这个问题,但我讨厌专门解决 mysql 问题的代码,尤其是当我无法解释它时。

第二个注意事项,是否在某处解释了“记录锁定”中的所有这些字段?

最佳答案

我创建了一张工单 http://bugs.mysql.com/bug.php?id=63665为了这。原因是锁只会在同一个粒度上发生冲突,这样ROW锁就不会和上表中的TABLE锁发生冲突。在工单中,我对手册提出了多项改进建议,以使其更加清晰。

关于Mysql innodb 锁定 : IX should not be granted when X is held, 但确实,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8402668/

相关文章:

php - 我想选择一个表并使用我选择的表中的相同数据创建一个表

go - "selective"goroutines 互斥

c# - 需要改进的线程锁定建议

mysql - 错误/usr/libexec/mysqld : Incorrect information in file

mysql - xampp 中的 Innodb 设置似乎找不到 my.cnf 文件

php - 表达式引擎中的 'username'全局变量可以用来从mysql数据库中选择记录吗?

mysql - 一个sql中的两个SQL查询

Java散列锁表

mysql - 无法创建表。愚蠢的问题,简单的查询

mysql - Unicode编码错误: 'charmap' codec can't encode character u'\xfd' in python 2. 7