试图模拟所示的死锁对我来说是这样的:
-- connection 0
set transaction isolation level serializable;
start transaction;
select * from test.innodb_deadlock_maker where a = 0;
-- does not block, fails immediately with
-- ERROR 1062 (23000): Duplicate entry '0' for key 'PRIMARY'
update test.innodb_deadlock_maker set a = 0 where a <> 0;
-- connection 1
set transaction isolation level serializable;
start transaction;
select * from test.innodb_deadlock_maker where a = 1;
-- does block, fails after a timeout with
-- ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
update test.innodb_deadlock_maker set a = 1 where a <> 1;
因此,连接 1 等待连接 0,但连接 0 不等待连接 1,只等待应用程序COMMIT
或ROLLBACK
来自应用程序。
这不是死锁,SHOW ENGINE INNODB STATUS
也不会报告死锁。
将顺序更改为:
-- connection 0
set transaction isolation level serializable;
start transaction;
select * from test.innodb_deadlock_maker where a = 0;
-- connection 1
set transaction isolation level serializable;
start transaction;
select * from test.innodb_deadlock_maker where a = 1;
-- connection 0
-- does block, waiting on connection 1
update test.innodb_deadlock_maker set a = 0 where a <> 0;
-- connection 1
-- would deadlock, fails immediately with
-- ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting
update test.innodb_deadlock_maker set a = 1 where a <> 1;
这会解锁连接 0:
-- connection 0
update test.innodb_deadlock_maker set a = 0 where a <> 0;
-- now unblocked, fails with
-- ERROR 1062 (23000): Duplicate entry '0' for key 'PRIMARY'
在这种情况下,SHOW ENGINE INNODB STATUS
报告:
LATEST DETECTED DEADLOCK
------------------------
2013-10-03 11:57:27 0x7f6b60308700
*** (1) TRANSACTION:
TRANSACTION 1297, ACTIVE 47 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 5 lock struct(s), heap size 1160, 3 row lock(s)
MySQL thread id 2, OS thread handle 140099372304128, query id 13 localhost root Searching rows for update
update test.innodb_deadlock_maker set a = 0 where a <> 0
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 8 page no 3 n bits 72 index `PRIMARY` of table `test`.`innodb_deadlock_maker` trx id 1297 lock_mode X waiting
Record lock, heap no 3 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
0: len 4; hex 80000001; asc ;;
1: len 6; hex 000000000508; asc ;;
2: len 7; hex a80000011a011c; asc ;;
*** (2) TRANSACTION:
TRANSACTION 1298, ACTIVE 15 sec starting index read
mysql tables in use 1, locked 1
4 lock struct(s), heap size 1160, 2 row lock(s)
MySQL thread id 3, OS thread handle 140099152021248, query id 14 localhost root Searching rows for update
update test.innodb_deadlock_maker set a = 1 where a <> 1
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 8 page no 3 n bits 72 index `PRIMARY` of table `test`.`innodb_deadlock_maker` trx id 1298 lock mode S locks rec but not gap
Record lock, heap no 3 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
0: len 4; hex 80000001; asc ;;
1: len 6; hex 000000000508; asc ;;
2: len 7; hex a80000011a011c; asc ;;
*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 8 page no 3 n bits 72 index `PRIMARY` of table `test`.`innodb_deadlock_maker` trx id 1298 lock_mode X waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
0: len 4; hex 80000000; asc ;;
1: len 6; hex 000000000508; asc ;;
2: len 7; hex a80000011a0110; asc ;;
*** WE ROLL BACK TRANSACTION (2)
我不能说话pt-deadlock-logger
,但我认为它依赖于 中的LATEST DETECTED DEADLOCK
部分SHOW ENGINE INNODB STATUS
,所以首先请确保该命令确实报告了死锁。