MySQL,ODBC 數(shù)據(jù)庫(kù)事務(wù)
目前創(chuàng)新互聯(lián)已為上千多家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計(jì)、環(huán)江網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
多事務(wù)同時(shí)執(zhí)行:彼此之間互不影響的方式進(jìn)行并行;事務(wù)之間交互,通過數(shù)據(jù)集。
START TRANSACTION; 啟動(dòng)事務(wù)命令 數(shù)據(jù)庫(kù)只有啟動(dòng)了事務(wù)才允許回滾撤銷等操作。
且數(shù)據(jù)的engine引擎必須是innodb,才支持事務(wù)
ROLLBACK 回滾事務(wù),mysql只要沒有提交開啟了事務(wù)都可以進(jìn)行回滾操作。
COMMIT:事務(wù)提交,事務(wù)提交后無法進(jìn)行回滾操作。
如果沒有明確啟動(dòng)事務(wù): autocommit能實(shí)現(xiàn)自動(dòng)提交,每一個(gè)操作都直接提交;
所以建議:明確使用事務(wù),否則所有操作都被當(dāng)成一個(gè)事務(wù)來處理,并關(guān)閉自動(dòng)提交。
否則會(huì)浪費(fèi)mysql很多IO操作,每寫一條語句都執(zhí)行提交至持久性存儲(chǔ),很浪費(fèi)資源
事務(wù)的特性:
Atomicity:原子性,事務(wù)所引起的數(shù)據(jù)庫(kù)操作,要不都完成,要么都不執(zhí)行;
Consistency: 一致性,
Isolation:隔離性
事務(wù)調(diào)度:事務(wù)之間影響最小
MVCC:多版本并發(fā)控制
Durability:持久性 ,一旦事務(wù)成功完成,系統(tǒng)必須保證任何故障都不會(huì)引起事務(wù)表示出不一致性;
1、事務(wù)提交之前就已經(jīng)寫出數(shù)據(jù)至持久性存儲(chǔ);
2、結(jié)合事務(wù)日志完成;
事務(wù)日志:順序IO
數(shù)據(jù)文件:隨機(jī)IO
事務(wù)的狀態(tài):
活動(dòng)的:active
部分提交的:最后一條語句執(zhí)行后
失敗的:
終止的:
提交的:
及狀態(tài)間的轉(zhuǎn)換過程:
事務(wù)并發(fā)執(zhí)行的優(yōu)勢(shì):1、提高吞吐量和資源利用率 2、減少等待時(shí)間
事務(wù)調(diào)度:1、可恢復(fù)調(diào)度 2、無極聯(lián)調(diào)度
實(shí)例:
mysql> SELECT @@AUTOCOMMIT; #查詢自動(dòng)提交的狀態(tài)1為開啟,0為關(guān)閉;
+--------------+
| @@AUTOCOMMIT |
+--------------+
| 1 |
+--------------+
1 row in set (0.00 sec)
mysql> SET AUTOCOMMIT=0; #設(shè)定自動(dòng)提交關(guān)閉
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @@AUTOCOMMIT; #查詢自動(dòng)提交的狀態(tài)1為開啟,0為關(guān)閉;
+--------------+
| @@AUTOCOMMIT |
+--------------+
| 0 |
+--------------+
1 row in set (0.00 sec)
mysql> DELETE FROM student WHERE Name LIKE 'Li%'; #刪除Name字段包含Li的行
Query OK, 1 row affected (0.03 sec)
mysql> SELECT * FROM student; #Li哪行已經(jīng)被刪除
+-----+------------+-----+-----+
| SID | Name | Age | CID |
+-----+------------+-----+-----+
| 2 | Cheng Long | 0 | 2 |
| 3 | Yang Guo | 0 | 3 |
| 4 | Guo Jing | 0 | 4 |
+-----+------------+-----+-----+
3 rows in set (0.00 sec)
mysql> ROLLBACK; #回滾事務(wù),自動(dòng)提交關(guān)閉后,默認(rèn)就開啟了事務(wù),可以實(shí)現(xiàn)回滾等操作
Query OK, 0 rows affected (0.01 sec)
mysql> SELECT * FROM student; #刪除的哪行恢復(fù)了
+-----+------------+-----+-----+
| SID | Name | Age | CID |
+-----+------------+-----+-----+
| 1 | Li Lianjie | 0 | 1 |
| 2 | Cheng Long | 0 | 2 |
| 3 | Yang Guo | 0 | 3 |
| 4 | Guo Jing | 0 | 4 |
+-----+------------+-----+-----+
4 rows in set (0.00 sec)
保存點(diǎn):恢復(fù)到所定義的那個(gè)保存點(diǎn)SAVEPOINT,保存點(diǎn)名稱不能以數(shù)字開頭。
保存點(diǎn):SAVEPOINT savepoint_name; 保存以上操作為該保存點(diǎn)名稱
回滾保存點(diǎn):ROLLBACK TO savepoint_name; 回滾到該保存點(diǎn)之前的狀態(tài)
Usage:
mysql> START TRANSACTION; #啟動(dòng)事務(wù)
Query OK, 0 rows affected (0.00 sec)
mysql> select * FROM student;
+-----+------------+-----+-----+
| SID | Name | Age | CID |
+-----+------------+-----+-----+
| 1 | Li Lianjie | 0 | 1 |
| 2 | Cheng Long | 0 | 2 |
| 3 | Yang Guo | 26 | 3 |
| 4 | Guo Jing | 53 | 4 |
+-----+------------+-----+-----+
4 rows in set (0.00 sec)
mysql> SAVEPOINT a; #該保存點(diǎn)student表數(shù)據(jù)都存在
Query OK, 0 rows affected (0.00 sec)
mysql> select * FROM student;
+-----+------------+-----+-----+
| SID | Name | Age | CID |
+-----+------------+-----+-----+
| 1 | Li Lianjie | 0 | 1 |
| 2 | Cheng Long | 0 | 2 |
| 3 | Yang Guo | 26 | 3 |
| 4 | Guo Jing | 53 | 4 |
+-----+------------+-----+-----+
4 rows in set (0.00 sec)
mysql> DELETE FROM student WHERE SID=4; #刪除student表的SID為4的行
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM student;
+-----+------------+-----+-----+
| SID | Name | Age | CID |
+-----+------------+-----+-----+
| 1 | Li Lianjie | 0 | 1 |
| 2 | Cheng Long | 0 | 2 |
| 3 | Yang Guo | 26 | 3 |
+-----+------------+-----+-----+
3 rows in set (0.00 sec)
mysql> SAVEPOINT b; #該保存點(diǎn)student表數(shù)據(jù)的SID為4的行不存在了
Query OK, 0 rows affected (0.00 sec)
mysql> DELETE FROM student WHERE SID=3;
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM student;
+-----+------------+-----+-----+
| SID | Name | Age | CID |
+-----+------------+-----+-----+
| 1 | Li Lianjie | 0 | 1 |
| 2 | Cheng Long | 0 | 2 |
+-----+------------+-----+-----+
2 rows in set (0.00 sec)
mysql> SAVEPOINT c; #該保存點(diǎn)student表的SID為3和4的行都不存在
Query OK, 0 rows affected (0.00 sec)
mysql> ROLLBACK TO b; #回滾至保存點(diǎn)b,即student表,SID為4的行不存在的行
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM student;
+-----+------------+-----+-----+
| SID | Name | Age | CID |
+-----+------------+-----+-----+
| 1 | Li Lianjie | 0 | 1 |
| 2 | Cheng Long | 0 | 2 |
| 3 | Yang Guo | 26 | 3 |
+-----+------------+-----+-----+
3 rows in set (0.00 sec)
mysql> ROLLBACK TO a;#即所有數(shù)據(jù)都存在的那個(gè)點(diǎn)
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM student;
+-----+------------+-----+-----+
| SID | Name | Age | CID |
+-----+------------+-----+-----+
| 1 | Li Lianjie | 0 | 1 |
| 2 | Cheng Long | 0 | 2 |
| 3 | Yang Guo | 26 | 3 |
| 4 | Guo Jing | 53 | 4 |
+-----+------------+-----+-----+
4 rows in set (0.00 sec)