SQL標準定義了4類隔離級別,包括了一些具體規則,用來限定事務內外的哪些改變是可見的,哪些是不可見的。低級別的隔離級通常支持更高的併發處理,並擁有更低的系統開銷。
Read Uncommitted(讀取未提交內容)java
在該隔離級別,全部事務均可以看到其餘未提交事務的執行結果。本隔離級別不多用於實際應用,由於它的性能也不比其餘級別好多少。讀取未提交的數據,也被稱之爲髒讀(Dirty Read)。
Read Committed(讀取提交內容)mysql
這是大多數數據庫系統的默認隔離級別(但不是MySQL默認的)。它知足了隔離的簡單定義:一個事務只能看見已經提交事務所作的改變。這種隔離級別 也支持所謂的不可重複讀(Nonrepeatable Read),由於同一事務的其餘實例在該實例處理其間可能會有新的commit,因此同一select可能返回不一樣結果。
Repeatable Read(可重讀)sql
這是MySQL的默認事務隔離級別,它確保同一事務的多個實例在併發讀取數據時,會看到一樣的數據行。不過理論上,這會致使另外一個棘手的問題:幻讀 (Phantom Read)。簡單的說,幻讀指當用戶讀取某一範圍的數據行時,另外一個事務又在該範圍內插入了新行,當用戶再讀取該範圍的數據行時,會發現有新的「幻影」 行。InnoDB和Falcon存儲引擎經過多版本併發控制(MVCC,Multiversion Concurrency Control)機制解決了該問題。數據庫
Serializable(可串行化)
這是最高的隔離級別,它經過強制事務排序,使之不可能相互衝突,從而解決幻讀問題。簡言之,它是在每一個讀的數據行上加上共享鎖。在這個級別,可能致使大量的超時現象和鎖競爭。session
這四種隔離級別採起不一樣的鎖類型來實現,若讀取的是同一個數據的話,就容易發生問題。例如:併發
髒讀(Drity Read):某個事務已更新一份數據,另外一個事務在此時讀取了同一份數據,因爲某些緣由,前一個RollBack了操做,則後一個事務所讀取的數據就會是不正確的。性能
不可重複讀(Non-repeatable read):在一個事務的兩次查詢之中數據不一致,這多是兩次查詢過程當中間插入了一個事務更新的原有的數據。spa
幻讀(Phantom Read):在一個事務的兩次查詢中數據筆數不一致,例若有一個事務查詢了幾列(Row)數據,而另外一個事務卻在此時插入了新的幾列數據,先前的事務在接下來的查詢中,就會發現有幾列數據是它先前所沒有的。rest
在MySQL中,實現了這四種隔離級別,分別有可能產生問題以下所示:code
用戶能夠用SET TRANSACTION語句改變單個會話或者全部新進鏈接的隔離級別。它的語法以下:
SET [SESSION | GLOBAL] TRANSACTION ISOLATION LEVEL {READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ | SERIALIZABLE}
注意:默認的行爲(不帶session和global)是爲下一個(未開始)事務設置隔離級別。若是你使用GLOBAL關鍵字,語句在全局對從那點開始建立的全部新鏈接(除了不存在的鏈接)設置默認事務級別。你須要SUPER權限來作這個。使用SESSION 關鍵字爲未來在當前鏈接上執行的事務設置默認事務級別。 任何客戶端都能自由改變會話隔離級別(甚至在事務的中間),或者爲下一個事務設置隔離級別。
你能夠用下列語句查詢全局和繪畫事物隔離級別:
SELECT @@global.tx_isolation; SELECT @@session.tx_isolation; SELECT @@tx_isolation;
一,將A的隔離界別設置爲read uncommintted(未提交讀)
客戶端A:
mysql> select @@tx_isolation; +------------------+ | @@tx_isolation | +------------------+ | READ-UNCOMMITTED | +------------------+ 1 row in set (0.00 sec) mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> select * from tx; +------+------+ | id | num | +------+------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | +------+------+ 3 rows in set (0.00 sec) mysql>
客戶端B:
mysql> select @@tx_isolation; +-----------------+ | @@tx_isolation | +-----------------+ | REPEATABLE-READ | +-----------------+ 1 row in set (0.00 sec) mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> update tx set num=10 where id=1; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from tx; +------+------+ | id | num | +------+------+ | 1 | 10 | | 2 | 2 | | 3 | 3 | +------+------+ 3 rows in set (0.00 sec)
客戶端A:
mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> select * from tx; +------+------+ | id | num | +------+------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | +------+------+ 3 rows in set (0.00 sec) mysql> select * from tx; +------+------+ | id | num | +------+------+ | 1 | 10 | | 2 | 2 | | 3 | 3 | +------+------+ 3 rows in set (0.00 sec) mysql>
客戶端B:
mysql> rollback; Query OK, 0 rows affected (0.00 sec) mysql> select * from tx; +------+------+ | id | num | +------+------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | +------+------+ 3 rows in set (0.00 sec)
客戶端A:
mysql> select * from tx; +------+------+ | id | num | +------+------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | +------+------+ 3 rows in set (0.00 sec) mysql>
上面的實驗能夠得出結論, 事物B更新一條記錄,可是沒有提交,此時A能夠查詢出未提交記錄。形成髒讀現象。未提交讀是最低的隔離級別。
二,將客戶端A的事物隔離級別設置爲read committed
在B未更新數據以前:
客戶端A:
mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> select * from tx; +------+------+ | id | num | +------+------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | +------+------+ 3 rows in set (0.00 sec) mysql>
客戶端B:
mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> update tx set num=10 where id=1; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from tx; +------+------+ | id | num | +------+------+ | 1 | 10 | | 2 | 2 | | 3 | 3 | +------+------+ 3 rows in set (0.00 sec) mysql>
此時查看客戶端A已經成功解決髒讀問題
客戶端A:
mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> select * from tx; +------+------+ | id | num | +------+------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | +------+------+ 3 rows in set (0.00 sec) mysql> select * from tx; +------+------+ | id | num | +------+------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | +------+------+ 3 rows in set (0.00 sec) mysql>
而後,將客戶端B的事物提交commit
客戶端B:
mysql> commit; Query OK, 0 rows affected (0.00 sec) mysql> select * from tx; +------+------+ | id | num | +------+------+ | 1 | 10 | | 2 | 2 | | 3 | 3 | +------+------+ 3 rows in set (0.00 sec)
此時再看客戶端A,數據已經更新
客戶端A:
mysql> select * from tx; +------+------+ | id | num | +------+------+ | 1 | 10 | | 2 | 2 | | 3 | 3 | +------+------+ 3 rows in set (0.00 sec) mysql>
通過上面的實驗能夠得出結論,已提交讀隔離級別解決了髒讀的問題,可是出現了不可重複讀的問題,即事務A在兩次查詢的數據不一致,由於在兩次查詢之間事務B更新了一條數據。已提交讀只容許讀取已提交的記錄,但不要求可重複讀。
三,將A的隔離級別設置爲repeeatable read(可重複讀)
在B未更新數據以前,客戶端A:
mysql> select @@tx_isolation; +-----------------+ | @@tx_isolation | +-----------------+ | REPEATABLE-READ | +-----------------+ 1 row in set (0.00 sec) mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> select * from tx; +------+------+ | id | num | +------+------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | +------+------+ 3 rows in set (0.00 sec) mysql>
B更新數據
客戶端B:
mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> update tx set num=10 where id = 1; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from tx; +------+------+ | id | num | +------+------+ | 1 | 10 | | 2 | 2 | | 3 | 3 | +------+------+ 3 rows in set (0.00 sec) mysql>
此時B沒有提交,A讀不到更新:
客戶端A:
mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> select * from tx; +------+------+ | id | num | +------+------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | +------+------+ 3 rows in set (0.00 sec) mysql> select * from tx; +------+------+ | id | num | +------+------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | +------+------+ 3 rows in set (0.00 sec) mysql>
此時B提交:
客戶端B:
mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> update tx set num=10 where id = 1; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from tx; +------+------+ | id | num | +------+------+ | 1 | 10 | | 2 | 2 | | 3 | 3 | +------+------+ 3 rows in set (0.00 sec) mysql> commit; Query OK, 0 rows affected (0.00 sec) mysql>
此時A仍然讀不到更新的數據,事物中的數據一致,可重複讀
客戶端A:
mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> update tx set num=10 where id = 1; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from tx; +------+------+ | id | num | +------+------+ | 1 | 10 | | 2 | 2 | | 3 | 3 | +------+------+ 3 rows in set (0.00 sec) mysql> commit; Query OK, 0 rows affected (0.00 sec) mysql>
B插入數據:
客戶端B:
mysql> insert into tx value(9,5); Query OK, 1 row affected (0.00 sec) mysql> select * from tx; +------+------+ | id | num | +------+------+ | 1 | 10 | | 2 | 2 | | 3 | 3 | | 9 | 5 | +------+------+ 4 rows in set (0.00 sec) mysql>
客戶端A依然讀不到更新:
mysql> select * from tx; +------+------+ | id | num | +------+------+ | 1 | 1 | | 2 | 2 | | 3 | 3 | +------+------+ 3 rows in set (0.00 sec) mysql>
A事物提交後讀取到更新:
mysql> commit; Query OK, 0 rows affected (0.00 sec) mysql> select * from tx; +------+------+ | id | num | +------+------+ | 1 | 10 | | 2 | 2 | | 3 | 3 | | 9 | 5 | +------+------+ 4 rows in set (0.00 sec) mysql>
由以上的實驗能夠得出結論,可重複讀隔離級別只容許讀取已提交記錄,並且在一個事務兩次讀取一個記錄期間,其餘事務更新該記錄不影響本事務的讀取,即一個事物中讀取結果同樣。但該事務不要求與其餘事務可串行化。例如,當一個事務能夠找到由一個已提交事務更新的記錄,可是可能產生幻讀問題(注意是可能,由於數據庫對隔離級別的實現有所差異)。像以上的實驗,就沒有出現數據幻讀的問題。
四,將A的隔離級別設置爲可串行化(Serializable)
A打開事物,B插入一條數據
客戶端A:
mysql> select @@tx_isolation; +----------------+ | @@tx_isolation | +----------------+ | SERIALIZABLE | +----------------+ 1 row in set (0.00 sec) mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> select * from tx; +------+------+ | id | num | +------+------+ | 1 | 10 | | 2 | 2 | | 3 | 3 | | 9 | 5 | +------+------+ 4 rows in set (0.00 sec) mysql>
客戶端B:
mysql> select @@tx_isolation; +-----------------+ | @@tx_isolation | +-----------------+ | REPEATABLE-READ | +-----------------+ 1 row in set (0.00 sec) mysql> insert into tx(num) value(4);
由於此時事物A的隔離級別設置爲serializable,開始事物後,並無提交。因此事物B只能等待。此時A若是一直不提交事物,B端會出現以下狀況:
mysql> select @@tx_isolation; +-----------------+ | @@tx_isolation | +-----------------+ | REPEATABLE-READ | +-----------------+ 1 row in set (0.00 sec) mysql> insert into tx(num) value(4); ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction mysql>
事物A提交事務
客戶端A:
mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> select * from tx; +------+------+ | id | num | +------+------+ | 1 | 10 | | 2 | 2 | | 3 | 3 | | 9 | 5 | +------+------+ 4 rows in set (0.00 sec) mysql> commit; Query OK, 0 rows affected (0.00 sec) mysql>
客戶端B此時正常
客戶端B:
mysql> insert into tx(num) value(4); Query OK, 1 row affected (0.00 sec) mysql> select @@tx_isolation; +-----------------+ | @@tx_isolation | +-----------------+ | REPEATABLE-READ | +-----------------+ 1 row in set (0.00 sec) mysql> select * from tx; +------+------+ | id | num | +------+------+ | 1 | 10 | | 2 | 2 | | 3 | 3 | | 9 | 5 | | NULL | 4 | +------+------+ 5 rows in set (0.00 sec) mysql>
serializale徹底鎖定字段,若一個事物來查詢同一份數據就必須等待,直到前一個事物完成並解除鎖定爲止。是完整的隔離級別,會鎖定對應的數據表格,於是會有效率問題。