• 场景:

    • 应用刚上线排除大批量请求的问题
    • 线上多次出现的Deadlock found when trying to get lock错误

  • 代码:

  1. async batchUpdate(skus, { transaction }) {
  2. const result = await Promise.all(skus.map(async sku => {
  3. const record = await this.app.model.Sku.upsert(sku, { transaction });
  4. return record;
  5. }));
  6. // SaaS 中删掉的 sku,插件也要同步删除
  7. const ids = _.map(skus, 'sku_id');
  8. const productIds = _.map(skus, 'product_id');
  9. const { Op } = this.app.Sequelize;
  10. await this.app.model.Sku.destroy({
  11. where: {
  12. sku_id: { [Op.notIn]: ids },
  13. product_id: productIds,
  14. }, transaction,
  15. });
  16. return result;
  17. };
  • 分析:

    • 报错位置都是在this.app.model.Sku.destroy的时候报错
    • Deadlock found when trying to get lock的原因是多个事物同事更新插入同一表的某一段数据
    • 在数据量不大的情况下,按道理说发生这种死锁的情况应该非常少但是事实上出现的概率很高
  • 结论:

    • 应该是destroy使用notIn会涉及到很多行的锁定,所以造成了死锁。但是业务上destroy删除的数据一般为0条。所以可以只在必要的时候进行destroy操作。
    • 更新的时候少用或不用notIn操作
  • 优化后代码:

  1. async batchUpdate(skus, { transaction }) {
  2. const result = await Promise.all(skus.map(async sku => {
  3. const record = await this.app.model.Sku.upsert(sku, { transaction });
  4. return record;
  5. }));
  6. // SaaS 中删掉的 sku,插件也要同步删除
  7. const ids = _.map(skus, 'sku_id');
  8. const productIds = _.map(skus, 'product_id');
  9. const { Op } = this.app.Sequelize;
  10. const delSkus = await this.app.model.Sku.findAll({
  11. where: {
  12. sku_id: { [Op.notIn]: ids },
  13. product_id: productIds,
  14. }, transaction,
  15. });
  16. if (delSkus && delSkus.length) {
  17. await this.app.model.Sku.destroy({
  18. where: {
  19. sku_id: delSkus.map(sku => sku.sku_id),
  20. }, transaction,
  21. });
  22. }
  23. return result;
  24. };

数据库死锁的问题,Deadlock found when trying to get lock; try restarting transaction at Query.formatError的更多相关文章

  1. mysql - InnoDB存储引擎 死锁问题( Deadlock found when trying to get lock; try restarting transaction )

    刚刚向数据库插入数据的时候出现了这么一段错误 Deadlock found when trying to get lock; try restarting transaction 主要原因(由于无法使 ...

  2. 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 ...

  3. 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 ...

  4. mysql报ERROR:Deadlock found when trying to get lock; try restarting transaction(nodejs)

    1 前言 出现错误 Deadlock found when trying to get lock; try restarting transaction.然后通过网上查找资料,重要看到有用信息了. 错 ...

  5. MySQL error : Deadlock found when trying to get lock; try restarting transaction

    在使用 MySQL 时,我们有时会遇到这样的报错:“Deadlock found when trying to get lock; try restarting transaction”. 在 14. ...

  6. Deadlock found when trying to get lock; try restarting transaction

    1.错误描述 [ERROR:]2015-06-09 16:56:19,481 [抄送失败] org.hibernate.exception.LockAcquisitionException: erro ...

  7. 1213 - Deadlock found when trying to get lock; try restarting transaction

    1213 - Deadlock found when trying to get lock; try restarting transaction 出现这个原因要记住一点就是:innodb的行锁 和解 ...

  8. mysql死锁com.mysql.cj.jdbc.exception.MYSQLTransactionRollbackException Deadlock found when trying to get lock;try restarting transaction

    1.生产环境出现以下报错 该错误发生在update操作中,该表并未建立索引,也就是只有InnoDB默认的主键索引,发生错误的程序是for循环中update. 什么情况下会出现Deadlock foun ...

  9. Mybatis-update - 数据库死锁 - 获取数据库连接池等待

    最近学习测试mybatis,单个增删改查都没问题,最后使用mvn test的时候发现了几个问题: update失败,原因是数据库死锁 select等待,原因是connection连接池被用光了,需要等 ...

随机推荐

  1. markdown语法之字体、字号、颜色以及背景色

    字体.字号与颜色 html标签:<font> font标签属性: face:字体 size:规定文本的尺寸大小. 可能的值:从 1 到 7 的数字. 浏览器默认值是 3. color: 颜 ...

  2. go学习第三天、数据类型

    基本数据类型 bool string int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 uintptr byte // alias ...

  3. POJ 1949 Chores

    Farmer John's family pitches in with the chores during milking, doing all the chores as quickly as p ...

  4. Selenium之ActionChains类、Keys类

    ActionChains类(鼠标操作)常用于模拟鼠标的行为,比如单击.双击.拖拽等行为. 一些常用的模拟鼠标的操作方法有: click(on_element=None)     --- 鼠标单击 do ...

  5. USB摄像头之130w像素 OV9655配置,ov9650,ov7725,ov7670

    USB摄像头之130w像素 OV9655配置 为了usb2.0采集达到足够的速率,不得不将采用raw格式输出. // 20150411 XVGA 1280*1024 实际上位机需要2560*1024 ...

  6. 改变SecureCRT的背景颜色

    1.在使用secureCRT客户端时,可以连接服务器,默认为白色底. 2.要进行对把底色的白色改为黑色的底色,右击的窗口的位置. 3.下拉菜单中点击 Session Options 4.点击Appea ...

  7. JS实现链式调用 a().b().c()

    function a() { this.b = function () { console.log('111') return this } this.c = function () { consol ...

  8. AI-Azure上的认知服务之Computer Vision(计算机视觉)

    使用 Azure 的计算机视觉服务,开发人员可以访问用于处理图像并返回信息的高级算法. 主要包含如下高级算法: 标记视觉特性Tag visual features 检测对象Detect objects ...

  9. 在Windows10中安装与配置Nginx

    一.使用环境: Windows 10 1909 nginx 1.16.1 二.安装与配置: 1. 打开链接,http://nginx.org/en/download.html 下载稳定版本nginx ...

  10. FCC---CSS Flexbox: Align Elements Using the justify-content Property

    Sometimes the flex items within a flex container do not fill all the space in the container. It is c ...