测试环境: MySQL 5.7.26
创建测试表:
mysql> create table t3(id int not null default '0',primary key(id));
Query OK, 0 rows affected (0.00 sec)
插入测试数据:
mysql> insert into t3 values(1);
Query OK, 1 row affected (0.00 sec)
死锁模拟过程如下:
![](https://i-blog.csdnimg.cn/blog_migrate/1c350759f701b6873ccfc5f7367b39ef.png)
会话1commit之后,会话2核会话3产生死锁,会话3回滚,并抛出:
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
过程说明:
![](https://i-blog.csdnimg.cn/blog_migrate/f2ed79826f27dabfe113b1502fc9ab88.png)
死锁日志:
LATEST DETECTED DEADLOCK
------------------------
2020-08-21 18:23:54 0x7f39023e7700
*** (1) TRANSACTION:
TRANSACTION 26174234, ACTIVE 8 sec inserting
mysql tables in use 1, locked 1
LOCK WAIT 3 lock struct(s), heap size 1136, 2 row lock(s)
MySQL thread id 208, OS thread handle 139883056465664, query id 3858292 localhost root update
insert into t3 values(1)
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 833 page no 3 n bits 72 index PRIMARY of table `test`.`t3` trx id 26174234 lock_mode X locks rec but not gap waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 3; compact format; info bits 32
0: len 4; hex 80000001; asc ;;
1: len 6; hex 0000018f6307; asc c ;;
2: len 7; hex 3b000021e40110; asc ; ! ;;
*** (2) TRANSACTION:
TRANSACTION 26174241, ACTIVE 5 sec inserting, thread declared inside InnoDB 1
mysql tables in use 1, locked 1
3 lock struct(s), heap size 1136, 2 row lock(s)
MySQL thread id 210, OS thread handle 139882827511552, query id 3858305 localhost root update
insert into t3 values(1)
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 833 page no 3 n bits 72 index PRIMARY of table `test`.`t3` trx id 26174241 lock mode S locks rec but not gap
Record lock, heap no 2 PHYSICAL RECORD: n_fields 3; compact format; info bits 32
0: len 4; hex 80000001; asc ;;
1: len 6; hex 0000018f6307; asc c ;;
2: len 7; hex 3b000021e40110; asc ; ! ;;
*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 833 page no 3 n bits 72 index PRIMARY of table `test`.`t3` trx id 26174241 lock_mode X locks rec but not gap waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 3; compact format; info bits 32
0: len 4; hex 80000001; asc ;;
1: len 6; hex 0000018f6307; asc c ;;
2: len 7; hex 3b000021e40110; asc ; ! ;;
*** WE ROLL BACK TRANSACTION (2)