当前位置:网站首页>【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. 串行化是最终方案
边栏推荐
- ORACLE进阶(一) 通过EXPDP IMPDP命令实现导dmp
- Numpy np.max和np.maximum实现relu函数
- 量化计算调研
- Duplicate numbers in the array of sword finger offer 03
- Keepalived中Master和Backup角色选举策略
- phpcms 提示信息页面跳转showmessage
- uniapp实现点击加载更多
- 基于turtlebot3实现SLAM建图及自主导航仿真
- After watching the video, AI model learned to play my world: cutting trees, making boxes, making stone picks, everything is good
- vulnhub之GeminiInc
猜你喜欢
vulnhub之Nagini
Qt+VTK+OCCT读取IGES/STEP模型
同事写了一个责任链模式,bug无数...
Use typora to draw flow chart, sequence diagram, sequence diagram, Gantt chart, etc. for detailed explanation
牛牛的组队竞赛
Excel quick cross table copy and paste
vulnhub之tomato(西红柿)
Analysis of EPS electric steering system
STL tutorial 10 container commonalities and usage scenarios
ftp登录时,报错“530 Login incorrect.Login failed”
随机推荐
同事写了一个责任链模式,bug无数...
Excel快速跨表复制粘贴
Stm32hal library upgrades firmware based on flash analog U disk (detailed explanation)
OpenStack中的测试分类
R language uses data The table package performs data aggregation statistics, calculates window statistics, calculates the median of sliding groups, and merges the generated statistical data into the o
Nestjs configuration service, configuring cookies and sessions
错排问题 (抽奖,发邮件)
OPenGL 基本知识(根据自己理解整理)
STL Tutorial 9 deep copy and shallow copy of container elements
Xml的(DTD,xml解析,xml建模)
Web安全总结
量化计算调研
STL tutorial 10 container commonalities and usage scenarios
Dynamic programming (interval DP)
金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~
R语言使用gridExtra包的grid.arrange函数将ggplot2包的多个可视化图像横向组合起来,ncol参数自定义组合图列数、nrow参数自定义组合图行数
Understand go language context in one article
Repo ~ common commands
Keepalived中Master和Backup角色选举策略
Use typora to draw flow chart, sequence diagram, sequence diagram, Gantt chart, etc. for detailed explanation