MySQL 的事務和事務的隔離級別

mysql數據庫,當且僅當引擎是InnoDB,才支持事務。
對於一個MYSQL數據庫(InnoDB),事務的開啓與提交模式無非下面這兩種情況:
1>若參數autocommit=0,事務則在用戶本次對數據進行操作時自動開啓,在用戶執行commit命令時提交,用戶本次對數據庫開始進行操作到用戶執行commit命令之間的一系列操作爲一個完整的事務週期。若不執行commit命令,系統則默認事務回滾。總而言之,當前情況下事務的狀態是自動開啓手動提交。

2>若參數autocommit=1(系統默認值),事務的開啓與提交又分爲兩種狀態:
手動開啓手動提交:當用戶執行start transaction命令時(事務初始化),一個事務開啓,當用戶執行commit命令時當前事務提交。從用戶執行start transaction命令到用戶執行commit命令之間的一系列操作爲一個完整的事務週期。若不執行commit命令,系統則默認事務回滾。

②自動開啓自動提交:如果用戶在當前情況下(參數autocommit=1)未執行start transaction命令而對數據庫進行了操作,系統則默認用戶對數據庫的每一個操作爲一個孤立的事務,也就是說用戶每進行一次操作系都會即時提交或者即時回滾。這種情況下用戶的每一個操作都是一個完整的事務週期。

事務的隔離級別分爲:未提交讀(read uncommitted)、已提交讀(read committed)、可重複讀(repeatable read)、串行化(serializable)。
我的本地 MySQL 默認的設置是:可重複讀

mysql> show variables like '%iso%';
+-----------------------+-----------------+
| Variable_name         | Value           |
+-----------------------+-----------------+
| transaction_isolation | REPEATABLE-READ |
| tx_isolation          | REPEATABLE-READ |
+-----------------------+-----------------+
2 rows in set, 1 warning (0.05 sec)
mysql> 
  1. 在 Console 1: a 和 b 二選一即可
    a. 執行 set autocmmit = 0; 即關閉mysql數據庫的自動提交屬性;從上可知其實這步不是必須的
    b. 執行 begin; 即開啓事務(這個 begin 不是必須的,好像設置了 autocmmit 爲 0 就會開啓事務)
    c. 執行 select * from student where id = 3

  2. 在 Console 2:
    d. 執行 update student set name = concat(name,‘3’) where id = 3;

  3. 在 Console 1:
    e. 執行 select * from student where id = 3 可以看到和步驟 1 看到的結果一樣,並沒有顯示步驟2的更新

示例:

Console 1:
	mysql> use robertdb;
	Reading table information for completion of table and column names
	You can turn off this feature to get a quicker startup with -A
	Database changed
	mysql> 
	mysql> begin;
	Query OK, 0 rows affected (0.00 sec)
	mysql> 
	mysql> select * from student where id = 3 ;
	+----+------+--------+---------+----------------------------+----------------------------+------------+
	| id | name | gender | address | create_date                | update_date                | country_id |
	+----+------+--------+---------+----------------------------+----------------------------+------------+
	|  3 | rob  |      4 | BeiJing | 2018-11-12 22:16:01.000000 | 2018-11-12 22:16:01.000000 |          1 |
	+----+------+--------+---------+----------------------------+----------------------------+------------+
	1 row in set (0.00 sec)
	mysql> 
	mysql> select * from student where id = 3;
	+----+------+--------+---------+----------------------------+----------------------------+------------+
	| id | name | gender | address | create_date                | update_date                | country_id |
	+----+------+--------+---------+----------------------------+----------------------------+------------+
	|  3 | rob  |      4 | BeiJing | 2018-11-12 22:16:01.000000 | 2018-11-12 22:16:01.000000 |          1 |
	+----+------+--------+---------+----------------------------+----------------------------+------------+
	1 row in set (0.00 sec)
	mysql> 

Console2:
	mysql> use robertdb;
	Reading table information for completion of table and column names
	You can turn off this feature to get a quicker startup with -A
	Database changed
	mysql> 
	mysql> select * from student;
	+-----+--------------+--------+--------------+----------------------------+----------------------------+------------+
	| id  | name         | gender | address      | create_date                | update_date                | country_id |
	+-----+--------------+--------+--------------+----------------------------+----------------------------+------------+
	|   1 | c'ppp\'ppp"x |      2 | HeNan        | 2018-11-12 22:15:48.000000 | 2019-10-30 15:50:50.452264 |          1 |
	|   3 | rob          |      4 | BeiJing      | 2018-11-12 22:16:01.000000 | 2018-11-12 22:16:01.000000 |          1 |
	|   4 | gww          |      1 | BeiJing      | 2018-11-12 22:16:06.000000 | 2020-01-10 14:58:19.914886 |          1 |
	|   5 | sxn          |      2 | HeNan        | 2018-11-12 22:16:15.000000 | 2018-11-12 22:16:15.000000 |          1 |
	|   7 | syy          |      2 | MoNaShi      | 2018-11-14 10:21:24.179000 | 2018-11-14 10:21:24.179000 |          3 |
	|   8 | cjs          |      2 | ShanXi       | 2018-11-14 13:52:41.193000 | 2018-11-14 13:52:41.193000 |          1 |
	+-----+--------------+--------+--------------+----------------------------+----------------------------+------------+
	6 rows in set (0.00 sec)
	mysql>
	mysql> update student set name = concat(name,'3') where id  = 3;
	Query OK, 1 row affected (0.08 sec)
	Rows matched: 1  Changed: 1  Warnings: 0
	mysql> 
	mysql> select * from student;
	+-----+--------------+--------+--------------+----------------------------+----------------------------+------------+
	| id  | name         | gender | address      | create_date                | update_date                | country_id |
	+-----+--------------+--------+--------------+----------------------------+----------------------------+------------+
	|   1 | c'ppp\'ppp"x |      2 | HeNan        | 2018-11-12 22:15:48.000000 | 2019-10-30 15:50:50.452264 |          1 |
	|   3 | rob3         |      4 | BeiJing      | 2018-11-12 22:16:01.000000 | 2018-11-12 22:16:01.000000 |          1 |
	|   4 | gww          |      1 | BeiJing      | 2018-11-12 22:16:06.000000 | 2020-01-10 14:58:19.914886 |          1 |
	|   5 | sxn          |      2 | HeNan        | 2018-11-12 22:16:15.000000 | 2018-11-12 22:16:15.000000 |          1 |
	|   7 | syy          |      2 | MoNaShi      | 2018-11-14 10:21:24.179000 | 2018-11-14 10:21:24.179000 |          3 |
	|   8 | cjs          |      2 | ShanXi       | 2018-11-14 13:52:41.193000 | 2018-11-14 13:52:41.193000 |          1 |
	+-----+--------------+--------+--------------+----------------------------+----------------------------+------------+
	6 rows in set (0.01 sec)
	mysql> 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章