前言
MySQL事務(wù)主要用于處理一個(gè)包含操作量比較大、復(fù)雜的業(yè)務(wù)。比如說,刪除一個(gè)學(xué)生,我們除了要?jiǎng)h除該學(xué)生的基本信息,同時(shí)也要?jiǎng)h除考試記錄、違規(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ù)會(huì)被執(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í)對(duì)其數(shù)據(jù)進(jìn)行讀寫和修改的能力,隔離性可以防止多個(gè)事務(wù)并發(fā)執(zhí)行時(shí)由于交叉執(zhí)行而導(dǎo)致數(shù)據(jù)的不一致。事務(wù)隔離分為不同級(jí)別,包括讀未提交(
read uncommitted)、讀提交(read committed)、可重復(fù)讀(repeatable read|默認(rèn)方式 )和串行化(serializable)。 -
持久性
事務(wù)成功執(zhí)行后,事務(wù)的所有操作對(duì)數(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對(duì)該組數(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ù)隔離級(jí)別的影響
| 事務(wù)隔離級(jí)別 | 臟讀 | 不可重復(fù)讀 | 幻讀 |
|---|---|---|---|
讀未提交 | read uncommitted
|
會(huì) | 會(huì) | 會(huì) |
讀已提交 | read committed
|
不會(huì) | 會(huì) | 會(huì) |
可重復(fù)讀 | repeatable read
|
不會(huì) | 不會(huì) | 會(huì) |
串行化 | serializable
|
不會(huì) | 不會(huì) | 不會(huì) |
事務(wù)隔離性說明
- 隔離級(jí)別越高,越能保證數(shù)據(jù)的完整性和一致性,但是對(duì)并發(fā)性能的影響也越大。
- 事務(wù)隔離級(jí)別為讀提交時(shí),寫數(shù)據(jù)只會(huì)鎖住相應(yīng)的行
- 事務(wù)隔離級(jí)別為串行化時(shí),讀寫數(shù)據(jù)都會(huì)鎖住整張表