测试环境:MySQL 5.7.26
创建测试表:
1 2 | create table t2(id int auto_increment,status int default null , primary key (id), unique key (status)); Query OK, 0 rows affected (0.01 sec) |
死锁模拟过程如下:
会话2申请s锁,等待会话1,会话1申请意向插入锁,等待会话2。innodb选择kill掉会话2,并抛出
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
过程说明:
查看死锁日志:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | LATEST DETECTED DEADLOCK ------------------------ 2020-08-21 17:28:12 0x7f38f84d3700 *** (1) TRANSACTION: TRANSACTION 26167492, ACTIVE 17 sec inserting mysql tables in use 1, locked 1 LOCK WAIT 2 lock struct(s), heap size 1136, 1 row lock(s), undo log entries 1 MySQL thread id 208, OS thread handle 139883056465664, query id 3844857 localhost root update insert into t2 values(null,10) *** (1) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 832 page no 4 n bits 72 index status of table ` test `.`t2` trx id 26167492 lock mode S waiting Record lock, heap no 2 PHYSICAL RECORD: n_fields 2; compact format ; info bits 0 0: len 4; hex 8000000a; asc ;; 1: len 4; hex 80000001; asc ;; *** (2) TRANSACTION: TRANSACTION 26167469, ACTIVE 28 sec inserting, thread declared inside InnoDB 5000 mysql tables in use 1, locked 1 3 lock struct(s), heap size 1136, 2 row lock(s), undo log entries 2 MySQL thread id 209, OS thread handle 139882660706048, query id 3844926 localhost root update insert into t2 values(null,9) *** (2) HOLDS THE LOCK(S): RECORD LOCKS space id 832 page no 4 n bits 72 index status of table ` test `.`t2` trx id 26167469 lock_mode X locks rec but not gap Record lock, heap no 2 PHYSICAL RECORD: n_fields 2; compact format ; info bits 0 0: len 4; hex 8000000a; asc ;; 1: len 4; hex 80000001; asc ;; *** (2) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 832 page no 4 n bits 72 index status of table ` test `.`t2` trx id 26167469 lock_mode X locks gap before rec insert intention waiting Record lock, heap no 2 PHYSICAL RECORD: n_fields 2; compact format ; info bits 0 0: len 4; hex 8000000a; asc ;; 1: len 4; hex 80000001; asc ;; *** WE ROLL BACK TRANSACTION (1) |