標籤:style blog http color os strong io 資料
MySQL交易隔離等級為"REPEATABLE-READ"下的"幻讀"現象
關於mysql命令列中事務控制的語句見該文章:http://my.oschina.net/xinxingegeya/blog/296459
本片參考文章:http://blog.csdn.net/jiao_fuyou/article/details/16368827
http://www.cnblogs.com/hancf/archive/2012/08/28/2660422.html
mysql中的可重複讀
這裡開啟兩個mysql的命令列視窗,視窗A,即session1,視窗B,即session2。
session1
mysql> begin ;Query OK, 0 rows affected (0.00 sec)mysql> select * from t1;+----+------+| a | b |+----+------+| 51 | 3000 || 52 | 3000 || 53 | 3000 || 54 | 3000 |+----+------+4 rows in set (0.00 sec)
session2
mysql> begin ;Query OK, 0 rows affected (0.00 sec)mysql> insert into t1 values (55, 3000);Query OK, 1 row affected (0.00 sec)mysql> commit;Query OK, 0 rows affected (0.03 sec)mysql> select * from t1;+----+------+| a | b |+----+------+| 51 | 3000 || 52 | 3000 || 53 | 3000 || 54 | 3000 || 55 | 3000 |+----+------+5 rows in set (0.00 sec)
session2事務已經顯示提交
session1
此時返回session1進行以下操作
mysql> select * from t1;+----+------+| a | b |+----+------+| 51 | 3000 || 52 | 3000 || 53 | 3000 || 54 | 3000 |+----+------+4 rows in set (0.00 sec)
此時,雖然在session2中插入了一條資料,並且提交了事務,但在session1中的查詢和session1的上次查詢還是同一個結果,這就是重複讀。如果是在"READ-COMMITTED"層級下是可以讀到a=55這條記錄的。
測試mysql可重複讀(二)
session1
mysql> begin;Query OK, 0 rows affected (0.00 sec)mysql> select * from t1 where a = 56;+----+------+| a | b |+----+------+| 56 | 7000 |+----+------+1 row in set (0.00 sec)
session2
mysql> begin;Query OK, 0 rows affected (0.00 sec)mysql> update t1 set b = 8000 where a = 56;Query OK, 1 row affected (0.00 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql> select * from t1 where a = 56;+----+------+| a | b |+----+------+| 56 | 8000 |+----+------+1 row in set (0.00 sec)mysql> commit -> ;Query OK, 0 rows affected (0.04 sec)
session1
mysql> select * from t1 where a = 56;+----+------+| a | b |+----+------+| 56 | 7000 |+----+------+1 row in set (0.00 sec)
和上次查詢結果一致,驗證了重複讀。。。
此時,如果session1提交該事務,重新開啟事務,查詢能查到session2中修改的結果
mysql> commit;Query OK, 0 rows affected (0.00 sec)mysql> select * from t1 where a = 56;+----+------+| a | b |+----+------+| 56 | 8000 |+----+------+1 row in set (0.00 sec)
REPEATABLE-READ下的幻讀
session1
mysql> begin;Query OK, 0 rows affected (0.00 sec)mysql> select * from t1;+----+------+| a | b |+----+------+| 51 | 3000 || 52 | 3000 || 53 | 3000 || 54 | 3000 || 55 | 4000 || 56 | 8000 |+----+------+6 rows in set (0.00 sec)
session2
mysql> begin;Query OK, 0 rows affected (0.00 sec)mysql> select * from t1;+----+------+| a | b |+----+------+| 51 | 3000 || 52 | 3000 || 53 | 3000 || 54 | 3000 || 55 | 4000 || 56 | 8000 |+----+------+6 rows in set (0.00 sec)mysql> insert into t1 values (57, 1000);Query OK, 1 row affected (0.00 sec)mysql> select * from t1;+----+------+| a | b |+----+------+| 51 | 3000 || 52 | 3000 || 53 | 3000 || 54 | 3000 || 55 | 4000 || 56 | 8000 || 57 | 1000 |+----+------+7 rows in set (0.00 sec)mysql> commit;Query OK, 0 rows affected (0.11 sec)
session1
mysql> update t1 set b = b+1000;Query OK, 7 rows affected (0.00 sec)Rows matched: 7 Changed: 7 Warnings: 0mysql> select * from t1;+----+------+| a | b |+----+------+| 51 | 4000 || 52 | 4000 || 53 | 4000 || 54 | 4000 || 55 | 5000 || 56 | 9000 || 57 | 2000 |+----+------+7 rows in set (0.00 sec)
session1整個會話的sql
mysql> begin;Query OK, 0 rows affected (0.00 sec)mysql> select * from t1;+----+------+| a | b |+----+------+| 51 | 3000 || 52 | 3000 || 53 | 3000 || 54 | 3000 || 55 | 4000 || 56 | 8000 |+----+------+6 rows in set (0.00 sec)mysql> select * from t1;+----+------+| a | b |+----+------+| 51 | 3000 || 52 | 3000 || 53 | 3000 || 54 | 3000 || 55 | 4000 || 56 | 8000 |+----+------+6 rows in set (0.00 sec)mysql> update t1 set b = b+1000;Query OK, 7 rows affected (0.00 sec)Rows matched: 7 Changed: 7 Warnings: 0mysql> select * from t1;+----+------+| a | b |+----+------+| 51 | 4000 || 52 | 4000 || 53 | 4000 || 54 | 4000 || 55 | 5000 || 56 | 9000 || 57 | 2000 |+----+------+7 rows in set (0.00 sec)mysql>
可以看到多出了一行,這就是幻讀。。。
但有一個問題,還是不明白,幻讀和不可重複讀取到底什麼區別?網上查了很多資料還是說的不明白,希望有瞭解的同學可以交流一下啊。。。
=====END====