当前位置:网站首页>[official MySQL document] deadlock
[official MySQL document] deadlock
2022-07-03 11:59:00 【Programmer Xiao Li】
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.
Deadlock is generated in this way : Different transactions hold the locks needed by each other , And wait for the other party to release the lock .
One select ··· lock in share mode And delete Deadlock instance of
Create a table , And insert data :
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)
Business A | Business B | result |
select * from t where i = 1 lock in share mode; | Get S lock | |
delete from t where i = 1; | Try to get X lock , For business A Hold the read lock of the row , So enter the lock waiting queue . | |
delete from t where i = 1; | Deadlock occurs , Here is the transaction A Trying to get a write lock , But the business B In front of the waiting queue , Business A Cannot upgrade a read lock to a write lock .A wait for B Release the write lock ,B wait for A Release read lock . |
When update、select for update Different tables are locked in different order , It's prone to deadlock .
In the same table , Two transactions lock different records or gaps in different order , It's also deadlocked .
InnoDB Use automatic row level locking . Even in only insert or delete In a row of transactions , There can also be deadlocks . This is because these operations are not real “ atom ”; They will automatically insert or delete lines ( There may be more than one ) Index record setting lock .
How to avoid deadlock
1. Business is better than LOCK TABLE It will be better
2. The business is as small as possible , Avoid reading and writing large amounts of data
3. As far as possible, different transactions acquire locks in the same order (select for update)
4. stay select for update and update where Create index on
5. The possibility of deadlock has nothing to do with the isolation level of transactions , Deadlocks are related to write operations , The isolation level solves the visibility of reading
6. SHOW ENGINE INNODB STATUS You can check the cause of the deadlock .
7. When deadlocks occur frequently , Can make innodb_print_all_deadlocks
8. Use select for update , select lock in share mode, The recommended isolation level is RC
9. Operate multiple tables , Or different rows of the same table , It is recommended to use the same sequence , Deadlock can be avoided
10. Set the appropriate index , Avoid scanning a large number of data rows
11. Use as common as possible select instead of for update, Avoid locking
12. Serialization is the final solution
边栏推荐
猜你喜欢
随机推荐
【学习笔记】dp 状态与转移
836. 合并集合(DAY 63)并查集
Differences between MySQL Union and union all
Solve msvcp120d DLL and msvcr120d DLL missing
OPenGL 基本知识(根据自己理解整理)
Qt+vtk+occt reading iges/step model
836. Merge sets (day 63) and search sets
STL教程8-map
DNS multi-point deployment IP anycast+bgp actual combat analysis
Test classification in openstack
typeScript
The uniapp scroll view solves the problems of high adaptability and bullet frame rolling penetration.
R语言使用data.table包进行数据聚合统计计算滑动窗口统计值(Window Statistics)、计算滑动分组中位数(median)并合并生成的统计数据到原数据集中
Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
MCDF Experiment 1
win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法
Extrapolated scatter data
Introduction to the implementation principle of rxjs observable filter operator
R语言使用aggregate函数计算dataframe数据分组聚合的均值(sum)、不设置na.rm计算的结果、如果分组中包含缺失值NA则计算结果也为NA
Concurrent programming - singleton