MYSQL死锁分析案例一(唯一键冲突)

Create Table: CREATE TABLE `t1` (
`id` int(11) NOT NULL,
`b` int(11) DEFAULT NULL,
`c` int(11) DEFAULT NULL,
`d` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `b` (`b`,`c`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

 

mysql> insert into t1 select 1,1,2,3;

mysql> insert into t1 select 5,2,2,3;

 

mysql> select * from t1;
+----+------+------+------+
| id | b | c | d |
+----+------+------+------+
| 1 | 1 | 2 | 3 |
| 5 | 2 | 2 | 3 |
+----+------+------+------+

 

session A:begin; select * from t1 where b=1 and c=2 for update;

session B:begin; select * from t1 where b=1 and c=2 for update;

sessionA :insert into t1 select 3,1,2,3;

 

报错顺序是会话B先报错,会话A再报错,下面会讲。

会话B报错:ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction

会话A报错:ERROR 1062 (23000): Duplicate entry '1-2' for key 'b'

 

输出下死锁信息:show engine innodb status\G

 

------------------------
LATEST DETECTED DEADLOCK
------------------------
2020-01-21 16:25:00 0x7fd5deeb2700
*** (1) TRANSACTION:
TRANSACTION 15117731, ACTIVE 28 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 1136, 1 row lock(s)
MySQL thread id 233174, OS thread handle 140556482070272, query id 5674894 106.1
1.34.12 root statistics
select * from t1 where b=1 and c=2 for update
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 21542 page no 4 n bits 72 index b of table `ceshi`.`t1` tr
x id 15117731 lock_mode X locks rec but not gap waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
0: len 4; hex 80000001; asc ;;
1: len 4; hex 80000002; asc ;;
2: len 4; hex 80000001; asc ;;

*** (2) TRANSACTION:
TRANSACTION 15117718, ACTIVE 49 sec inserting
mysql tables in use 1, locked 1
4 lock struct(s), heap size 1136, 3 row lock(s), undo log entries 1
MySQL thread id 208385, OS thread handle 140556544714496, query id 5675592 106.1
1.34.12 root executing

insert into t1 select 3,1,2,3
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 21542 page no 4 n bits 72 index b of table `ceshi`.`t1` tr
x id 15117718 lock_mode X locks rec but not gap
Record lock, heap no 2 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
0: len 4; hex 80000001; asc ;;
1: len 4; hex 80000002; asc ;;
2: len 4; hex 80000001; asc ;;

*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 21542 page no 4 n bits 72 index b of table `ceshi`.`t1` tr
x id 15117718 lock mode S waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
0: len 4; hex 80000001; asc ;;
1: len 4; hex 80000002; asc ;;
2: len 4; hex 80000001; asc ;;

*** WE ROLL BACK TRANSACTION (1)

下面分析下加锁流程:

会话A:对唯一索引(1,2)这条记录加X锁。

会话B:对唯一索引(1,2)这条记录加X锁,会话A持有锁,会话B只能等待。

会话A:写入一条(3,1,2,3)记录,可以看到1,2会造成唯一键冲突,

数据库会在主键冲突的记录上加S锁,这时就会有环路出现, 

B------->A:B在等A的X锁。

A------->B:A要拿记录的S锁,但要等B的X锁。

1、数据库检测到有死锁,会自动KILL掉其中一个事务,根据日志信息,可以看到KILL掉了B会话,

2、B会话KILL掉之后,死锁消失,此时A会继续执行insert into t1 select 3,1,2,3;这条写入记录,会报唯一键冲突报错。

posted on   柴米油盐酱醋  阅读(1134)  评论(3编辑  收藏  举报

编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示