MySQL error : Deadlock found when trying to get lock; try restarting transaction
在使用 MySQL 时,我们有时会遇到这样的报错:“Deadlock found when trying to get lock; try restarting transaction”。
在 14.5.5.3 How to Minimize and Handle Deadlocks 中有这样一句话:
Deadlocks are not dangerous. Just try again.
死锁不危险,重试一下就行。
实际上这个建议非常实用。
我们回顾一下死锁发生的四个条件:
- 资源的独占性(在某一时刻,最多只能被一个事务访问);
- 请求与保持(事务获取到锁后,不会主动释放);
- 不可剥夺(事务无法获取另一个事务拥有的锁);
- 循环等待(即事务 A 获取到锁 Lock1,等待 Lock2,同时事务 B 获取到锁 Lock2,等待 Lock1,此时,事务 A 和 B 循环等待对方释放各自需要的锁)。
值得注意的是,InnoDB 在绝大部分错误发生时都不会回滚(如:等待锁超时不会回滚);
只有一个例外,那就是发生死锁时,InnoDB 会回滚一个影响最小的事务(直接破坏了上面的第 3 点,这个事务就成为了 victim)。此时我们只要重试一下这个 victim 事务,那么,所有的事务都会成功提交。
同时,14.5.5.3 How to Minimize and Handle Deadlocks 中另一段话很重要:
InnoDBuses automatic row-level locking. You can get deadlocks even in the case of transactions that just insert or delete a single row. That is because these operations are not really “atomic”; they automatically set locks on the (possibly several) index records of the row inserted or deleted.
insert / delete 一行也可能引发死锁。
因为这些操作不是原子性的,它们在执行中会加 index record lock。
例子很好构造。
比如有一张表:create table t1 (id int PRIMARY KEY, name char(20) key);
有两个 session 并发:
session 1:
insert into t1 (id, name) values(5, 'Branden');
session 2:
select * from t1 where name > ‘B’ and id >3;

session1 访问 name>'B',会在 Ben、Bob上加 X 锁,并且在 Alex 与 Ben、Ben 与 Bob、Bob 与 Cathy 间加 Gap lock,在聚簇索引 id 为 1、3上加X锁;接下来要获取聚簇索引上 id 为 6、7的 X 锁,以及 (3, 6)、(6, 7)间的 gap lock。
session2 先获取聚簇索引上(3, 6)间的 gap lock;接下来尝试获取 name 索引上(Ben, Bob)、(Bob, Cathy)间的 gap lock。
最后发生死锁。
怎么消除呢?让 select 中 where 子句先访问 id ,再访问 name。
可以阅读这篇文章:http://hedengcheng.com/?p=771#_Toc374698321。
另外,有必要了解各种语句需要的锁:14.5.3 Locks Set by Different SQL Statements in InnoDB。
参考资料:
MySQL 5.7 Reference Manual:
14.5.2.3 Consistent Nonlocking Reads
Stack Overflow:
How to avoid mysql 'Deadlock found when trying to get lock; try restarting transaction'
Working around MySQL error “Deadlock found when trying to get lock; try restarting transaction”
理解innodb的锁(record,gap,Next-Key lock)
MySQL InnoDB锁机制之Gap Lock、Next-Key Lock、Record Lock解析
MySQL error : Deadlock found when trying to get lock; try restarting transaction的更多相关文章
- mysql报ERROR:Deadlock found when trying to get lock; try restarting transaction(nodejs)
1 前言 出现错误 Deadlock found when trying to get lock; try restarting transaction.然后通过网上查找资料,重要看到有用信息了. 错 ...
- Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction
我在update数据库的时候出现的死锁 数据库表死锁 Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackExcept ...
- MySql 更新死锁问题 Deadlock found when trying to get lock; try restarting transaction
文章导航-readme MySql 更新死锁问题 Deadlock found when trying to get lock; try restarting transaction 1.场景 //t ...
- mysql - InnoDB存储引擎 死锁问题( Deadlock found when trying to get lock; try restarting transaction )
刚刚向数据库插入数据的时候出现了这么一段错误 Deadlock found when trying to get lock; try restarting transaction 主要原因(由于无法使 ...
- Deadlock found when trying to get lock; try restarting transaction
1.错误描述 [ERROR:]2015-06-09 16:56:19,481 [抄送失败] org.hibernate.exception.LockAcquisitionException: erro ...
- 1213 - Deadlock found when trying to get lock; try restarting transaction
1213 - Deadlock found when trying to get lock; try restarting transaction 出现这个原因要记住一点就是:innodb的行锁 和解 ...
- mysql死锁com.mysql.cj.jdbc.exception.MYSQLTransactionRollbackException Deadlock found when trying to get lock;try restarting transaction
1.生产环境出现以下报错 该错误发生在update操作中,该表并未建立索引,也就是只有InnoDB默认的主键索引,发生错误的程序是for循环中update. 什么情况下会出现Deadlock foun ...
- 数据库死锁的问题,Deadlock found when trying to get lock; try restarting transaction at Query.formatError
场景: 应用刚上线排除大批量请求的问题 线上多次出现的Deadlock found when trying to get lock错误 代码: async batchUpdate(skus, { tr ...
- MySQL遇到Deadlock found when trying to get lock,解决方案
最近遇到一个MYSQL update语句出现Deadlock found when trying to get lock的问题,分析一下原因. 什么情况下会出现Deadlock found when ...
随机推荐
- postman(谷歌) httprequester(火狐)
http://www.cnblogs.com/s380774061/p/4624326.html @an http://www.tuicool.com/articles/67Rnaej 测试文档券栈 ...
- Hibernate -- 一对多的双向关联关系
示例代码: Customer.java package cn.itcast.many2onedouble; import java.util.HashSet; import java.util.Set ...
- java sleep()和wait()的区别
java sleep()和wait()的区别? sleep()和wait()都能阻塞当前线程. 区别1: sleep()属于Thread类:wait()属于Object类. 区别2: 调用sleep( ...
- ActiveMQ_01
http://shhyuhan.iteye.com/blog/1278103http://www.cnblogs.com/blsong/archive/2012/09/26/2704337.htmlh ...
- 编写程序时候莫名出现<property name="dialect">org.hibernate.dialect.FirebirdDialect</property>
把<propertyname="dialect">org.hibernate.dialect.FirebirdDialect</property> 语句去掉 ...
- spring mvc:复选框(多选)
以user为例,user下有 username用户,password密码, address地址, receivePaper是否订阅, favotireFramework兴趣爱好, user.java ...
- Java多线程编程总结
Java线程:概念与原理 Java线程:创建与启动 Java线程:线程栈模型与线程的变量 Java线程:线程状态的转换 Java线程:线程的同步与锁 Java线程:线程的交互 Java线程:线程的调 ...
- 命令行创建db2数据库
在cmd界面执行db2cmd命令 然后在db2cmd界面执行db2命令 然后执行 CREATE DATABASE UIBS命令 创建完成之后可以使用CONNECT TO UIBS命令连接数据库
- sed、awk学习篇
[yongsan@yz6245 ~]$ awk 'BEGIN {FS=":"}{shells[$NF]++;}END{for(i in shells)print i ": ...
- jQuery对select操作
(转自:http://www.cnblogs.com/as285996985/articles/1535014.html) //遍历option和添加.移除optionfunction changeS ...