建表插入測試數據
A> create table test(id int ,num int) ;
Query OK, 0 rows affected (0.53 sec)mysql
A> insert into test values(1,1);
Query OK, 1 row affected (0.01 sec)sql
A> insert into test values(2,2);
Query OK, 1 row affected (0.00 sec)session
A> insert into test values(3,3);
Query OK, 1 row affected (0.01 sec)測試
A> select * from test ;
+------+------+
| id | num |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)rest
A> show create table test ;
+-------+----------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------+
| test | CREATE TABLE `test` (
`id` int(11) DEFAULT NULL,
`num` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)事務
一。隔離級別read uncommitted,事物A能夠讀到事物B的未提交數據,未提交數據稱爲髒數據,所以叫髒讀。
A> set session transaction isolation level read uncommitted ;
Query OK, 0 rows affected (0.00 sec)it
A> SELECT @@tx_isolation ;
+------------------+
| @@tx_isolation |
+------------------+
| READ-UNCOMMITTED |
+------------------+
1 row in set (0.00 sec)io
A> start transaction ;
Query OK, 0 rows affected (0.00 sec)innodb
A> select * from test ;
+------+------+
| id | num |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)table
B> start transaction ;
Query OK, 0 rows affected (0.00 sec)
B> update test set num=10 where id=1 ;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
B> select * from test ;
+------+------+
| id | num |
+------+------+
| 1 | 10 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)
A> select * from test ;
+------+------+
| id | num |
+------+------+
| 1 | 10 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.01 sec)
B> rollback ;
Query OK, 0 rows affected (0.01 sec)
B> select * from test ;
+------+------+
| id | num |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)
A> select * from test ;
+------+------+
| id | num |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)
二。隔離級別read committed ,
對於事物B未提交的DML操做,事物A是看不到的
可是事物A在自己事物操做的過程當中(未作commit or rollback),能夠讀到事物B的已提交數據,這就是不可重複讀
下面測試下:
A> set session transaction isolation level read committed ;
Query OK, 0 rows affected (0.00 sec)
A> start transaction ;
Query OK, 0 rows affected (0.00 sec)
A> select * from test ;
+------+------+
| id | num |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)
B>start transaction ;
Query OK, 0 rows affected (0.00 sec)
B>update test set num=20 where id=1 ;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
B>rollback ;
Query OK, 0 rows affected (0.01 sec)
B>start transaction ;
Query OK, 0 rows affected (0.00 sec)
B>insert into test values(4,4) ;
Query OK, 1 row affected (0.00 sec)
B>select * from test;
+------+------+
| id | num |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
+------+------+
4 rows in set (0.00 sec)
B>rollback ;
Query OK, 0 rows affected (0.01 sec)
B>start transaction ;
Query OK, 0 rows affected (0.00 sec)
B>delete from test where id =1 ;
Query OK, 1 row affected (0.01 sec)
B>select * from test;
+------+------+
| id | num |
+------+------+
| 2 | 2 |
| 3 | 3 |
+------+------+
2 rows in set (0.00 sec)
B>rollback ;
Query OK, 0 rows affected (0.01 sec)
在事物B作這些DML操做的時候,事物A的查詢結果一直沒變,此時事物A還沒有結束
A> select * from test ;
+------+------+
| id | num |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)
提交事物B的某個操做,事物A裏面的對錶test的查詢結果就變了,所以事物A裏面對test表的查詢是不可重複的,因此叫不可重複讀
三。隔離級別repeatable read
mysql的默認事物隔離級別,防止了髒讀和不可重複讀,但可能出現幻讀
該隔離級別解決了不重複讀,保證了同一個事務裏,查詢的結果都是事務開始時的狀態(一致性)。
可是,若是另外一個事務同時提交了新數據,本事務再更新時,就會「驚奇的」發現了這些新數據,貌似以前讀到的數據是「鬼影」同樣的幻覺。
A> set session transaction isolation level repeatable read ;
Query OK, 0 rows affected (0.00 sec)
A> select @@session.tx_isolation ;
+------------------------+
| @@session.tx_isolation |
+------------------------+
| REPEATABLE-READ |
+------------------------+
1 row in set (0.00 sec)
A> start transaction ;
Query OK, 0 rows affected (0.00 sec)
A> select * from test ;
+------+------+
| id | num |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)
B> start transaction ;
Query OK, 0 rows affected (0.00 sec)
B> update test set num=10 where id=1 ;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
B> select * from test ;
+------+------+
| id | num |
+------+------+
| 1 | 10 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)
B> commit ;
Query OK, 0 rows affected (0.01 sec)
此處A的修改應該是num=3纔對,可是由於幻讀,事物B雖然提交了,但事物A讀到的仍是舊記錄,
如同幻影通常,事物A修改後,發現結果是30,而不是想要的結果3
A> update test set num=num*3 where id=1 ;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
A> select * from test ;
+------+------+
| id | num |
+------+------+
| 1 | 30 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)
A> commit ;
Query OK, 0 rows affected (0.01 sec)
四。隔離級別SERIALIZABLE
SERIALIZABLE事務隔離級別最嚴厲,在進行查詢時就會對錶或行加上共享鎖,其餘事務對該表將只能進行讀操做,而不能進行寫操做。
A> set session transaction isolation level serializable ;
Query OK, 0 rows affected (0.00 sec)
A> select @@session.tx_isolation ;
+------------------------+
| @@session.tx_isolation |
+------------------------+
| SERIALIZABLE |
+------------------------+
1 row in set (0.00 sec)
A> start transaction ;
Query OK, 0 rows affected (0.00 sec)
A> select * from test ;
+------+------+
| id | num |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)
B> start transaction ;
Query OK, 0 rows affected (0.00 sec)
B> update test set num=3 where id=1 ;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
控制超時的參數innodb_lock_wait_timeout | 120