当前位置:网站首页>【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. 串行化是最终方案
边栏推荐
- 金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~
- Mmc5603nj geomagnetic sensor (Compass example)
- Slam mapping and autonomous navigation simulation based on turnlebot3
- Stm32hal library upgrades firmware based on flash analog U disk (detailed explanation)
- Excel快速跨表复制粘贴
- GCC compilation process and dynamic link library and static link library
- ftp登录时,报错“530 Login incorrect.Login failed”
- Numpy np.max和np.maximum实现relu函数
- OpenStack中的测试分类
- STL教程9-容器元素深拷贝和浅拷贝问题
猜你喜欢

Hongmeng third training (project training)

Qt OpenGL 纹理贴图

STL Tutorial 9 deep copy and shallow copy of container elements

Spl06-007 air pressure sensor (example of barometer)

Kibana~Kibana的安装和配置

Slam mapping and autonomous navigation simulation based on turnlebot3

Excel表格转到Word中,表格不超边缘纸张范围

MCDF实验1

GCC compilation process and dynamic link library and static link library

STL tutorial 10 container commonalities and usage scenarios
随机推荐
Analysis of EPS electric steering system
repo ~ 常用命令
鸿蒙第三次培训(项目实训)
Cacti monitors redis implementation process
Ripper of vulnhub
Test classification in openstack
VS2015的下载地址和安装教程
在CoreOS下部署WordPress实例教程
MySQL union和union all区别
银泰百货点燃城市“夜经济”
MCDF实验1
Phpcms prompt message page Jump to showmessage
vulnhub之Ripper
一些常用术语
Xiaopeng P7 hit the guardrail and the airbag did not pop up. The official responded that the impact strength did not meet the ejection requirements
PHP Basics
vulnhub之cereal
phpcms 提示信息頁面跳轉showmessage
OpenStack中的测试分类
Introduction to the implementation principle of rxjs observable filter operator