前言
MySQL事務(wù)主要用于處理一個(gè)包含操作量比較大膏萧、復(fù)雜的業(yè)務(wù)。比如說蝌衔,刪除一個(gè)學(xué)生榛泛,我們除了要刪除該學(xué)生的基本信息,同時(shí)也要刪除考試記錄胚委、違規(guī)記錄等挟鸠。諸多的操作組成一個(gè)事務(wù)。事務(wù)是用來管理insert
亩冬、update
艘希、delete
基本指令的。當(dāng)MySQL使用innodb引擎的前提下才支持事務(wù)操作硅急。
事務(wù)的基本特點(diǎn)
-
原子性
一個(gè)事務(wù)的執(zhí)行所有的操作覆享,結(jié)果只有兩種:要么全部執(zhí)行、要么全部不執(zhí)行营袜。事務(wù)在執(zhí)行的過程中撒顿,當(dāng)在某一個(gè)節(jié)點(diǎn)執(zhí)行發(fā)生錯(cuò)誤的時(shí)候,事務(wù)會被執(zhí)行
rollback
操作荚板,將數(shù)據(jù)恢復(fù)到執(zhí)行該事務(wù)之前的狀態(tài)凤壁。A去銀行轉(zhuǎn)賬吩屹,要么轉(zhuǎn)賬成功、要么轉(zhuǎn)賬失敗拧抖。 -
一致性
從事務(wù)開始執(zhí)行到執(zhí)行完成后煤搜,數(shù)據(jù)庫的完整性約束完全沒有收到破壞。A轉(zhuǎn)賬給B唧席,不可能發(fā)生這種情況:A轉(zhuǎn)賬成功擦盾、B沒有收到款。
-
隔離性
在同一時(shí)間點(diǎn)淌哟,數(shù)據(jù)庫允許多個(gè)并發(fā)事務(wù)同時(shí)對其數(shù)據(jù)進(jìn)行讀寫和修改的能力迹卢,隔離性可以防止多個(gè)事務(wù)并發(fā)執(zhí)行時(shí)由于交叉執(zhí)行而導(dǎo)致數(shù)據(jù)的不一致。事務(wù)隔離分為不同級別徒仓,包括讀未提交(
read uncommitted
)腐碱、讀提交(read committed
)、可重復(fù)讀(repeatable read
|默認(rèn)方式 )和串行化(serializable
)蓬衡。 -
持久性
事務(wù)成功執(zhí)行后喻杈,事務(wù)的所有操作對數(shù)據(jù)庫的更新是永久的,不能回滾狰晚。
隔離性的類別
-
read uncommitted
| 讀未提交 -
read committed
| 讀已提交 -
repeatable read
| 可重復(fù)讀 -
serializable
| 串行化
在MySQL
數(shù)據(jù)庫中筒饰,引擎默認(rèn)使用repeatable read
# SELECT @@tx_isolation 或者 SELECT @@transaction_isolation
# MySQL 8.x
# transaction_isolation在MySQL 5.7.20中添加了作為別名 tx_isolation,現(xiàn)已棄用壁晒,并在MySQL 8.0中刪除瓷们。
# 應(yīng)調(diào)整應(yīng)用程序transaction_isolation以優(yōu)先使用 tx_isolation。
mysql> SELECT @@transaction_isolation;
+-------------------------+
| @@transaction_isolation |
+-------------------------+
| REPEATABLE-READ |
+-------------------------+
1 row in set (0.01 sec)
事務(wù)的并發(fā)問題
-
臟讀
事務(wù)A讀取了事務(wù)B更新的數(shù)據(jù)秒咐,然后事務(wù)B在某些因素下執(zhí)行了回滾谬晕,那么事務(wù)A讀取的數(shù)據(jù)就是不合理的,即臟數(shù)據(jù)携取。
## (1)事務(wù)A的操作 ## 設(shè)置為隔離方式為[讀未提交 | read uncommitted] ## 開啟事務(wù)并查詢id為1的score的值 mysql> set session transaction isolation level read uncommitted; Query OK, 0 rows affected (0.00 sec) mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> select * from score; +----+----------+-------+ | id | name | score | +----+----------+-------+ | 1 | alicfeng | 80 | | 2 | feng | 100 | | 3 | alic | 90 | +----+----------+-------+ 3 rows in set (0.00 sec) ## (2)事務(wù)B的操作 ## 開啟事務(wù)并將id為1的score修改成 75 mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> update score set score=75 where id=1; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from score; +----+----------+-------+ | id | name | score | +----+----------+-------+ | 1 | alicfeng | 75 | | 2 | feng | 100 | | 3 | alic | 90 | +----+----------+-------+ 3 rows in set (0.00 sec) ## (3)事務(wù)A的操作 ## 再次讀取id為1的score值 75 mysql> select * from score; +----+----------+-------+ | id | name | score | +----+----------+-------+ | 1 | alicfeng | 75 | | 2 | feng | 100 | | 3 | alic | 90 | +----+----------+-------+ 3 rows in set (0.00 sec) ## (4) 事務(wù)B的操作 ## 事務(wù)回滾 mysql> rollback; Query OK, 0 rows affected (0.00 sec)
上述四個(gè)步驟中堰汉,事務(wù)A在事務(wù)B前讀取的
score
的值為80
病毡,在事務(wù)B執(zhí)行修改后讀取score
的值為75
德挣,事務(wù)B再進(jìn)行回滾操作臼勉,那么事務(wù)A在兩次讀取的score
的值是不一致的,那么就是臟讀晤斩。
-
不可重復(fù)讀
事務(wù)A需要重復(fù)多次讀取某組數(shù)據(jù)焕檬,事務(wù)A在事務(wù)B對該組數(shù)據(jù)修改提交前后進(jìn)行讀取,很顯然澳泵、兩次讀取的數(shù)據(jù)是不一致的实愚,即不可重復(fù)讀。側(cè)重于元數(shù)據(jù)的修改。
## 使用[讀已提交]的模式實(shí)踐 mysql> set session transaction isolation level read committed; Query OK, 0 rows affected (0.00 sec) ## (1) 事務(wù)A查詢id為1的score 80 mysql> select * from score; +----+----------+-------+ | id | name | score | +----+----------+-------+ | 1 | alicfeng | 80 | | 2 | feng | 100 | | 3 | alic | 90 | +----+----------+-------+ 3 rows in set (0.00 sec) ## (2) 事務(wù)B修改id為1的score并提交事務(wù) 75 mysql> update score set score=75 where id=1; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> commit; Query OK, 0 rows affected (0.00 sec) ## (3) 事務(wù)A再次查詢id為1的score的值 75 mysql> select * from score; +----+----------+-------+ | id | name | score | +----+----------+-------+ | 1 | alicfeng | 80 | | 2 | feng | 100 | | 3 | alic | 90 | +----+----------+-------+ 3 rows in set (0.00 sec) mysql> select * from score; +----+----------+-------+ | id | name | score | +----+----------+-------+ | 1 | alicfeng | 75 | | 2 | feng | 100 | | 3 | alic | 90 | +----+----------+-------+ 3 rows in set (0.00 sec)
從上述的三個(gè)步驟中顯而易見可以看出腊敲,事務(wù)A在事務(wù)B修改并提交的前后讀取同一條數(shù)據(jù)的值得不一樣的击喂,具有不可重復(fù)讀問題。
-
幻讀
事務(wù)A在修改每一條元數(shù)據(jù)的時(shí)候兔仰,事務(wù)B在此時(shí)添加了一條新記錄茫负,事務(wù)A在處理的過程中突然多了一條數(shù)據(jù)蕉鸳,即幻讀乎赴。側(cè)重于數(shù)據(jù)的刪除與修改。
## 將事務(wù)隔離的模式設(shè)置為[可重復(fù)讀] mysql> set session transaction isolation level repeatable read; Query OK, 0 rows affected (0.00 sec) ## (1)事務(wù)A讀取scor數(shù)據(jù)表 mysql> select * from score; +----+----------+-------+ | id | name | score | +----+----------+-------+ | 1 | alicfeng | 75 | | 2 | feng | 100 | | 3 | alic | 90 | +----+----------+-------+ 3 rows in set (0.00 sec) ## (2)事務(wù)B新增刪除一條數(shù)據(jù)并提交 mysql> delete from score where id=1; Query OK, 1 row affected (0.01 sec) mysql> commit; Query OK, 0 rows affected (0.01 sec) ## (3)事務(wù)A再次讀取score數(shù)據(jù)表 mysql> select * from score; +----+----------+-------+ | id | name | score | +----+----------+-------+ | 1 | alicfeng | 75 | | 2 | feng | 100 | | 3 | alic | 90 | +----+----------+-------+ 3 rows in set (0.00 sec)
可見潮尝,事務(wù)A在事務(wù)B刪除并提交前后讀取的數(shù)據(jù)一樣榕吼,出現(xiàn)了幻讀。
事務(wù)隔離級別的影響
事務(wù)隔離級別 | 臟讀 | 不可重復(fù)讀 | 幻讀 |
---|---|---|---|
讀未提交 | read uncommitted
|
會 | 會 | 會 |
讀已提交 | read committed
|
不會 | 會 | 會 |
可重復(fù)讀 | repeatable read
|
不會 | 不會 | 會 |
串行化 | serializable
|
不會 | 不會 | 不會 |
事務(wù)隔離性說明
- 隔離級別越高勉失,越能保證數(shù)據(jù)的完整性和一致性羹蚣,但是對并發(fā)性能的影響也越大。
- 事務(wù)隔離級別為讀提交時(shí)乱凿,寫數(shù)據(jù)只會鎖住相應(yīng)的行
- 事務(wù)隔離級別為串行化時(shí)顽素,讀寫數(shù)據(jù)都會鎖住整張表