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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql insert 锁

發布時間:2024/9/16 数据库 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql insert 锁 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

擼了今年阿里、頭條和美團的面試,我有一個重要發現.......>>>

多線程插入mysql時會發生死鎖。

數據庫test中,id1和id2是聯合主鍵

當前數據庫test內容為空。mysql引擎是:InnoDB,隔離級別為REPEATABLE-READ

?

情況一:

線程A開啟事務:

mysql> start transaction; Query OK, 0 rows affected mysql>

線程B開啟事務:

mysql> start transaction; Query OK, 0 rows affected mysql>

線程A,插入數據:

mysql> start transaction; Query OK, 0 rows affectedmysql> INSERT ignore into test values ('1', '1'),('2', '2'),('3','3'); Query OK, 3 rows affected Records: 3 Duplicates: 0 Warnings: 0mysql>

線程A沒有commit,所以數據('1', '1'),('2', '2'),('3','3')被上鎖,

線程B這時也插入數據:

mysql> start transaction; Query OK, 0 rows affectedmysql> INSERT ignore into test values ('2', '2'),('4','4'); 等待中....

因為數據('2','2')被上鎖,所以等待線程A釋放鎖,此時數據('4','4')還沒有被上鎖。線程A可以插入('4','4');

mysql> start transaction; Query OK, 0 rows affectedmysql> INSERT ignore into test values ('1', '1'),('2', '2'),('3','3'); Query OK, 3 rows affected Records: 3 Duplicates: 0 Warnings: 0mysql> INSERT ignore into test values ('5', '5'),('4', '4') ; Query OK, 2 rows affected Records: 2 Duplicates: 0 Warnings: 0mysql>

線程A提交:

mysql> start transaction; Query OK, 0 rows affectedmysql> INSERT ignore into test values ('1', '1'),('2', '2'),('3','3'); Query OK, 3 rows affected Records: 3 Duplicates: 0 Warnings: 0mysql> INSERT ignore into test values ('5', '5'),('4', '4') ; Query OK, 2 rows affected Records: 2 Duplicates: 0 Warnings: 0mysql> commit; Query OK, 0 rows affectedmysql>

線程B提交:

mysql> start transaction; Query OK, 0 rows affectedmysql> INSERT ignore into test values ('2', '2'),('4','4'); Query OK, 0 rows affected Records: 2 Duplicates: 2 Warnings: 2mysql> commit; Query OK, 0 rows affected

情況二:

線程A開啟事務:

mysql> start transaction; Query OK, 0 rows affected mysql>

線程B開啟事務:

mysql> start transaction; Query OK, 0 rows affected mysql>

線程A,插入數據:

mysql> start transaction; Query OK, 0 rows affectedmysql> INSERT ignore into test values ('1', '1'),('2', '2'),('3','3'); Query OK, 3 rows affected Records: 3 Duplicates: 0 Warnings: 0mysql>

線程A沒有commit,所以數據('1', '1'),('2', '2'),('3','3')被上鎖,

線程B這時也插入數據:

mysql> start transaction; Query OK, 0 rows affectedmysql> INSERT ignore into test values ('4', '4'),('2','2'),('5','5'); 等待中....

因為數據('2','2')被上鎖,所以等待線程A釋放鎖,此時數據('4','4')已經成功執行被上鎖。線程A插入('4','4');會造成死鎖

A線程插入('4','4'),('5','5'):

mysql> INSERT ignore into test values ('4', '4'),('6', '6') ; Query OK, 2 rows affected Records: 2 Duplicates: 0 Warnings: 0mysql>

此時B線程檢測到死鎖:

mysql> INSERT ignore into test values ('4', '4'),('2','2'),('5','5'); 1213 - Deadlock found when trying to get lock; try restarting transaction mysql>

也就是說

A線程執行insert?ignore?into?test?values ('1', '1'),('2', '2');能執行成功,會給('1', '1'),('2', '2')加鎖

B線程執行insert?ignore?into?test?values ('3', '3'),('2', '2'),('4','4');檢測('3','3')沒有加鎖,然后給('3','3')加上鎖,('2','2')檢測到鎖,被A線程鎖住,等待鎖釋放,這時('4','4')這條數據沒有加鎖。

接下來:

  • 如果A線程執行insert?ignore?into?test?values ('3', '3');時?檢測到 ('3', '3')有鎖,被B線程鎖住,互相等待釋放鎖,這時就形成了死鎖。
  • 如果A線程執行insert?ignore?into?test?values ('4', '4');時?沒檢測到('4','4')有鎖,可以正常執行。這時如果A線程commit,B線程就會提示重復數據。但是不形成死鎖。
  • ?死鎖的方向

    線程B死鎖:

    線程A執行:

    mysql> start transaction; Query OK, 0 rows affectedmysql> INSERT ignore into test values ('1', '1'),('2', '2'); Query OK, 2 rows affected Records: 2 Duplicates: 0 Warnings: 0mysql>

    線程B執行:

    mysql> start transaction; Query OK, 0 rows affectedmysql> INSERT into test values ('3', '3'),('2','2'); 等待中....

    然后線程A執行:

    mysql> INSERT ignore into test values ('3', '3'); Query OK, 1 row affectedmysql>

    這時B線程檢測到死鎖,結束等待,并且自動回滾,線程A正常執行。

    B檢測到死鎖:

    mysql> INSERT into test values ('3', '3'),('2','2'); 1213 - Deadlock found when trying to get lock; try restarting transaction mysql>

    ========

    A線程執行:

    mysql> start transaction; Query OK, 0 rows affectedmysql> INSERT ignore into test values ('1', '1'),('2', '2'),('9','9'),('8','8'); Query OK, 4 rows affected Records: 4 Duplicates: 0 Warnings: 0mysql>

    B線程執行:

    mysql> start transaction; Query OK, 0 rows affectedmysql> INSERT into test values ('3', '3'),('2','2'); 等待中

    然后A線程執行:

    mysql> INSERT ignore into test values ('3', '3'); Query OK, 1 row affectedmysql>

    B線程檢測到死鎖,停止等待,自動回滾:

    ysql> INSERT into test values ('3', '3'),('2','2'); 1213 - Deadlock found when trying to get lock; try restarting transaction mysql>

    線程A死鎖:

    線程A:

    mysql> start transaction; Query OK, 0 rows affectedmysql> INSERT ignore into test values ('1', '1'),('2', '2'); Query OK, 2 rows affected Records: 2 Duplicates: 0 Warnings: 0mysql>

    線程B:

    mysql> start transaction; Query OK, 0 rows affectedmysql> INSERT into test values ('3', '3'),('4','4'),('2','2'); 等待中

    這時A線程執行:

    mysql> INSERT ignore into test values ('3', '3'); 1213 - Deadlock found when trying to get lock; try restarting transaction mysql>

    檢測到死鎖,死鎖在A線程這邊,A線程自動回滾,B線程結束等待,正常提交

    mysql> INSERT into test values ('3', '3'),('4','4'),('2','2'); Query OK, 3 rows affected Records: 3 Duplicates: 0 Warnings: 0mysql>

    ========

    線程A執行:

    mysql> start transaction; Query OK, 0 rows affectedmysql> INSERT ignore into test values ('1', '1'),('2', '2'); Query OK, 2 rows affected Records: 2 Duplicates: 0 Warnings: 0mysql>

    線程B執行:

    mysql> start transaction; Query OK, 0 rows affectedmysql> insert into test values ('3','3'),('4','4'),('5','5'),('2','2'); Query OK, 3 rows affected Records: 3 Duplicates: 0 Warnings: 0mysql>

    這時A線程執行:

    mysql> INSERT ignore into test values ('3', '3'); 1213 - Deadlock found when trying to get lock; try restarting transaction mysql>

    ?A線程檢測到死鎖,并且自動回滾,釋放鎖,B線程結束等待。

    猜想:誰加鎖的數據少,那么誰就檢測到死鎖,就先釋放鎖。

    例如:線程A執行:

    mysql> start transaction; Query OK, 0 rows affectedmysql> insert ignore into test values ('1','1'),('2','2'); Query OK, 2 rows affected Records: 2 Duplicates: 0 Warnings: 0mysql>

    線程A加鎖數據:('1','1'),('2','2')

    線程B:

    mysql> start transaction; Query OK, 0 rows affectedmysql> insert into test values ('3','3'),('4','4'),('5','5'),('2','2'); 等待中....

    線程B加鎖數據:('3','3'),('4','4'),('5','5')

    然后A線程執行:

    mysql> insert ignore into test values ('6','6'),('7','7'),('8','8'),('9','9'),('3','3');

    此時A線程加鎖數據為:('1','1'),('2','2'),('6','6'),('7','7'),('8','8'),('9','9')

    A線程加鎖數據多于B線程,所以B線程檢測到死鎖,主動釋放鎖,

    B線程:

    mysql> insert into test values ('3','3'),('4','4'),('5','5'),('2','2'); 1213 - Deadlock found when trying to get lock; try restarting transaction mysql>

    說明誰加鎖的數據少,那么誰就檢測到死鎖,就先釋放鎖。

    與50位技術專家面對面20年技術見證,附贈技術全景圖

    總結

    以上是生活随笔為你收集整理的mysql insert 锁的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。