[Mysql]——通过例子理解事务的4种隔离级别
第1級(jí)別:Read Uncommitted(讀取未提交內(nèi)容)
第2級(jí)別:Read Committed(讀取提交內(nèi)容)
第3級(jí)別:Repeatable Read(可重讀)第4級(jí)別:Serializable(可串行化)
SQL標(biāo)準(zhǔn)定義了4種隔離級(jí)別,包括了一些具體規(guī)則,用來(lái)限定事務(wù)內(nèi)外的哪些改變是可見(jiàn)的,哪些是不可見(jiàn)的。
低級(jí)別的隔離級(jí)一般支持更高的并發(fā)處理,并擁有更低的系統(tǒng)開(kāi)銷。
首先,我們使用 test 數(shù)據(jù)庫(kù),新建 tx 表,并且如圖所示打開(kāi)兩個(gè)窗口來(lái)操作同一個(gè)數(shù)據(jù)庫(kù):
第1級(jí)別:Read Uncommitted(讀取未提交內(nèi)容)
(1)所有事務(wù)都可以看到其他未提交事務(wù)的執(zhí)行結(jié)果
(2)本隔離級(jí)別很少用于實(shí)際應(yīng)用,因?yàn)樗男阅芤膊槐绕渌?jí)別好多少
(3)該級(jí)別引發(fā)的問(wèn)題是——臟讀(Dirty Read):讀取到了未提交的數(shù)據(jù)
#首先,修改隔離級(jí)別
set tx_isolation='READ-UNCOMMITTED';
select @@tx_isolation;
+------------------+
| @@tx_isolation ? |
+------------------+
| READ-UNCOMMITTED |
+------------------+
#事務(wù)A:啟動(dòng)一個(gè)事務(wù)
start transaction;
select * from tx;
+------+------+
| id ? | num ?|
+------+------+
| ? ?1 | ? ?1 |
| ? ?2 | ? ?2 |
| ? ?3 | ? ?3 |
+------+------+
#事務(wù)B:也啟動(dòng)一個(gè)事務(wù)(那么兩個(gè)事務(wù)交叉了)
? ? ? ?在事務(wù)B中執(zhí)行更新語(yǔ)句,且不提交
start transaction;
update tx set num=10 where id=1;
select * from tx;
+------+------+
| id ? | num ?|
+------+------+
| ? ?1 | ? 10 |
| ? ?2 | ? ?2 |
| ? ?3 | ? ?3 |
+------+------+
#事務(wù)A:那么這時(shí)候事務(wù)A能看到這個(gè)更新了的數(shù)據(jù)嗎?
select * from tx;
+------+------+
| id ? | num ?|
+------+------+
| ? ?1 | ? 10 | ? --->可以看到!說(shuō)明我們讀到了事務(wù)B還沒(méi)有提交的數(shù)據(jù)
| ? ?2 | ? ?2 |
| ? ?3 | ? ?3 |
+------+------+
#事務(wù)B:事務(wù)B回滾,仍然未提交
rollback;
select * from tx;
+------+------+
| id ? | num ?|
+------+------+
| ? ?1 | ? ?1 |
| ? ?2 | ? ?2 |
| ? ?3 | ? ?3 |
+------+------+
#事務(wù)A:在事務(wù)A里面看到的也是B沒(méi)有提交的數(shù)據(jù)
select * from tx;
+------+------+
| id ? | num ?|
+------+------+
| ? ?1 | ? ?1 | ? ? ?--->臟讀意味著我在這個(gè)事務(wù)中(A中),事務(wù)B雖然沒(méi)有提交,但它任何一條數(shù)據(jù)變化,我都可以看到!
| ? ?2 | ? ?2 |
| ? ?3 | ? ?3 |
+------+------+
?
第2級(jí)別:Read Committed(讀取提交內(nèi)容)
(1)這是大多數(shù)數(shù)據(jù)庫(kù)系統(tǒng)的默認(rèn)隔離級(jí)別(但不是MySQL默認(rèn)的)
(2)它滿足了隔離的簡(jiǎn)單定義:一個(gè)事務(wù)只能看見(jiàn)已經(jīng)提交事務(wù)所做的改變
(3)這種隔離級(jí)別出現(xiàn)的問(wèn)題是——不可重復(fù)讀(Nonrepeatable Read):不可重復(fù)讀意味著我們?cè)谕粋€(gè)事務(wù)中執(zhí)行完全相同的select語(yǔ)句時(shí)可能看到不一樣的結(jié)果。
? ? ?|——>導(dǎo)致這種情況的原因可能有:(1)有一個(gè)交叉的事務(wù)有新的commit,導(dǎo)致了數(shù)據(jù)的改變;(2)一個(gè)數(shù)據(jù)庫(kù)被多個(gè)實(shí)例操作時(shí),同一事務(wù)的其他實(shí)例在該實(shí)例處理其間可能會(huì)有新的commit
#首先修改隔離級(jí)別
set tx_isolation='read-committed';
select @@tx_isolation;
+----------------+
| @@tx_isolation |
+----------------+
| READ-COMMITTED |
+----------------+
#事務(wù)A:啟動(dòng)一個(gè)事務(wù)
start transaction;
select * from tx;
+------+------+
| id ? | num ?|
+------+------+
| ? ?1 | ? ?1 |
| ? ?2 | ? ?2 |
| ? ?3 | ? ?3 |
+------+------+
#事務(wù)B:也啟動(dòng)一個(gè)事務(wù)(那么兩個(gè)事務(wù)交叉了)
? ? ? ?在這事務(wù)中更新數(shù)據(jù),且未提交
start transaction;
update tx set num=10 where id=1;
select * from tx;
+------+------+
| id ? | num ?|
+------+------+
| ? ?1 | ? 10 |
| ? ?2 | ? ?2 |
| ? ?3 | ? ?3 |
+------+------+
#事務(wù)A:這個(gè)時(shí)候我們?cè)谑聞?wù)A中能看到數(shù)據(jù)的變化嗎?
select * from tx; --------------->
+------+------+ ? ? ? ? ? ? ? ?|
| id ? | num ?| ? ? ? ? ? ? ? ?|
+------+------+ ? ? ? ? ? ? ? ?|
| ? ?1 | ? ?1 |--->并不能看到! ?|
| ? ?2 | ? ?2 | ? ? ? ? ? ? ? ?|
| ? ?3 | ? ?3 | ? ? ? ? ? ? ? ?|
+------+------+ ? ? ? ? ? ? ? ?|——>相同的select語(yǔ)句,結(jié)果卻不一樣
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |
#事務(wù)B:如果提交了事務(wù)B呢? ? ? ? ? ? ?|
commit; ? ? ? ? ? ? ? ? ? ? ? ? ? |
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |
#事務(wù)A: ? ? ? ? ? ? ? ? ? ? ? ? ? ?|
select * from tx; --------------->
+------+------+
| id ? | num ?|
+------+------+
| ? ?1 | ? 10 |--->因?yàn)槭聞?wù)B已經(jīng)提交了,所以在A中我們看到了數(shù)據(jù)變化
| ? ?2 | ? ?2 |
| ? ?3 | ? ?3 |
+------+------+
?
第3級(jí)別:Repeatable Read(可重讀)
(1)這是MySQL的默認(rèn)事務(wù)隔離級(jí)別
(2)它確保同一事務(wù)的多個(gè)實(shí)例在并發(fā)讀取數(shù)據(jù)時(shí),會(huì)看到同樣的數(shù)據(jù)行
(3)此級(jí)別可能出現(xiàn)的問(wèn)題——幻讀(Phantom Read):當(dāng)用戶讀取某一范圍的數(shù)據(jù)行時(shí),另一個(gè)事務(wù)又在該范圍內(nèi)插入了新行,當(dāng)用戶再讀取該范圍的數(shù)據(jù)行時(shí),會(huì)發(fā)現(xiàn)有新的“幻影” 行
(4)InnoDB和Falcon存儲(chǔ)引擎通過(guò)多版本并發(fā)控制(MVCC,Multiversion Concurrency Control)機(jī)制解決了該問(wèn)題
#首先,更改隔離級(jí)別
set tx_isolation='repeatable-read';
select @@tx_isolation;
+-----------------+
| @@tx_isolation ?|
+-----------------+
| REPEATABLE-READ |
+-----------------+
#事務(wù)A:啟動(dòng)一個(gè)事務(wù)
start transaction;
select * from tx;
+------+------+
| id ? | num ?|
+------+------+
| ? ?1 | ? ?1 |
| ? ?2 | ? ?2 |
| ? ?3 | ? ?3 |
+------+------+
#事務(wù)B:開(kāi)啟一個(gè)新事務(wù)(那么這兩個(gè)事務(wù)交叉了)
? ? ? ?在事務(wù)B中更新數(shù)據(jù),并提交
start transaction;
update tx set num=10 where id=1;
select * from tx;
+------+------+
| id ? | num ?|
+------+------+
| ? ?1 | ? 10 |
| ? ?2 | ? ?2 |
| ? ?3 | ? ?3 |
+------+------+
commit;
#事務(wù)A:這時(shí)候即使事務(wù)B已經(jīng)提交了,但A能不能看到數(shù)據(jù)變化?
select * from tx;
+------+------+
| id ? | num ?|
+------+------+
| ? ?1 | ? ?1 | --->還是看不到的!(這個(gè)級(jí)別2不一樣,也說(shuō)明級(jí)別3解決了不可重復(fù)讀問(wèn)題)
| ? ?2 | ? ?2 |
| ? ?3 | ? ?3 |
+------+------+
#事務(wù)A:只有當(dāng)事務(wù)A也提交了,它才能夠看到數(shù)據(jù)變化
commit;
select * from tx;
+------+------+
| id ? | num ?|
+------+------+
| ? ?1 | ? 10 |
| ? ?2 | ? ?2 |
| ? ?3 | ? ?3 |
+------+------+
?
第4級(jí)別:Serializable(可串行化)
(1)這是最高的隔離級(jí)別
(2)它通過(guò)強(qiáng)制事務(wù)排序,使之不可能相互沖突,從而解決幻讀問(wèn)題。簡(jiǎn)言之,它是在每個(gè)讀的數(shù)據(jù)行上加上共享鎖。
(3)在這個(gè)級(jí)別,可能導(dǎo)致大量的超時(shí)現(xiàn)象和鎖競(jìng)爭(zhēng)
#首先修改隔離界別
set tx_isolation='serializable';
select @@tx_isolation;
+----------------+
| @@tx_isolation |
+----------------+
| SERIALIZABLE ? |
+----------------+
#事務(wù)A:開(kāi)啟一個(gè)新事務(wù)
start transaction;
#事務(wù)B:在A沒(méi)有commit之前,這個(gè)交叉事務(wù)是不能更改數(shù)據(jù)的
start transaction;
insert tx values('4','4');
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
update tx set num=10 where id=1;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
?
總結(jié)
以上是生活随笔為你收集整理的[Mysql]——通过例子理解事务的4种隔离级别的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 禁止修改varchar到int|[运维规
- 下一篇: MySQL小误区:关于set globa