当前位置:网站首页>【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. 串行化是最终方案
边栏推荐
- 并发编程-单例
- Slam mapping and autonomous navigation simulation based on turnlebot3
- 导师对帮助研究生顺利完成学业提出了20条劝告:第一,不要有度假休息的打算.....
- libvirt 中体验容器
- How to mix embedded MCU, arm and DSP?
- DS90UB949
- Kibana - installation and configuration of kibana
- Kubernetes 三打探针及探针方式
- Software testing weekly (issue 78): the more confident you are about the future, the more patient you are about the present.
- R language uses grid of gridextra package The array function combines multiple visual images of the ggplot2 package horizontally, and the ncol parameter defines the number of columns of the combined g
猜你喜欢

"Jianzhi offer 04" two-dimensional array search

vulnhub之raven2

Raven2 of vulnhub

cgroup简介

AI模型看看视频,就学会了玩《我的世界》:砍树、造箱子、制作石镐样样不差...

AOSP ~ NTP ( 网络时间协议 )

Slam mapping and autonomous navigation simulation based on turnlebot3

Viewing binary bin files with notepad++ editor

Xml的(DTD,xml解析,xml建模)

vulnhub之presidential
随机推荐
How should intermediate software designers prepare for the soft test
Cacti监控Redis实现过程
OpenGL 着色器使用
Mmc5603nj geomagnetic sensor (Compass example)
牛牛的组队竞赛
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
Go语言实现静态服务器
PHP基础
Kibana~Kibana的安装和配置
DS90UB949
Qt OpenGL 旋转、平移、缩放
一些常用术语
MySQL searches and sorts out common methods according to time
This article explains the complex relationship between MCU, arm, MCU, DSP, FPGA and embedded system
vulnhub之raven2
Qt+VTK+OCCT读取IGES/STEP模型
vulnhub之presidential
AI模型看看视频,就学会了玩《我的世界》:砍树、造箱子、制作石镐样样不差...
Notes on 32-96 questions of sword finger offer
R语言使用data.table包进行数据聚合统计计算滑动窗口统计值(Window Statistics)、计算滑动分组中位数(median)并合并生成的统计数据到原数据集中