Deadlock found when trying to get lock; try restarting transaction
报错详情
Error updating database. Cause: com.mysql.cj.jdbc.exceptions.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction
The error may involve com.iss.cms.fdrb.common.dao.entity.InterfaceQueue.updateInterfaceQueue-Inline
The error occurred while setting parameters
SQL: update t_fdrb_interface_queue t set t.send_status = ?, t.update_time = ? where tenant_id = ? and biz_code= ? and biz_id = ? and send_status in (1,3)
Cause: com.mysql.cj.jdbc.exceptions.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction
; Deadlock found when trying to get lock; try restarting transaction; nested exception is com.mysql.cj.jdbc.exceptions.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:267)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
at org.mybatis.spring.SqlSessionTemplate
MySql锁类型的介绍
上面是MySql锁表的问题报错日志,今天记录一下解决方案。
- 行级锁在使用的时候并不是直接锁掉这行表记录,而是锁索引
- 如果一条Sql用到了主键索引的话,mysql会锁住这条记录主键索引
- 如果一条Sql用到了非主键索引,mysql会先锁住非主键索引,然后再锁定主键索引
原理
mysql的两情况的锁,排它锁与共享锁。
- 排它锁是事务T对数据A加上排他锁,只能允许事务T读取和修改数据A,别的事务没有办法进行读取与修改的操作处理,所以叫做排他锁,是互斥的
- 共享锁是事务T对数据A加上共享锁,其他事务只能再对数据A加上共享锁,而不能进行排它锁的操作,别的事务也可以加上共享锁。是不会进行互斥的
一般造成死锁的原因是因为两个事务添加了锁的时候没有及时进行释放锁资源,等到第二个事务要添加排他锁的时候,发现已经被锁了,从而导致的环路等待,构成死锁
问题的排查过程
日志定位可以找到具体的表是什么,然后定位这
从表里面可以发现进行更新的时候,没有用到索引,而是使用两个非索引,类似聚合索引的方式进行更新的处理。
问题的解决方案
针对这种情况,需要进行调整,先查询出来这一条记录,然后根据主键来进行更新的操作即可。
调整前的sql
update t_fdrb_interface_queue${tableNo} t set
t.send_status = #{sendStatus},
t.update_time = #{updateTime}
where tenant_id = #{tenantId} and biz_code= #{bizCode} and biz_id = #{bizId} and send_status in (1,3)
调整后的sql
第一步:查询
select
t.biz_code as bizCode,
t.biz_id as bizId,
t.send_status as sendStatus,
t.create_time as createTime,
t.update_time as updateTime,
t.send_num as sendNum,
t.send_Lang as sendLang,
t.id as id
from t_fdrb_interface_queue${tableNo} t
where t.tenant_id = #{tenantId} and t.biz_code= #{bizCode} and biz_id = #{bizId} and send_status in (1,3)
第二步:更新
update t_fdrb_interface_queue${tableNo} t set
t.send_status = #{sendStatus},
t.update_time = #{updateTime}
where id= #{id}
这里备注一下,后续在操作更新的操作时,要使用索引进行更新,避免死锁的情况
Deadlock found when trying to get lock; try restarting transaction相关推荐
- Deadlock found when trying to get lock; try restarting transaction主要要是死锁问题呢怎么解决
目录 问题描述: 问题发生原因: 问题日志详情: 问题描述: 生产启动的时候,定期排查的 一些日志,发现一个问题呢,主要是加锁了,很奇怪,主要是业务数据的一般的CURD.至于其他的很奇怪!主要查看业务 ...
- mysql事物sql语句死锁,定时任务启动失败Lock wait timeout exceeded;try restarting transaction
把定时任务quartz配置mysql里多台机器启动时可能会出现数据库死锁,然后控制台报错: Lock wait timeout exceeded:try restarting transaction ...
- Lock wait timeout exceeded; try restarting transaction解决
前言 服务报错: Lock wait timeout exceeded; try restarting transaction 排查 字面意思 锁等待超时了,尝试重启事务. 既然是数据库死锁,着手排查 ...
- mysql 死锁问题 Lock wait timeout exceeded; try restarting transaction
Lock wait timeout exceeded; try restarting transaction mysql出现死锁并获取锁超时后,会出现上面的错误. 解决步骤: 我们可以通过到infor ...
- [Err] 1205 - Lock wait timeout exceeded; try restarting transaction Mysql 报错
这个错误是由于当前操作的记录存在于数据库中未结束的事务导致行锁定. 简单说,就是现在要对一条记录进行修改,那么sql语句应该是这样的: update user set uname = 'zhangsa ...
- mysql线上问题之Lock wait timeout exceeded; try restarting transaction
本文来说下mysql线上问题之Lock wait timeout exceeded; try restarting transaction 文章目录 问题描述 解决方案 三张表字段说明 本文小结 问题 ...
- MySQL抛出 Lock wait timeout exceeded; try restarting transaction
问题描述:在MySQL中使用手动提交事务时,出现了Lock wait timeout exceeded; try restarting transaction 错误 解决方法: LOCK WAIT t ...
- 解决mysql报Lock wait timeout exceeded; try restarting transaction的问题
解决Lock wait timeout exceeded; try restarting transaction的问题 在idea调试发现一直执行不下去,于是把sql语句放到mysql执行,出现请求锁 ...
- [1205] [40001]: Lock wait timeout exceeded; try restarting transaction
该情况大概率是数据被锁.解决办法: 1.查看当前是否有未结束的事务 select * from information_schema.innodb_trx; 2.执行结果,trx _tables_lo ...
最新文章
- 【C++】C++虚函数表详细分析(下)
- 流水线调度(51Nod-1205)
- linux tomcat 发布servlet,SpringBoot项目使用war包部署至云服务器(Linux+Tomcat)
- linux c++开发_Linux/Windows下进行C/C++开发的差异
- gravity与Layout_gravity的区别
- bzoj5312 冒险(吉司机线段树)题解
- 2015 CCPC 这次,我为自己鼓掌
- Mybatis系列(四)注解
- 4选1选择器(第一天)
- 紫外线杀菌器:Photoscience紫外线杀菌器在食品饮料中的作用
- 搭建 LimeSurvey投票调查问卷系统
- 探索性测试方法讲解之一
- OSError: [Errno 28] inotify watch limit reached
- 为什么越来越多的人选择了企业微信
- Techwiz LCD 1D:SRF的颜色分析
- .NET操作Excel高效低内存的开源框架 - MiniExcel
- C++在线编辑器:cpp.sh
- Microbiome:环境过滤驱动农田生态系统土壤古菌独特的空间分布
- Kids Photography: Newborns 儿童摄影教程:新生儿 Lynda课程中文字幕
- 降维算法PCA的应用----高维数据的可视化