当前位置:网站首页>MySQ deadlock
MySQ deadlock
2022-07-30 03:32:00 【Leon_Jinhai_Sun】
Deadlock means that two or more transactions occupy each other on the same resource and request to lock the resources occupied by each other, resulting in a vicious circle.
Condition:
1. Two or more transactions
2. Each transaction already holds a lock and applies for a new lock
3. Lock resources can only be held by the same transaction at the same time or are incompatible
4. Transactions wait for each other circularly due to holding locks and applying for locks
Transaction 1 and transaction 2 set row locks for the rows with id=1 and id=2 respectively.Then transaction 1 is waiting for transaction 2 to release the row lock with id=2, and transaction 2 is waiting for transaction 1 to release the row lock with id=1.Transaction 1 and transaction 2 are waiting for each other's resources to be released, that is, they enter a deadlock state.
The way to solve the deadlock:
1. Set the timeout period.When a transaction wait time reaches the threshold, it is rolled back and another transaction continues.The disadvantage is that the waiting time is too long and it is often unacceptable, and the time is too short and it is easy to accidentally hurt the ordinary lock waiting.
2. Use deadlock detection processing.After a deadlock is found, a transaction in the deadlock chain is actively rolled back (the transaction that holds the least row-level exclusive lock is rolled back/minimum amount of undo), so that other transactions can continue to execute.
The principle of deadlock detection is to construct a directed graph with transactions as vertices and locks as edges, and to determine whether there is a cycle in the directed graph, which means deadlock.
The disadvantage is that each new blocked thread has to determine whether it is deadlocked because of its addition, which is time-consuming.
Solutions:
1. Turn off deadlock detection.
2. Control the amount of concurrent access.For example, at most 10 threads are updating the same row at the same time, so the cost of deadlock detection is very low, and this problem will not occur.
How to avoid deadlock?
1. Set the index reasonably so that SQL can locate fewer rows through the index as much as possible.
2. Adjust the SQL execution order to avoid the SQL that holds the lock for a long time in front of the transaction.
3. Try to split large transactions into small transactions.
4. Do not explicitly lock in systems with high concurrency.
5. Lower the isolation level.
边栏推荐
猜你喜欢
随机推荐
Redis (ten) - Redission principle and practice
Software testing interview questions and answer analysis, the strongest version in 2022
MyCat中对分库分表、ER表、全局表、分片规则、全局序列等的实现与基本使用操作
LoadBalancer load balancing
3.nodejs--modularization
sublime text 3 设置
【GPU并行计算】利用OpenCL&OpenCLUtilty进行GPU并行计算
雪花是否一样问题
JUC(七):变量的线程安全分析
SQLSERVER将子查询数据合并拼接成一个字段
Awesome, Tencent technical experts handed Redis technical notes, and the download volume has exceeded 30W
Nacos服务注册与发现
(RCE)远程代码/命令执行漏洞漏洞练习
gnss rtcm rtklib Ntrip...
星光不问赶路人!武汉校区小姐姐三个月成功转行软件测试,收获9k+13薪!
SDL播放器实战
联邦学习综述(一)——联邦学习的背景、定义及价值
(六)《数电》——二极管与CMOS门电路(入门)
【无标题】
最重要的传输层









