当前位置:网站首页>【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. 串行化是最终方案
边栏推荐
- vulnhub之GeminiInc v2
- 一些常用术语
- MySQL union和union all区别
- Go语言实现静态服务器
- P3250 [hnoi2016] Network + [necpc2022] f.tree path tree section + segment tree maintenance heap
- Kubernetes 三打探针及探针方式
- Nestjs configuration service, configuring cookies and sessions
- PHP server interacts with redis with a large number of close_ Wait analysis
- 2022年中南大学夏令营面试经验
- 牛牛的组队竞赛
猜你喜欢
随机推荐
How should intermediate software designers prepare for the soft test
How to make others fear you
PHP基础
Nestjs配置服务,配置Cookie和Session
R language uses grid of gridextra package The array function combines multiple visual images of the lattice package horizontally, and the ncol parameter defines the number of columns of the combined g
vulnhub之narak
Visual Studio 2022下载及配置OpenCV4.5.5
Extrapolated scatter data
STL教程9-容器元素深拷贝和浅拷贝问题
STL教程8-map
导师对帮助研究生顺利完成学业提出了20条劝告:第一,不要有度假休息的打算.....
Numpy np. Max and np Maximum implements the relu function
AOSP ~ NTP ( 网络时间协议 )
Concurrent programming - singleton
聊聊Flink框架中的状态管理机制
The excel table is transferred to word, and the table does not exceed the edge paper range
Introduction to the implementation principle of rxjs observable filter operator
Kubernetes 三打探针及探针方式
Based on MCU, how to realize OTA differential upgrade with zero code and no development?
Uniapp implementation Click to load more








