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

1

2

3

4

5

[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

1

2

3

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.

上面有两句:

1

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

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 <a href="https://www.xiaojishu.com/tags/oppo.html" target="_blank" class="infotextkey">OPPO</a>site 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

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

来源: https://www.xiaojishu.com/db/mysql/15105.html

发现问题最近在补以前数据的时候程序突然报如下错误: 1 2 3 4 5 [2017-02-10 13:12:06.678] [INFO] mysqlLog - update tbl_playerdata_error: { [Error: ER_LOCK_DEADLOCK: Deadlock found when trying to...
Deadlock found when trying to get lock ; try restarting transaction 【 MySQL 死锁问题解决】 最近在调试接口的时候遇到了 MySQL 死锁问题,我自己测试的时候一切都好好的,但在并发下,就死锁了 其实死锁问题,并没有一个类似“万金油”的解决办法,解铃还需系铃人,能做的只有写这个代码的人自己去解决 第一次出现死锁,想必你也会和我一样整个人都是懵的,不知道如何下手,等你看过下面的文章明白了死锁,就不会害怕了.........
SequelizeDatabaseError: Deadlock found when trying to get lock ; try restarting transaction at Query.formatError (/www/server/node_modules/sequelize/lib/dialects/ mysql /query.js:244:16) 出现错误 Deadlock found when trying to get lock ; try restarting transaction。然后通过网上查找资料,重要看到有用信息了。 错误图片如下: 2解决方案 由于 mysql 执行delete操作时WHERE中字段使用了非主键,然而那个表有在进行其它操作时,就会出现这个错了。所以只要删除时使用主键作为条件即可...
重复插入相同数据导致 deadlock 问题: Deadlock found when trying to get lock ; try restarting transaction
业务逻辑:第三方登录情况下,获取到用户的实名信息。之后判断该用户在用户表中是否存在,如果不存在或非实名,那么将其实名;如果已经实名,那么不做处理,直接登录。ORM使用的是spring data jpa,用户表在mobile字段上有唯一索引idx_mobile 发现不定期的发生业务 报错 Deadlock found when trying to get lock ; try restart... Java中的连接池PoolingHttpClientConnectionManager用setMaxTotal设置了最大连接,PoolingHttpClientConnectionManager默认的 兄弟,这个不是最小连接数 Java Integer.equals()判断相等(亲测) f == g怎么可能是true java接收前台tex格式t数据_java 下载文件时,设置response.setContentType 根据文件类型(亲测) ibei023: word格式的设置response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document"); 报错 java.lang.IllegalArgumentException: The Unicode character [重] at code point [37,325] cannot be encoded as it is outside the permitted range of 0 to 255