当前位置:网站首页>【mysql官方文档】死锁
【mysql官方文档】死锁
2022-07-03 11:06:00 【程序员·小李】
A deadlock is a situation where different transactions are unable to proceed because each holds a lock that the other needs. Because both transactions are waiting for a resource to become available, neither ever release the locks it holds.
死锁是这样产生的:不同的事务都持有对方所需要的锁,并同时等待对方释放锁。
一个select ··· lock in share mode与delete的死锁实例
创建一个表,并插入数据:
mysql> CREATE TABLE t (i INT) ENGINE = InnoDB;
Query OK, 0 rows affected (1.07 sec)
mysql> INSERT INTO t (i) VALUES(1);
Query OK, 1 row affected (0.09 sec)
事务A | 事务B | 结果 |
select * from t where i = 1 lock in share mode; | 获取到S锁 | |
delete from t where i = 1; | 试图获取X锁, 因事务A持有该行的读锁,因此进入锁等待队列。 | |
delete from t where i = 1; | 死锁产生,此处事务A试图获取一个写锁,但是事务B在等待队列的前面,事务A无法将读锁升级为写锁。A等待B释放写锁,B等待A释放读锁。 |
当update、select for update按照不同的顺序锁定了不同的表,就容易发生死锁。
在同一张表中,两个事务按不同顺序分别锁定不同的记录或间隙,也会死锁。
InnoDB使用自动行级锁定。即使在只insert或delete一行的事务中,也可能出现死锁。这是因为这些操作不是真正的“原子”;它们会自动对插入或删除的行的(可能有多个)索引记录设置锁。
如何避免死锁
1. 事务比LOCK TABLE会好一些
2. 事务尽可能小,避免读写大量数据
3. 不同事务尽可能按照相同的顺序获取锁(select for update)
4. 在select for update 和 update where 上创建索引
5. 死锁出现的可能性与事务的隔离级别没有关系,死锁与写操作有关,隔离级别解决的是读取的可见性
6. SHOW ENGINE INNODB STATUS 可以查看死锁的原因。
7. 频繁出现死锁时,使能innodb_print_all_deadlocks
8. 使用select for update , select lock in share mode,建议隔离级别为RC
9. 操作多张表,或者同一张表的不同行时,建议使用相同的顺序,可以避免死锁的产生
10. 设置合适的索引,避免扫描大量的数据行
11. 尽可能使用一般select而不是for update,避免锁定
12. 串行化是最终方案
边栏推荐
- 机器学习 3.2 决策树模型 学习笔记(待补)
- Machine learning 3.2 decision tree model learning notes (to be supplemented)
- Concurrent programming - singleton
- 小鹏 P7 撞护栏安全气囊未弹出,官方回应称撞击力度未达到弹出要求
- R language ggplot2 visualization: gganimate package creates dynamic line graph animation (GIF) and uses transition_ The reveal function displays data step by step along a given dimension in the animat
- Groovy测试类 和 Junit测试
- Go语言实现静态服务器
- Qt OpenGL相机的使用
- POI excel cell wrap
- P3250 [hnoi2016] Network + [necpc2022] f.tree path tree section + segment tree maintenance heap
猜你喜欢
Kibana~Kibana的安装和配置
Groovy测试类 和 Junit测试
Viewing binary bin files with notepad++ editor
Kibana - installation and configuration of kibana
Based on MCU, how to realize OTA differential upgrade with zero code and no development?
Spl06-007 air pressure sensor (example of barometer)
836. Merge sets (day 63) and search sets
After watching the video, AI model learned to play my world: cutting trees, making boxes, making stone picks, everything is good
vulnhub之raven2
抓包整理外篇fiddler———— 会话栏与过滤器[二]
随机推荐
鸿蒙第四次培训
P3250 [HNOI2016] 网络 + [NECPC2022] F.Tree Path 树剖+线段树维护堆
Sheet1$. Output [excel source output] Error in column [xxx]. The returned column status is: "the text is truncated, or one or more characters have no matches in the target code page.".
银泰百货点燃城市“夜经济”
repo ~ 常用命令
Web安全总结
Deploying WordPress instance tutorial under coreos
Qt OpenGL 旋转、平移、缩放
DNS多点部署IP Anycast+BGP实战分析
How should intermediate software designers prepare for the soft test
R language ggplot2 visualization: gganimate package creates dynamic line graph animation (GIF) and uses transition_ The reveal function displays data step by step along a given dimension in the animat
STL教程9-容器元素深拷贝和浅拷贝问题
导师对帮助研究生顺利完成学业提出了20条劝告:第一,不要有度假休息的打算.....
Vulnhub geminiinc V2
MCDF实验1
Hongmeng fourth training
在CoreOS下部署WordPress实例教程
优化接口性能
R语言使用gridExtra包的grid.arrange函数将ggplot2包的多个可视化图像横向组合起来,ncol参数自定义组合图列数、nrow参数自定义组合图行数
The excel table is transferred to word, and the table does not exceed the edge paper range