日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql报错:Deadlock found when trying to get lock;

發(fā)布時(shí)間:2024/9/20 数据库 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql报错:Deadlock found when trying to get lock; 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

發(fā)現(xiàn)問題

最近在補(bǔ)以前數(shù)據(jù)的時(shí)候程序突然報(bào)如下錯(cuò)誤:

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出現(xiàn)了死鎖問題,其實(shí)上面跑的程序在測試服跑了好久都沒什么問題,為什么在正式服上會(huì)出現(xiàn)mysql的死鎖問題呢,第一反應(yīng)是不是數(shù)據(jù)量太大(3百多萬條),可是也不可能啊,再說死鎖和這些有什么雞毛的關(guān)系,看來要好好解決下了。

問題分析

我的分析是:由于現(xiàn)在處理的是正式服的數(shù)據(jù),而正式服還有許多用戶在操作,應(yīng)該是在用戶查詢,或者是其他操作的時(shí)候,和我這邊的數(shù)據(jù)更新產(chǎn)生了死鎖(首先說明使用的是:InnoDB存儲引擎。由于用戶那邊的查詢或者其他操作鎖定了我需要的資源,而我這邊更新也鎖定了用戶操作的一部分資源,兩邊都等著對方釋放資源,從而導(dǎo)致死鎖)。

解決方法

知道錯(cuò)誤code之后,先來查看mysql的說明,關(guān)于上面的 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

這兩句也就道出了處理死鎖的方法了,我就是在死鎖錯(cuò)誤發(fā)生的時(shí)候,使用定時(shí)器再重新做一次更新操作,這樣就避免了上面出現(xiàn)的問題。

另外,參考了stack overflow上面一個(gè)回答: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

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對小技術(shù)網(wǎng)的支持。

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

總結(jié)

以上是生活随笔為你收集整理的mysql报错:Deadlock found when trying to get lock;的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 免费观看视频在线观看 | 亚洲AV午夜福利精品一级无码 | 国产一区一一区高清不卡 | 91片黄在线观 | 国产5区| 丰满少妇大力进入 | 精品人妻伦一二三区免费 | 懂色av中文字幕 | 萌白酱喷水视频 | 成人性生生活性生交全黄 | 成人免费做受小说 | 亚洲精品7777 | 91精品国产一区二区三竹菊影视 | 精品人伦一区二区三区蜜桃免费 | 免费黄色网址在线 | 久久无码人妻丰满熟妇区毛片 | 日本中文字幕视频在线 | 美女诱惑av | 久草福利资源在线观看 | 国产精品露脸视频 | 黄色av免费在线 | com国产| 永久av| 欧美a一级| 无码国产精品一区二区免费16 | 澳门超碰| 韩国国产在线 | 日韩在线不卡视频 | 亚洲黄色一区二区 | 9999热视频 | 欧美成人福利视频 | 黄色片免费看 | 青青五月天 | 久久久久亚洲av无码专区首jn | 国产午夜电影在线观看 | 国产九色av | 国产亚洲精品成人av在线 | 中文字幕一区二区三区免费 | 国产大学生视频 | 天堂中文在线8 | 日韩人妻无码一区二区三区 | 一区二区网站 | 网址你懂的在线 | 99国产精品99 | 国产精品一区二区免费视频 | 国产嫩草在线观看 | 国产精品视频导航 | 国产精品178页 | 男生女生操操操 | 午夜视频在线免费播放 | 丰满肉嫩西川结衣av | 日韩av在线一区二区三区 | 欧美人交a欧美精品 | 五月天激情小说 | 国内毛片毛片 | 欧美一区二区三区免 | 欧美成人极品 | 九九五月天 | 在线成人福利 | 国产精品人人妻人人爽人人牛 | 一级黄大片 | 国产剧情精品在线 | 色乱码一区二区三区网站 | 亚洲色图欧美激情 | av午夜在线 | 国产日韩欧美不卡 | 久久精品欧美一区二区 | 色噜噜综合网 | 偷啪自啪 | 国产精品人妻一区二区三区 | 91欧美一区二区 | 国产性猛交╳xxx乱大交 | 免费人成年激情视频在线观看 | 亚洲精品自拍视频 | 六月婷婷激情 | 色丁香婷婷| 午夜天堂 | av观看国产| 国产精品一二三区视频 | 欧美国产中文字幕 | 在线观看免费中文字幕 | 麻豆亚洲av熟女国产一区二 | 在线免费精品视频 | 九九午夜| 久久人人添人人爽添人人片 | 先锋资源网av | 欧美国产专区 | 在线播放免费av | 美女视频网址 | 免费黄色看片 | 女上男下动态图 | 国产无套精品一区二区 | 欧美色亚洲 | 久久久久久久久国产精品 | 亚洲成人免费av | 人妻av无码一区二区三区 | 国产顶级毛片 | 久久亚洲中文字幕无码 | 欧美视频直播网站 |