溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MySQL在RR隔離級別下的unique失效和死鎖模擬

發(fā)布時間:2020-08-10 09:53:44 來源:ITPUB博客 閱讀:204 作者:jeanron100 欄目:MySQL數(shù)據(jù)庫

今天在測試MySQL事務(wù)隔離級別的時候,發(fā)現(xiàn)了一個有趣的問題,也參考了楊一之前總結(jié)的一篇。http://blog.itpub.net/22664653/viewspace-1612574/

    問題的背景是在MySQL隔離級別為RR(Repeatable Read)時,唯一性約束沒有失效,多并發(fā)的場景下能夠復(fù)現(xiàn)出下面的問題。

  這樣一個看起來不可能的事情,能否復(fù)現(xiàn)呢。

MySQL在RR隔離級別下的unique失效和死鎖模擬

  我都這么問了,潛臺詞就是可以,要不今天的筆記就一個問題就結(jié)束了。

  為了模擬這個問題,我們打開兩個會話窗口,來模擬一下這個問題。

mysql> create table test3(id1 int primary key,id2 int unique,id3 int);
Query OK, 0 rows affected (0.01 sec)

#會話1
set autocommit=0;
mysql> insert into test3 values(1,20170831,1);
Query OK, 1 row affected (0.00 sec)
commit;


#會話2

這個時候充分利用了MVCC的特性,這是一個快照讀。

mysql> select *from test3;
+-----+----------+------+
| id1 | id2      | id3  |
+-----+----------+------+
|   1 | 20170831 |    1 |
+-----+----------+------+
1 row in set (0.00 sec)
會話1插入了一條數(shù)據(jù),我們在會話2中刪除。
mysql> delete from test3 where id1=1;
Query OK, 1 row affected (0.01 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
提交之后,會話2中就修改完畢了。

#會話1

這個時候根據(jù)MVCC的特點,會話2中已經(jīng)刪除了id1=1的記錄。所以主鍵列相關(guān)數(shù)據(jù)是插入不了了,那么唯一性索引呢。根據(jù)MVCC的特點,能夠保證重復(fù)讀的特點,讀到的數(shù)據(jù)還是不變。

mysql> select *from test3;
+-----+----------+------+
| id1 | id2      | id3  |
+-----+----------+------+
|   1 | 20170831 |    1 |
+-----+----------+------+
1 row in set (0.00 sec)

現(xiàn)在的關(guān)鍵就來了,我們插入一條數(shù)據(jù),主鍵不沖突,唯一性索引沖突,看看是否能夠插入成功。

mysql> insert into test3 values(2,20170831,2);
Query OK, 1 row affected (0.00 sec)

魔性的一幕上演了。

mysql> select *from test3;
+-----+----------+------+
| id1 | id2      | id3  |
+-----+----------+------+
|   1 | 20170831 |    1 |
|   2 | 20170831 |    2 |
+-----+----------+------+
2 rows in set (0.00 sec)

當然到了這里,我們繼續(xù)玩一玩,常規(guī)來說,插入主鍵列沖突數(shù)據(jù)可能是行不通的,比如id1=1,id2=20170831,id3=1,客戶端很快會反饋失敗。但是在這里做唯一性校驗時,因為id1=1的數(shù)據(jù)已經(jīng)被物理刪除了。

mysql>  insert into test3 values(1,20170831,1);
ERROR 1062 (23000): Duplicate entry '20170831' for key 'id2'


我們就來繼續(xù)模擬個死鎖吧。

會話2:

這個步驟是做一次數(shù)據(jù)清理,where條件中是根據(jù)主鍵來查找刪除。

mysql> delete from test3 where id1=1;
Query OK, 0 rows affected (0.00 sec)


會話1:

mysql> insert into test3 values(1,20170831,1);

這個時候會話會被阻塞


會話2:

這個時候在會話2繼續(xù)插入這個值,就會報出死鎖問題。

mysql> insert into test3 values(1,20170831,1);
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction

--產(chǎn)生死鎖


會話1:

這個時候死鎖有了,事務(wù)也自動回滾了。再次插入違反約束的數(shù)據(jù),就不行了。

mysql> insert into test3 values(1,20170831,1);
ERROR 1062 (23000): Duplicate entry '20170831' for key 'id2'


我們來看看在上面的測試過程中,關(guān)于死鎖的日志:
2017-08-28T07:27:48.329631Z 14140 [Note] InnoDB: Transactions deadlock detected, dumping detailed information.
2017-08-28T07:27:48.329740Z 14140 [Note] InnoDB:
*** (1) TRANSACTION:

TRANSACTION 31790, ACTIVE 315 sec inserting
mysql tables in use 1, locked 1
LOCK WAIT 5 lock struct(s), heap size 1136, 5 row lock(s), undo log entries 1
MySQL thread id 14138, OS thread handle 139809903986432, query id 108686 localhost root update
insert into test3 values(1,20170831,1)
2017-08-28T07:27:48.329801Z 14140 [Note] InnoDB: *** (1) WAITING FOR THIS LOCK TO BE GRANTED:

RECORD LOCKS space id 36 page no 3 n bits 72 index PRIMARY of table `test`.`test3` trx id 31790 lock mode S waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 5; compact format; info bits 0
 0: len 4; hex 80000001; asc     ;;
 1: len 6; hex 000000007c2f; asc     |/;;
 2: len 7; hex 33000001ac2f63; asc 3    /c;;
 3: len 4; hex 8133c84f; asc  3 O;;
 4: len 4; hex 80000001; asc     ;;

2017-08-28T07:27:48.330040Z 14140 [Note] InnoDB: *** (2) TRANSACTION:

TRANSACTION 31791, ACTIVE 51 sec inserting
mysql tables in use 1, locked 1
5 lock struct(s), heap size 1136, 4 row lock(s), undo log entries 1
MySQL thread id 14140, OS thread handle 139809903720192, query id 108687 localhost root update
insert into test3 values(1,20170831,1)
2017-08-28T07:27:48.330084Z 14140 [Note] InnoDB: *** (2) HOLDS THE LOCK(S):

RECORD LOCKS space id 36 page no 3 n bits 72 index PRIMARY of table `test`.`test3` trx id 31791 lock_mode X locks rec but not gap
Record lock, heap no 2 PHYSICAL RECORD: n_fields 5; compact format; info bits 0
 0: len 4; hex 80000001; asc     ;;
 1: len 6; hex 000000007c2f; asc     |/;;
 2: len 7; hex 33000001ac2f63; asc 3    /c;;
 3: len 4; hex 8133c84f; asc  3 O;;
 4: len 4; hex 80000001; asc     ;;

2017-08-28T07:27:48.330342Z 14140 [Note] InnoDB: *** (2) WAITING FOR THIS LOCK TO BE GRANTED:

RECORD LOCKS space id 36 page no 4 n bits 72 index id2 of table `test`.`test3` trx id 31791 lock mode S waiting
Record lock, heap no 3 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
 0: len 4; hex 8133c84f; asc  3 O;;
 1: len 4; hex 80000002; asc     ;;
2017-08-28T07:27:48.330470Z 14140 [Note] InnoDB: *** WE ROLL BACK TRANSACTION (2)

這里會充分把x,s鎖,細粒度鎖的知識聯(lián)系起來,搞明白又進步了一大截。


會話1:

最后,我們提交一下事務(wù),再次查看數(shù)據(jù),一切又恢復(fù)了平靜。

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> select *from test3;
+-----+----------+------+
| id1 | id2      | id3  |
+-----+----------+------+
|   2 | 20170831 |    2 |
+-----+----------+------+
1 row in set (0.00 sec)





向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI