数据库死锁的问题,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连接池被用光了,需要等 ...
随机推荐
- 关于<form> autocomplete 属性
基本知识 值 描述 on 默认.规定启用自动完成功能. off 规定禁用自动完成功能. autocomplete 属性规定表单是否应该启用自动完成功能. 自动完成允许浏览器预测对字段的输入.当用户在字 ...
- MyBatis_多表关联查询_resultMap_单个对象_N+1方式实现
mapper 层 提供 StudentMapper 和 ClazzMapper, StudentMapper 查询所有学生信息, ClazzMapper 根据编号查询班级信息. 再 StudentMa ...
- [TimLinux] django model关于QuerySet
1. 获取执行过的sql命令 from django.db import connections connections['default'].queries 2. 获取QuerySet将执行的sql ...
- 洛谷 SPOJ 题解 SP1 【TEST - Life, the Universe, and Everything】
给出一种主函数递归的方法(其实主函数 main() 也是可以递归的) #include <stdio.h> int main() { int a; scanf("%d" ...
- ARTS-S docker安装miniconda
FROM centos:centos7.3.1611 MAINTAINER zhouyang3 <aaa@qq.com> WORKDIR /usr/local ADD ./ /usr/lo ...
- unity3d WeelCollider 漂移
物理漂移 基础控制不在说明 Forward Friction 为轮胎直线摩擦力 Sideways Friction 为侧面摩擦力 Extremum Slip为速度达到多少后产生漂移效果 Extremu ...
- 【Webpack】315- 手把手教你搭建基于 webpack4 的 vue2 多页应用
背景 前司和现司都会存在这种业务场景:有很多 H5 页面是不相关的,如果使用 SPA 的话,对于很多落地页和活动页不太友好,有一些纯前端页面加载过慢,所以就萌生了创建一个多页面 MPA 的框架. 起初 ...
- 【Hybrid】288- Hybrid App 应用开发中 9 个必备知识点复习
前言 我们大前端团队内部 ?每周一练 的知识复习计划继续加油,本篇文章是 <Hybrid APP 混合应用专题> 主题的第二期和第三期的合集. 这一期共整理了 10 个问题,和相应的参考答 ...
- kubernetes-概念
kubernetes内部组件工作原理 http://dockone.io/article/5108 一.Master Master是整个集群的控制中心,kubernetes的所有控制指令都是发 ...
- 小白学 Python 爬虫(23):解析库 pyquery 入门
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...