数据库死锁的问题,Deadlock found when trying to get lock; try restarting transaction at Query.formatError
场景:
- 应用刚上线排除大批量请求的问题
- 线上多次出现的Deadlock found when trying to get lock错误
代码:
async batchUpdate(skus, { transaction }) {
const result = await Promise.all(skus.map(async sku => {
const record = await this.app.model.Sku.upsert(sku, { transaction });
return record;
}));
// SaaS 中删掉的 sku,插件也要同步删除
const ids = _.map(skus, 'sku_id');
const productIds = _.map(skus, 'product_id');
const { Op } = this.app.Sequelize;
await this.app.model.Sku.destroy({
where: {
sku_id: { [Op.notIn]: ids },
product_id: productIds,
}, transaction,
});
return result;
};
分析:
- 报错位置都是在this.app.model.Sku.destroy的时候报错
- Deadlock found when trying to get lock的原因是多个事物同事更新插入同一表的某一段数据
- 在数据量不大的情况下,按道理说发生这种死锁的情况应该非常少但是事实上出现的概率很高
结论:
- 应该是destroy使用notIn会涉及到很多行的锁定,所以造成了死锁。但是业务上destroy删除的数据一般为0条。所以可以只在必要的时候进行destroy操作。
- 更新的时候少用或不用notIn操作
优化后代码:
async batchUpdate(skus, { transaction }) {
const result = await Promise.all(skus.map(async sku => {
const record = await this.app.model.Sku.upsert(sku, { transaction });
return record;
}));
// SaaS 中删掉的 sku,插件也要同步删除
const ids = _.map(skus, 'sku_id');
const productIds = _.map(skus, 'product_id');
const { Op } = this.app.Sequelize;
const delSkus = await this.app.model.Sku.findAll({
where: {
sku_id: { [Op.notIn]: ids },
product_id: productIds,
}, transaction,
});
if (delSkus && delSkus.length) {
await this.app.model.Sku.destroy({
where: {
sku_id: delSkus.map(sku => sku.sku_id),
}, transaction,
});
}
return result;
};
数据库死锁的问题,Deadlock found when trying to get lock; try restarting transaction at Query.formatError的更多相关文章
- mysql - InnoDB存储引擎 死锁问题( Deadlock found when trying to get lock; try restarting transaction )
刚刚向数据库插入数据的时候出现了这么一段错误 Deadlock found when trying to get lock; try restarting transaction 主要原因(由于无法使 ...
- 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 ...
- 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报ERROR:Deadlock found when trying to get lock; try restarting transaction(nodejs)
1 前言 出现错误 Deadlock found when trying to get lock; try restarting transaction.然后通过网上查找资料,重要看到有用信息了. 错 ...
- MySQL error : Deadlock found when trying to get lock; try restarting transaction
在使用 MySQL 时,我们有时会遇到这样的报错:“Deadlock found when trying to get lock; try restarting transaction”. 在 14. ...
- 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 ...
- Mybatis-update - 数据库死锁 - 获取数据库连接池等待
最近学习测试mybatis,单个增删改查都没问题,最后使用mvn test的时候发现了几个问题: update失败,原因是数据库死锁 select等待,原因是connection连接池被用光了,需要等 ...
随机推荐
- git 使用详解(3)—— 最基本命令 + .gitignore 文件
Git 基础 本章将介绍几个最基本的,也是最常用的 Git 命令,以后绝大多数时间里用到的也就是这几个命令.读完本章,你就能初始化一个新的代码仓库,做一些适当配置:开始或停止跟踪某些文件:暂存或提交某 ...
- (全国多校重现赛一)A-Big Binary Tree
You are given a complete binary tree with n nodes. The root node is numbered 1, and node x's father ...
- python学习笔记—DataFrame和Series的排序
更多大数据分析.建模等内容请关注公众号<bigdatamodeling> ################################### 排序 ################## ...
- C语言之推箱子游戏代码
前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:Yan_Less 正文 新手注意:如果你学习遇到问题找不到人解答,可以点 ...
- 浅谈C++虚函数机制
0.前言 在后端面试中语言特性的掌握直接决定面试成败,C++语言一直在增加很多新特性来提高使用者的便利性,但是每种特性都有复杂的背后实现,充分理解实现原理和设计原因,才能更好地掌握这种新特性. 只要出 ...
- Java Swing JFrame实现全屏--无标题,无边框
实现方式一: import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.JFrame; public class T ...
- node-sass下载失败
在angular项目中下载依赖npm install时提示node-sass安装失败,解决方法如下: 1.下载win32-x64-57_binding.node文件至指定目录 2.添加环境变量: 变量 ...
- ASP.NET Core开发者指南()
你可以在下面找到一张图,该图展示了你可以选取的路径及你想学习的库,从而成为一名 ASP.NET Core 开发者.“作为 ASP.NET Core 开发者,我接下来应该学习什么?”,我把这张图作为建议 ...
- CouchDB学习-维护
官方文档 1 压缩 压缩操作是通过从数据库或者视图索引文件中移除无用的和老的数据减少硬盘使用空间.操作非常简单类似于其他数据库(SQLite等)管理系统. 在压缩目标期间,CouchDB将创建扩展名为 ...
- day 28-1 元类
元类 元类的用途:自定义元类控制类的创建行为及类的实例化行为 Python 中一切皆为对象. 一切接对象,对象可以怎么用呢? 1.都可以被引用,x=obj 2.都可以当作函数的参数传入 3.都可以当作 ...