我們知道Mysql的存儲引擎有很多種,默認的為InnoDB,它也是mysql中唯一支持事務(wù)的存儲引擎。
一、事務(wù)的基本要素(ACID)
1、原子性(Atomicity):事務(wù)開始后所有操作,要么全部做完,要么全部不做,不可能停滯在中間環(huán)節(jié)。事務(wù)執(zhí)行過程中出錯,會回滾到事務(wù)開始前的狀態(tài),所有的操作就像沒有發(fā)生一樣。也就是說事務(wù)是一個不可分割的整體,就像化學(xué)中學(xué)過的原子,是物質(zhì)構(gòu)成的基本單位。
2、一致性(Consistency):事務(wù)開始前和結(jié)束后,數(shù)據(jù)庫的完整性約束沒有被破壞 。比如A向B轉(zhuǎn)賬,不可能A扣了錢,B卻沒收到。
3、隔離性(Isolation):同一時間,只允許一個事務(wù)請求同一數(shù)據(jù),不同的事務(wù)之間彼此沒有任何干擾。比如A正在從一張銀行卡中取錢,在A取錢的過程結(jié)束前,B不能向這張卡轉(zhuǎn)賬。
4、持久性(Durability):事務(wù)完成后,事務(wù)對數(shù)據(jù)庫的所有更新將被保存到數(shù)據(jù)庫,不能回滾。
二、事務(wù)的并發(fā)特性
1. 臟讀:事務(wù)A讀取了事務(wù)B更新的數(shù)據(jù),然后B回滾操作,那么A讀取到的數(shù)據(jù)是臟數(shù)據(jù)
2. 不可重復(fù)讀:事務(wù)A在一次事務(wù)中多次讀取同一數(shù)據(jù),但是事務(wù)B在數(shù)據(jù)A多次讀取的過程中,對數(shù)據(jù)做了更新并提交,導(dǎo)致事務(wù)A多次讀取同一數(shù)據(jù)時,結(jié)果不一致。
3. 幻讀:系統(tǒng)管理員A將數(shù)據(jù)庫中所有學(xué)生的成績從具體分數(shù)改為ABCDEFG等級,但是系統(tǒng)管理員B就在這個時候插入了一條具體分數(shù)的記錄,當(dāng)系統(tǒng)管理員A修改結(jié)束后發(fā)現(xiàn)還有一條記錄沒有改過來,就好像發(fā)生了幻覺一樣,這就叫幻讀。
不可重復(fù)讀和幻讀很容易混淆,不可重復(fù)讀側(cè)重于修改,幻讀側(cè)重于新增或刪除。解決不可重復(fù)讀的問題只需鎖住滿足條件的行,解決幻讀需要鎖表
三、Mysql事務(wù)隔離級別
下表為事務(wù)的隔離級別與可能會引發(fā)的并發(fā)問題
事務(wù)隔離級別 | 臟讀 | 不可重復(fù)讀 | 幻讀 |
---|---|---|---|
讀未提交(read-uncommitted) | 是 | 是 | 是 |
不可重復(fù)讀(read-committed) | 否 | 是 | 是 |
可重復(fù)讀(repeatable-read) | 否 | 否 | 是 |
串行化(serializable) | 否 | 否 | 否 |
mysql8中使用select @@transaction_isolation;
查看數(shù)據(jù)庫默認隔離級別
+-------------------------+
| @@transaction_isolation |
+-------------------------+
| REPEATABLE-READ |
+-------------------------+
1 row in set (0.01 sec)
四、隔離級別演示
- 先查看我們的數(shù)據(jù)庫存儲引擎
show variables like '%storage_engine%';
+----------------------------------+-----------+
| Variable_name | Value |
+----------------------------------+-----------+
| default_storage_engine | InnoDB |
| default_tmp_storage_engine | InnoDB |
| disabled_storage_engines | |
| internal_tmp_disk_storage_engine | InnoDB |
| internal_tmp_mem_storage_engine | TempTable |
+----------------------------------+-----------+
- 讀未提交
1.客戶端A
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 mg_user;
+-------------+----------------------------------+
| mg_username | mg_password |
+-------------+----------------------------------+
| zcl | 202cb962ac59075b964b07152d234b70 |
+-------------+----------------------------------+
1 row in set (0.00 sec)
- 在客戶端A的事務(wù)提交執(zhí)勤啊,打開另一個客戶端B,更新表
mg_user
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> update mg_user set mg_password = 10086 where mg_username = 'zcl';
Query OK, 1 row affected (0.23 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from mg_user;
+-------------+-------------+
| mg_username | mg_password |
+-------------+-------------+
| zcl | 10086 |
+-------------+-------------+
1 row in set (0.00 sec)
- 這時,雖然客戶端B的事務(wù)還沒提交,但是客戶端A就可以查詢到B已經(jīng)更新的數(shù)據(jù):
mysql> select * from mg_user;
+-------------+-------------+
| mg_username | mg_password |
+-------------+-------------+
| zcl | 10086 |
+-------------+-------------+
1 row in set (0.01 sec)
- 一旦客戶端B的事務(wù)因為某種原因回滾,所有的操作都將會被撤銷,那客戶端A查詢到的數(shù)據(jù)其實就是臟數(shù)據(jù):
mysql> rollback;
Query OK, 0 rows affected (0.15 sec)
mysql> select * from mg_user;
+-------------+----------------------------------+
| mg_username | mg_password |
+-------------+----------------------------------+
| zcl | 202cb962ac59075b964b07152d234b70 |
+-------------+----------------------------------+
1 row in set (0.00 sec)
- 在客戶端A執(zhí)行
select * from mg_user;
mysql> select * from mg_user;
+-------------+----------------------------------+
| mg_username | mg_password |
+-------------+----------------------------------+
| zcl | 202cb962ac59075b964b07152d234b70 |
+-------------+----------------------------------+
1 row in set (0.00 sec)
- 讀已提交
- 客戶端A
mysql> set session transaction isolation level read committed;
Query OK, 0 rows affected (0.00 sec)
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from mg_user;
+-------------+----------------------------------+
| mg_username | mg_password |
+-------------+----------------------------------+
| zcl | 10086 |
+-------------+----------------------------------+
1 row in set (0.00 sec)
- 在客戶端A的事務(wù)提交之前,打開另一客戶端B,更新表account;
mysql> set session transaction isolation level read committed;
Query OK, 0 rows affected (0.00 sec)
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> update mg_user set mg_password = 10010 where mg_username = 'zcl';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from mg_user;
+-------------+-------------+
| mg_username | mg_password |
+-------------+-------------+
| zcl | 10010 |
+-------------+-------------+
1 row in set (0.00 sec)
- 這時客戶端B還沒提交,客戶端A不能查詢到B已經(jīng)更新的數(shù)據(jù),解決了臟讀問題;
mysql> select * from mg_user;
+-------------+-------------+
| mg_username | mg_password |
+-------------+-------------+
| zcl | 10086 |
+-------------+-------------+
1 row in set (0.00 sec)
- 客戶端B事務(wù)提交
mysql> commit;
Query OK, 0 rows affected (0.07 sec)
mysql> select * from mg_user;
+-------------+-------------+
| mg_username | mg_password |
+-------------+-------------+
| zcl | 10010 |
+-------------+-------------+
1 row in set (0.00 sec)
- 客戶端A執(zhí)行與上一步相同的查詢,結(jié)果與上一步不一致,即產(chǎn)生了不可重復(fù)讀的問題。
mysql> select * from mg_user;
+-------------+-------------+
| mg_username | mg_password |
+-------------+-------------+
| zcl | 10086 |
+-------------+-------------+
1 row in set (0.00 sec)
mysql> select * from mg_user;
+-------------+-------------+
| mg_username | mg_password |
+-------------+-------------+
| zcl | 10010 |
+-------------+-------------+
1 row in set (0.00 sec)
- 可重復(fù)讀
- 打開客戶端A,設(shè)置當(dāng)前事務(wù)模式為repeatable read
mysql> set session transaction isolation level repeatable read;
Query OK, 0 rows affected (0.00 sec)
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from mg_user;
+-------------+-------------+
| mg_username | mg_password |
+-------------+-------------+
| zcl | 10010 |
+-------------+-------------+
1 row in set (0.00 sec)
- 在客戶端A的事務(wù)提交之前,打開另一個客戶端B,更新表account并提交
mysql> update mg_user set mg_password = 10086 where mg_username = 'zcl';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> commit;
Query OK, 0 rows affected (0.14 sec)
mysql> select * from mg_user;
+-------------+-------------+
| mg_username | mg_password |
+-------------+-------------+
| zcl | 10086 |
+-------------+-------------+
1 row in set (0.00 sec)
- 在客戶端A查詢表account的所有記錄,沒有出現(xiàn)不可重復(fù)讀的問題。
mysql> select * from mg_user;
+-------------+-------------+
| mg_username | mg_password |
+-------------+-------------+
| zcl | 10010 |
+-------------+-------------+
1 row in set (0.00 sec)
mysql> select * from mg_user;
+-------------+-------------+
| mg_username | mg_password |
+-------------+-------------+
| zcl | 10010 |
+-------------+-------------+
1 row in set (0.00 sec)
4 . 在客戶端執(zhí)行字符串拼接,mg_password的值變?yōu)?008611,它用的是步驟2中的10086來計算的,值也就變?yōu)榱?008611.可重復(fù)讀的隔離級別下使用了MVCC機制,select操作不會更新版本號,是快照讀(歷史版本);insert、update和delete會更新版本號,是當(dāng)前讀(當(dāng)前版本)。
mysql> update mg_user set mg_password = concat(mg_password,'11') where mg_username = 'zcl';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from mg_user;
+-------------+-------------+
| mg_username | mg_password |
+-------------+-------------+
| zcl | 1008611 |
+-------------+-------------+
1 row in set (0.00 sec)
- 重新打開客戶端B,插入一條新數(shù)據(jù)后提交
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into mg_user values('cl','10000');
Query OK, 1 row affected (0.05 sec)
mysql> commit;
Query OK, 0 rows affected (0.04 sec)
mysql> select * from mg_user;
+-------------+-------------+
| mg_username | mg_password |
+-------------+-------------+
| cl | 10000 |
| zcl | 10086 |
+-------------+-------------+
2 rows in set (0.00 sec)
- 在客戶端A查詢記錄,沒有查出新增數(shù)據(jù),所以出現(xiàn)了幻讀
mysql> select * from mg_user;
+-------------+-------------+
| mg_username | mg_password |
+-------------+-------------+
| zcl | 1008611 |
+-------------+-------------+
1 row in set (0.00 sec)
- 串行化
- 打開一個客戶端A,并設(shè)置當(dāng)前事務(wù)模式為serializable,查詢表
mysql> set session transaction isolation level serializable;
Query OK, 0 rows affected (0.00 sec)
mysql> start transaction;
Query OK, 0 rows affected (0.06 sec)
mysql> select * from mg_user;
+-------------+-------------+
| mg_username | mg_password |
+-------------+-------------+
| cl | 10000 |
| zcl | 1008611 |
+-------------+-------------+
2 rows in set (0.00 sec)
- 打開一個客戶端B,并設(shè)置當(dāng)前事務(wù)模式為serializable,插入一條記錄報錯,表被鎖了插入失敗。mysql中事務(wù)隔離級別為serializable時會鎖表,因此不會出現(xiàn)幻讀的情況,這種隔離級別并發(fā)性極低,開發(fā)中很少會用到。
mysql> set session transaction isolation level serializable;
Query OK, 0 rows affected (0.00 sec)
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into account values(5,'tom',0);
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
五、間隙鎖與MVCC模式
- 間隙鎖
間隙鎖是innodb中行鎖的一種,但是這種鎖鎖住的不止一行數(shù)據(jù),它鎖住的是多行,是一個數(shù)據(jù)范圍。間隙鎖的主要作用是為了防止出現(xiàn)幻讀,但是它會把鎖定范圍擴大,有時候也會給我們帶來麻煩。
這幅圖中多了一個gap鎖,而且gap鎖不是加在記錄上的,倒是像加在兩條記錄之間的,為的是防止別的事務(wù)在事務(wù)A多次讀取同一數(shù)據(jù)時,插入新的滿足條件的記錄并提交。 - mvcc(多版本并發(fā)控制)
何為多版本并發(fā)控制?即在每一行后面增加兩個隱藏列,記錄創(chuàng)建版本號和刪除版本號,而每一個事務(wù)啟動的時候,都有一個唯一遞增的版本號
- 在插入操作時:記錄的創(chuàng)建版本號就是當(dāng)前事務(wù)版本號
id | name | create version | delete version |
---|---|---|---|
1 | test | 1 |
2、在更新操作的時候,采用的是先標(biāo)記舊的那行記錄為已刪除,并且刪除版本號是事務(wù)版本號,然后插入一行新的記錄的方式。
比如,針對上面那行記錄,事務(wù)Id為2 要把name字段更新
id | name | create version | delete version |
---|---|---|---|
1 | test | 1 | 2 |
1 | new_name | 2 |
3.刪除操作也一樣
id | name | create version | delete version |
---|---|---|---|
1 | new_name | 2 | 3 |
-
查詢操作
從上面的描述可以看到,在查詢時符合以下兩個條件的記錄才能被查詢到: - 刪除版本號大于當(dāng)前事務(wù)版本號,即刪除操作是在當(dāng)前事務(wù)啟動之后做的
- 創(chuàng)建版本號小于或者等于當(dāng)前事務(wù)版本號,記錄創(chuàng)建是在事務(wù)之前啟動的。
這里很巧妙的通過版本號來減少了鎖的爭奪,相比于鎖它提高了系統(tǒng)性能
只有rc和rr模式兩種事務(wù)隔離界別才能使用mvcc