日韩性视频-久久久蜜桃-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ò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 噜噜噜色 | 国产午夜精品久久久久久久久久 | 亚洲精品久 | 国产乱free国语对白 | 欧美成人看片黄a免费看 | 成人涩涩视频 | 五月天久久久久久 | 加勒比hezyo黑人专区 | 欧美日韩欧美日韩在线观看视频 | 亚洲精品一区二区三区蜜臀 | 东京热一区二区三区四区 | 日韩精品视频免费播放 | 午夜精品久久久久久久99热黄桃 | 95精品视频 | 欧美性受xxxx狂喷水 | 黄色大全在线观看 | 成人av在线播放网站 | 天天躁日日躁bbbbb | 一卡二卡三卡在线观看 | 三级a做爰全过程 | 热99视频 | a毛片在线| 欧美日韩精品一区 | 日韩欧美亚洲国产精品字幕久久久 | 精品国产人妻一区二区三区 | 狂躁美女大bbbbbb黑人 | 国产精品3p视频 | 久久精品2019中文字幕 | 亚洲av无码一区二区三区在线播放 | 欧美色炮| 久草不卡| 国产aⅴ激情无码久久久无码 | 非洲黑人狂躁日本妞 | 性插视频在线观看 | 中文字幕久久网 | 亚洲AV无码成人精品区在线观 | 婷婷影音 | 开心六月婷婷 | www.精品视频 | 波多野吉衣在线视频 | 床戏高潮做进去大尺度视频 | 久久久久人妻一区精品色欧美 | 999zyz玖玖资源站永久 | 国产精品久久在线观看 | 国产精品99精品久久免费 | av导航网站| 久久伊人中文字幕 | 日本少妇全体裸体洗澡 | 麻豆福利视频 | 亚洲美女精品视频 | 久久久久久久久久福利 | 经典av在线| 久久久久亚洲AV成人无在 | 免费福利影院 | 日韩欧美亚洲精品 | 女人被男人躁得好爽免费视频 | 精品国产av色一区二区深夜久久 | 欧美一区二区三区四区在线观看 | 伊人亚洲精品 | 韩国成人理伦片免费播放 | 巨茎大战刘亦菲 | 狠狠干2020 | 国产大片中文字幕在线观看 | 欧美三级a做爰在线观看 | 天堂成人在线视频 | 久久久美女视频 | 精品视频一区二区在线观看 | 肥臀浪妇太爽了快点再快点 | 国产精品成人国产乱 | 秋霞影院av | 综合在线播放 | 国产精品亚洲一区二区三区在线观看 | 久久麻豆精品 | 91 在线观看| 免费麻豆av| 五月天婷婷在线观看 | 欧美黑人又粗又大又爽免费 | 欧美视频网站 | 国产色 | 中文字幕在线2018 | 欧美精品一区三区 | www.日韩在线| 91视频在线观看视频 | 男裸体无遮挡网站 | 欧美成人精品一区二区三区 | 国产a不卡 | 伊人av一区 | 午夜亚洲av永久无码精品 | 国产高清视频在线 | 亚洲视频二 | 中文字幕在线观看一区二区三区 | 国产探花一区二区三区 | 久久爱影视i | 黄色av免费网站 | 亚洲少妇激情 | 91爱爱视频 | 初尝黑人巨炮波多野结衣 | 久久爱网| 亚洲精品资源在线 |