测试环境:MySQL 5.7.26创建测试表:
create table t1 (id int not null,name int,primary key(id),unique key(name));
死锁模拟过程如下:
会话1rollback之后会话2和3出现死锁,innodb选择kill掉会话3并抛出:ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
死锁日志如下:
LATEST DETECTED DEADLOCK ------------------------ 2020-08-21 16:56:19 0x7f39023e7700 *** (1) TRANSACTION: TRANSACTION 26163597, ACTIVE 46 sec inserting, thread declared inside InnoDB 1 mysql tables in use 1, locked 1 LOCK WAIT 4 lock struct(s), heap size 1136, 2 row lock(s), undo log entries 1 MySQL thread id 208, OS thread handle 139883056465664, query id 3837074 localhost root update insert into t1 values(2018,255) *** (1) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 831 page no 4 n bits 72 index name of table `test`.`t1` trx id 26163597 lock_mode X insert intention waiting Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0 0: len 8; hex 73757072656d756d; asc supremum;; *** (2) TRANSACTION: TRANSACTION 26163678, ACTIVE 6 sec inserting, thread declared inside InnoDB 1 mysql tables in use 1, locked 1 4 lock struct(s), heap size 1136, 2 row lock(s), undo log entries 1 MySQL thread id 210, OS thread handle 139882827511552, query id 3837238 localhost root update insert into t1 values(2019,255) *** (2) HOLDS THE LOCK(S): RECORD LOCKS space id 831 page no 4 n bits 72 index name of table `test`.`t1` trx id 26163678 lock mode S Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0 0: len 8; hex 73757072656d756d; asc supremum;; *** (2) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 831 page no 4 n bits 72 index name of table `test`.`t1` trx id 26163678 lock_mode X insert intention waiting Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0 0: len 8; hex 73757072656d756d; asc supremum;; *** WE ROLL BACK TRANSACTION (2)
