溫馨提示×

溫馨提示×

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

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

怎么理解mysql中read commited與repeatable read

發(fā)布時間:2021-11-11 16:05:25 來源:億速云 閱讀:188 作者:iii 欄目:MySQL數(shù)據(jù)庫

這篇文章主要介紹“怎么理解mysql中read commited與repeatable read”,在日常操作中,相信很多人在怎么理解mysql中read commited與repeatable read問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么理解mysql中read commited與repeatable read”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

兩個事物,A與B

1. 先看read committed級別的情況:

--A事物
mysql> set session transaction isolation level read committed;
Query OK, 0 rows affected (0.00 sec)

mysql> set autocommit = 0;
Query OK, 0 rows affected (0.01 sec)

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

mysql> select * from test;
+------+------+
| id   | comm |
+------+------+
|    1 | aaa  |
+------+------+
1 row in set (0.00 sec)

--B事物

mysql> set autocommit = 0;
Query OK, 0 rows affected (0.00 sec)

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

mysql> update test set comm = 'bbb' where id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

--A事物此時查詢表test,此時B事物未提交,因此讀到的仍然是舊的數(shù)據(jù)

mysql> select * from test;
+------+------+
| id   | comm |
+------+------+
|    1 | aaa  |
+------+------+
1 row in set (0.00 sec)

--B事物提交
mysql> commit;
Query OK, 0 rows affected (0.00 sec)

--A事物再次查詢,數(shù)據(jù)已發(fā)生改變。

mysql> select * from test;
+------+------+
| id   | comm |
+------+------+
|    1 | bbb  |
+------+------+
1 row in set (0.00 sec)

可見,在 read committed級別,顧名思義,A事物在事物進(jìn)行過程中,讀取不到B事物未提交的數(shù)據(jù),但是可以讀取到B事物已經(jīng)提交的數(shù)據(jù)修改

2.再看repeatable read級別

--A事物:

mysql> set autocommit = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> set session transaction isolation level repeatable read;
Query OK, 0 rows affected (0.00 sec)

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

mysql> select * from test;
+------+------+
| id   | comm |
+------+------+
|    1 | aaa  |
+------+------+
1 row in set (0.00 sec)

--B事物:
mysql> set autocommit = 0;
Query OK, 0 rows affected (0.00 sec)

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

mysql> update test set comm = 'bbb' where id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0


--A事物此時查詢表test,此時B事物未提交,因此讀到的仍然是舊的數(shù)據(jù)
mysql> select * from test;
+------+------+
| id   | comm |
+------+------+
|    1 | aaa  |
+------+------+
1 row in set (0.00 sec)

--B事物提交
mysql> commit;
Query OK, 0 rows affected (0.00 sec)

--A事物再次查詢,發(fā)現(xiàn)數(shù)據(jù)未發(fā)生改變
mysql> select * from test;
+------+------+
| id   | comm |
+------+------+
|    1 | aaa  |
+------+------+
1 row in set (0.00 sec)

可見,在 repeatable read 級別,顧名思義,A事物在事物進(jìn)行過程中,是不能讀取到B事物未提交和已提交的數(shù)據(jù)修改。

對于repeatable read 級別,事物只能讀取到事物開始時的數(shù)據(jù),因此在事物進(jìn)行過程中讀取到的數(shù)據(jù)時一致的,而對于read commited級別,在事物進(jìn)行過程中,讀取到的數(shù)據(jù)可能是不一致的。

到此,關(guān)于“怎么理解mysql中read commited與repeatable read”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細(xì)節(jié)

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

AI