1 前言

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

错误图片如下:

2 解决方案

由于mysql执行delete操作时WHERE 中字段使用了非主键,然而那个表有在进行其它操作时,就会出现这个错了。所以只要删除时使用主键作为条件即可。

参考文章中部分解释如下:

One easy trick that can help with most deadlocks is sorting the operations in a specific order.

You get a deadlock when two transactions are trying to lock two locks at opposite orders, ie:

connection 1: locks key(1), locks key(2);
connection 2: locks key(2), locks key(1);
If both run at the same time, connection 1 will lock key(1), connection 2 will lock key(2) and each connection will wait for the other to release the key -> deadlock. Now, if you changed your queries such that the connections would lock the keys at the same order, ie: connection 1: locks key(1), locks key(2);
connection 2: locks key(1), locks key(2);
it will be impossible to get a deadlock. So this is what I suggest:
//翻译:特别是删除语句时,确保你没有其他查询语句使用加锁,如果你必须那么删除操作,那建议查询条件按字段升序排序,然后查询出主键,最后还是按主键来删除
Make sure you have no other queries that lock access more than one key at a time except for the delete statement. if you do (and I suspect you do), order their WHERE in (k1,k2,..kn) in ascending order.
Fix your delete statement to work in ascending order:
Change DELETE FROM onlineusers WHERE datetime <= now() - INTERVAL 900 SECOND
To DELETE FROM onlineusers WHERE id IN (SELECT id FROM onlineusers
WHERE datetime <= now() - INTERVAL 900 SECOND order by id) u;
//翻译:另一件重要的事,当发现死锁时,客户端最好自动尝试3次机会,如果都失败就放弃吧,真的是失败了。
Another thing to keep in mind is that mysql documentation suggest that in case of a deadlock the client should retry automatically. you can add this logic to your client code. (Say, 3 retries on this particular error before giving up). 

3 参考

1.https://www.jb51.net/article/118468.htm(参考3)

2.https://www.cnblogs.com/duhuo/p/6386331.html

3.http://blog.sina.com.cn/s/blog_4acbd39c01014gsq.html

mysql报ERROR:Deadlock found when trying to get lock; try restarting transaction(nodejs)的更多相关文章

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

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

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

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

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

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

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

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

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

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

  7. 数据库死锁的问题,Deadlock found when trying to get lock; try restarting transaction at Query.formatError

    场景: 应用刚上线排除大批量请求的问题 线上多次出现的Deadlock found when trying to get lock错误 代码: async batchUpdate(skus, { tr ...

  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. centos环境下登录mysql报 ERROR 1045 (28000)怎么解决

    centos环境下登录mysql报 ERROR 1045 (28000)怎么解决 新入手一台虚拟机,Centos7系列的操作系统,安装mysql后,执行连接出现了Mysql ERROR 1045 (2 ...

随机推荐

  1. GreenDao设置数据版本

    GreenDao设置数据库版本增加后,会自动删除并创建新数据库,将SCHEMA_VERSION增加即可. 在3.0里可以在config配置里进行设置 apply plugin: 'com.androi ...

  2. Retrofit GreenDao开发中遇到的坑

    持续更新中1.使用@FormUrlEncoded的话,服务端需要使用Request.Form,如果不使用@FormUrlEncoded本地需要由 @FieldMap Map<String, Ob ...

  3. webpack学习笔记——打包js

    1.新建一个入口js文件,如entry.js,代码如下: document.write("It works.") 2.然后编译 entry.js 并打包到 bundle.js(会自 ...

  4. jupyter notebook出现cannot import name 'create_prompt_application'问题(Died Kernel)

    应该是在安装其它python第三方库时更新了prompt-toolkit版本,降级到下面的版本即可: sudo pip install 'prompt-toolkit==1.0.15'

  5. Linux/Windows双系统引导修复

    安装双系统建议先安装windows,然后在安装Linux,使用Linux(grub2)引导双系统 如果重新安装了windows,则无法引导进入linux,需要修复引导 在windows下安装easyB ...

  6. RHEL 6.4 通过mysql安装文件安装mysql

    参考文章:http://www.linuxidc.com/Linux/2013-12/93507.htm 1.首先下载mysql安装文件 我是下载的最新版的5.6 2. 以RPM方式安装MySQL 在 ...

  7. day 8 - 1 文件操作

    文件操作 注意: 1. r+ 最为常用 2.encoding 的编码格式一定要与文件编码格式一致 读取 r  rb #在本地创建 txt 格式的文件默认使用 gbk 格式 f = open('e:/p ...

  8. drozer的使用介绍

    0x00. 配置adb环境变量 下载地址:http://pan.baidu.com/s/1o8itZtC 密码:9o6j 如何配置android的adb环境变量 http://jingyan.baid ...

  9. i春秋 小漏洞也有大梦想

    i春秋上的课,提到了一些概念,学习并记录. 此文主要以了解概念为主,没有代码和实战. 水坑式攻击 百度定义: “水坑式攻击”,是指黑客通过分析被攻击者的网络活动规律,寻找被攻击者经常访问的网站的弱点, ...

  10. Springboot实体类转JSON报错Could not find acceptable representation & 设置访问项目根路径的默认欢迎页面

    =================实体类转JSON报错的解决办法============= 之前在springmvc的时候也报过这个错,原因以及springmvc中解决办法参考:https://www ...