通过唯一索引S锁与X锁来了解MySQL死锁套路
在初学者从源码理解mysql死锁问题中介绍了使用调试 mysql 源码的方式来查看死锁的过程,这篇文章来讲讲一个常见的案例。
这次我们讲一段唯一索引 s 锁与 x 锁的爱恨情仇
我们来看一个简化过的例子
# 构造数据 create table `t1` ( `id` int(11) not null auto_increment, `name` varchar(10), `level` int(11), primary key (`id`), unique key `uk_name` (`name`) ); insert into `t1` (`name`, `level`) values ('a',0); # 出现问题的sql语句如下,并发情况下就会出现死锁 insert ignore into `t1` (`name`, `level`) values ('a',0); update t1 set level = 1 where name = "a";
我们用之前介绍过的源码分析方式,先来看下这两条语句分别加什么锁,然后分析死锁形成的过程。
第一条语句
insert ignore into t1 (name, level) values ('a',0);
在调试中得到的结果如下
可以看到这条语句对唯一键 uk_name 加共享锁(s锁),而且成功。
第二条语句
update t1 set level = 1 where name = "a";
通过唯一键更新数据库字段。
这种情况在之前的文章已经介绍过,会对唯一索引加 x 锁,然后对主键索引加 x 锁
这样就可以非常轻松的复现死锁的问题了,步骤如下
1.开启两个 session,分别 begin
2.session1 执行insert ignore into t1 (name, level) values ('a',0);
3.session2 执行insert ignore into t1 (name, level) values ('a',0);
4.session1 执行update t1 set level = 1 where name = "a"; 进入等待状态
5.session2 执行update t1 set level = 1 where name = "a";,死锁产生,被回滚,同时事务 1 执行成功
详细的锁状态变化如下
t1 | t2 | 备注 |
---|---|---|
insert ignore into | - | t1成功获得uk的s锁 db_success |
- | insert ignore into | t2成功获得uk的s锁 db_success |
update | - | t1尝试获得uk的x锁,但没有成功,处于等待状态 db_lock_wait |
- | update | t2尝试获得uk的x锁,发现死锁产生 db_deadlock |
- | deadlock | t2释放s锁 |
成功 | - | - |
死锁日志如下:
latest detected deadlock ------------------------ 181208 23:00:52 *** (1) transaction: transaction 53a7, active 162 sec starting index read mysql tables in use 1, locked 1 lock wait 3 lock struct(s), heap size 376, 2 row lock(s) mysql thread id 12, os thread handle 0x700010522000, query id 1424 localhost root updating update t1 set level = 1 where name = "a" *** (1) waiting for this lock to be granted: record locks space id 89 page no 4 n bits 72 index `uk_name` of table `lock_demo2`.`t1` trx id 53a7 lock_mode x locks rec but not gap waiting record lock, heap no 2 physical record: n_fields 2; compact format; info bits 0 0: len 1; hex 41; asc a;; 1: len 4; hex 80000001; asc ;; *** (2) transaction: transaction 53a8, active 8 sec starting index read mysql tables in use 1, locked 1 3 lock struct(s), heap size 376, 2 row lock(s) mysql thread id 96, os thread handle 0x70001062e000, query id 1425 localhost root updating update t1 set level = 1 where name = "a" *** (2) holds the lock(s): record locks space id 89 page no 4 n bits 72 index `uk_name` of table `lock_demo2`.`t1` trx id 53a8 lock mode s record lock, heap no 2 physical record: n_fields 2; compact format; info bits 0 0: len 1; hex 41; asc a;; 1: len 4; hex 80000001; asc ;; *** (2) waiting for this lock to be granted: record locks space id 89 page no 4 n bits 72 index `uk_name` of table `lock_demo2`.`t1` trx id 53a8 lock_mode x locks rec but not gap waiting record lock, heap no 2 physical record: n_fields 2; compact format; info bits 0 0: len 1; hex 41; asc a;; 1: len 4; hex 80000001; asc ;; *** we roll back transaction (2)
来详细看一下这个死锁日志
*** (1) waiting for this lock to be granted: record locks space id 89 page no 4 n bits 72 index uk_name of table lock_demo2.t1 trx id 53a7 lock_mode x locks rec but not gap waiting
事务 1 想获取 uk_name 唯一索引上的 x 锁 (非 gap 锁的记录锁)
*** (2) holds the lock(s): record locks space id 89 page no 4 n bits 72 index uk_name of table lock_demo2.t1 trx id 53a8 lock mode s
事务 2 持有uk_name 唯一索引上的 s 锁(共享锁)
*** (2) waiting for this lock to be granted: record locks space id 89 page no 4 n bits 72 index uk_name of table lock_demo2.t1 trx id 53a8 lock_mode x locks rec but not gap waiting
事务 2 想获得 uk_name 唯一索引上的 x 锁(非 gap 锁的记录锁)
跟之前理论上推断的结论是一致的
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: C4D R19模型怎么封闭多边形孔洞?