mysql报错:Deadlock found when trying to get lock; try restarting transaction的解决方法

发现问题

最近在补以前数据的时候程序突然报如下错误:

[2017-02-10 13:12:06.678] [INFO] mysqlLog - update tbl_playerdata_error: { [Error: ER_LOCK_DEADLOCK: Deadlock found when trying to get lock; try restarting transaction]
 code: 'ER_LOCK_DEADLOCK',
 errno: 1213,
 sqlState: '40001',
 index: 0 }

一看就是mysql出现了死锁问题,其实上面跑的程序在测试服跑了好久都没什么问题,为什么在正式服上会出现mysql的死锁问题呢,第一反应是不是数据量太大(3百多万条),可是也不可能啊,再说死锁和这些有什么鸡毛的关系,看来要好好解决下了。

问题分析

我的分析是:由于现在处理的是正式服的数据,而正式服还有许多用户在操作,应该是在用户查询,或者是其他操作的时候,和我这边的数据更新产生了死锁(首先说明使用的是:InnoDB存储引擎。由于用户那边的查询或者其他操作锁定了我需要的资源,而我这边更新也锁定了用户操作的一部分资源,两边都等着对方释放资源,从而导致死锁)。

解决方法

知道错误code之后,先来查看mysql的说明,关于上面的 Error: 1213 SQLSTATE: 40001,参见:Server Error Codes and Messages

Message: Deadlock found when trying to get lock; try restarting transaction

InnoDB reports this error when a transaction encounters a deadlock and is automatically rolled back so that your application can take corrective action. To recover from this error, run all the operations in this transaction again. A deadlock occurs when requests for locks arrive in inconsistent order between transactions. The transaction that was rolled back released all its locks, and the other transaction can now get all the locks it requested. Thus, when you re-run the transaction that was rolled back, it might have to wait for other transactions to complete, but typically the deadlock does not recur. If you encounter frequent deadlocks, make the sequence of locking operations (LOCK TABLES, SELECT ... FOR UPDATE, and so on) consistent between the different transactions or applications that experience the issue. See Section 14.8.5, “Deadlocks in InnoDB” for details.

上面有两句:

To recover from this error, run all the operations in this transaction again<br><br>If you encounter frequent deadlocks, make the sequence of locking operations (<code class="literal">LOCK TABLES</code>, <code class="literal">SELECT ... FOR UPDATE</code>, and so on) <br>consistent between the different transactions or applications that experience the issue 

这两句也就道出了处理死锁的方法了,我就是在死锁错误发生的时候,使用定时器再重新做一次更新操作,这样就避免了上面出现的问题。

另外,参考了stack overflow上面一个回答:http://stackoverflow.com/questions/2332768/how-to-avoid-mysql-deadlock-found-when-trying-to-get-lock-try-restarting-trans

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;
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).

参考:http://blog.sina.com.cn/s/blog_4acbd39c01014gsq.html

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对我们的支持。

(0)

相关推荐

  • mysql 数据库死锁原因及解决办法

    死锁(Deadlock) 所谓死锁:是指两个或两个以上的进程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统产生了死锁,这些永远在互相等待的进程称为死锁进程.由于资源占用是互斥的,当某个进程提出申请资源后,使得有关进程在无外力协助下,永远分配不到必需的资源而无法继续运行,这就产生了一种特殊现象死锁. 一种情形,此时执行程序中两个或多个线程发生永久堵塞(等待),每个线程都在等待被其他线程占用并堵塞了的资源.例如,如果线程A锁住了记

  • MySQL死锁问题分析及解决方法实例详解

    MySQL死锁问题是很多程序员在项目开发中常遇到的问题,现就MySQL死锁及解决方法详解如下: 1.MySQL常用存储引擎的锁机制 MyISAM和MEMORY采用表级锁(table-level locking) BDB采用页面锁(page-level locking)或表级锁,默认为页面锁 InnoDB支持行级锁(row-level locking)和表级锁,默认为行级锁 2.各种锁特点 表级锁:开销小,加锁快;不会出现死锁;锁定粒度大,发生锁冲突的概率最高,并发度最低 行级锁:开销大,加锁慢;

  • mysql报错:Deadlock found when trying to get lock; try restarting transaction的解决方法

    发现问题 最近在补以前数据的时候程序突然报如下错误: [2017-02-10 13:12:06.678] [INFO] mysqlLog - update tbl_playerdata_error: { [Error: ER_LOCK_DEADLOCK: Deadlock found when trying to get lock; try restarting transaction] code: 'ER_LOCK_DEADLOCK', errno: 1213, sqlState: '4000

  • Linux连接mysql报错:Access denied for user ‘root’@‘localhost’(using password: YES)的解决方法

    前言 最近在linux连接mysql /usr/local/mysql/bin/mysql -uroot -p 输入密码出现Access denied for user 'root'@'localhost'(using password: YES)错误.下面话不多说了,来一起看看详细的解决方法吧 解决办法: 1.先停止mysql 服务 service mysqld stop 2.进入mysql安装目录bin/ 使用safe模式,进行重启: ./mysqld_safe --skip-grant-t

  • Spring Boot报错:No session repository could be auto-configured, check your configuration的解决方法

    本文主要跟大家分享了关于Spring Boot报错:No session repository could be auto-configured, check your configuration的解决方法,下面话不多说,来一起看看详细的介绍: 一.环境介绍 JDK 1.8  spring-Boot 1.5.1.RELEASE, STS IDE 二. 问题的提出 创建了一个非常简约的Spring Boot Web Application,其中使用了Spring-Session,具体的maven依

  • Tomcat ssl报错Connector attribute SSLCertificateFile must be defined when using SSL with APR解决方法

    今天同事要求帮忙配置tomcat ssl,直接把linux下tomcat配置ssl这篇文章发给他了,没想到他居然说启动tomcat的时候,报Connector attribute SSLCertificateFile must be defined when using SSL with APR的错误,马上跑过去看,原来他的tomcat版本是7.0的,我发给他的是tomcat6的,检查了key,检查了配置文件,因为他直接复制的我发他文章的配置,折腾了2-3个小时,终于把问题给解决了. 系统:wi

  • mysql 报错This function has none of DETERMINISTIC解决方案

    本文章向朋友们介绍开启bin-log日志mysql报错:This function has none of DETERMINISTIC, NO SQL解决办法, 创建存储过程时 出错信息: ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want t

  • mysql报错:MySQL server version for the right syntax to use near type=InnoDB的解决方法

    本文实例讲述了mysql报错:MySQL server version for the right syntax to use near type=InnoDB的解决方法.分享给大家供大家参考,具体如下: 一.问题: 工作中使用sql语句建表时,mysql报了如下错误: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right

  • IDEA连接mysql报错的问题及解决方法

    IDEA连接mysql报错了! 本人之前使用MySQL以及后续使用mybatis和mybatisPlus都是使用sqlyog或者navicat,今天重新学习sql,看到up使用了,idea插件来连接数据库(mysql)也来学习使用了,结果出现一系类问题.本博客开始记录. ---------------------------前进的道路充满荆棘.--------------------------------------------------------------------------- 错

  • IDEA链接MySQL报错08001和连接成功后不显示表的问题及解决方法

    报错Connection to blog0@localhost failed. [08001] Could not create connection to database server. Attempted reconnect 3 times. Giving up. 解决办法:在url后面拼接?serverTimezone=UTC 连接成功后数据库不显示表 通过设置解决: scheme选择当前使用的数据库 到此这篇关于IDEA链接MySQL报错08001和连接成功后不显示表的问题及解决方法的

  • 解决navicat远程连接mysql报错10038的问题

    navicat远程连接mysql报错10038一般由以下两个原因: 一:本地防火墙问题 在本地安装了mysql.navicat并打开了mysql服务的情况下,来设置防火墙. 首先右击或者点击入站规则,找到新建规则,点击. 点击端口. 在特定本地端口中填入3306. 一直点击下一步. 这里可以给一个好分别的名称即可. 之前再尝试连接即可,若仍然不可以,可能是服务器方面的问题. 二:服务器3306端口未打开 首先需要在安全组开放端口. 我这里使用的是阿里云服务器.首先需要进入云服务器,找到安全组.

  • MySQL报错1040'Too many connections'的原因以及解决方案

    目录 报错原因: 解决办法 总结 MySQL 报错1040 ‘Too many connections’ 报错原因: 实际连接数超过了mysql 允许的最大连接数,访问量过高,MySQL服务器抗不住. 解决办法 1.修改max_connections,如果这个值已经很大,2.这个时候就要考虑增加从服务器分散读压力: Windows 找到mysql.ini(Linux 修改/etc/my.cnf文件,在[mysqld]中新增max_connections=N).修改允许最大连接数max_conne

随机推荐