標籤:使用 io art cti ar sql mysql table
建立表t1
CREATE TABLE `t1` ( `a` int(11) NOT NULL, `b` int(11) DEFAULT NULL, PRIMARY KEY (`a`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;
查看mysql系統層級的交易隔離等級:
mysql> SELECT @@global.tx_isolation;+-----------------------+| @@global.tx_isolation |+-----------------------+| REPEATABLE-READ |+-----------------------+1 row in set (0.00 sec)
查看mysql會話層級的交易隔離等級
mysql> SELECT @@tx_isolation;+-----------------+| @@tx_isolation |+-----------------+| REPEATABLE-READ |+-----------------+1 row in set (0.00 sec)mysql> SELECT @@session.tx_isolation;+------------------------+| @@session.tx_isolation |+------------------------+| REPEATABLE-READ |+------------------------+1 row in set (0.00 sec)
設定系統層級的交易隔離等級
mysql> set global transaction isolation level read committed;Query OK, 0 rows affected (0.00 sec)mysql> SELECT @@global.tx_isolation;+-----------------------+| @@global.tx_isolation |+-----------------------+| READ-COMMITTED |+-----------------------+1 row in set (0.00 sec)mysql> SELECT @@tx_isolation;+-----------------+| @@tx_isolation |+-----------------+| REPEATABLE-READ |+-----------------+1 row in set (0.00 sec)
設定會話層級的交易隔離等級
mysql> set session transaction isolation level read committed;Query OK, 0 rows affected (0.00 sec)mysql> SELECT @@global.tx_isolation;+-----------------------+| @@global.tx_isolation |+-----------------------+| READ-COMMITTED |+-----------------------+1 row in set (0.00 sec)mysql> SELECT @@tx_isolation;+----------------+| @@tx_isolation |+----------------+| READ-COMMITTED |+----------------+1 row in set (0.00 sec)
mysql預設是自動認可事務的,查看autocommit變數
mysql> select @@autocommit;+--------------+| @@autocommit |+--------------+| 1 |+--------------+1 row in set (0.01 sec)
設定mysql不自動認可事務
mysql> set autocommit = 0;Query OK, 0 rows affected (0.00 sec)mysql> select @@autocommit;+--------------+| @@autocommit |+--------------+| 0 |+--------------+1 row in set (0.01 sec)
使用rollback復原事務
mysql> select * from t1;Empty set (0.00 sec)mysql> insert into t1 values (51, 3000);Query OK, 1 row affected (0.00 sec)mysql> select * from t1;+----+------+| a | b |+----+------+| 51 | 3000 |+----+------+1 row in set (0.00 sec)mysql> rollback;Query OK, 0 rows affected (0.03 sec)mysql> select * from t1;Empty set (0.00 sec)mysql>
使用start transaction;或begin;顯示的開啟一個事務
mysql> start transaction;Query OK, 0 rows affected (0.00 sec)mysql> begin;Query OK, 0 rows affected (0.00 sec)mysql> insert into t1 values (51, 3000);Query OK, 1 row affected (0.00 sec)mysql> select * from t1;+----+------+| a | b |+----+------+| 51 | 3000 |+----+------+1 row in set (0.00 sec)mysql> rollback;Query OK, 0 rows affected (0.04 sec)mysql> select * from t1;Empty set (0.00 sec)mysql> commit;Query OK, 0 rows affected (0.00 sec)mysql> select * from t1;Empty set (0.00 sec)
使用savepoint在事務中建立一個儲存點(可以在一個事務中建立多個儲存點)
mysql> begin;Query OK, 0 rows affected (0.00 sec)mysql> insert into t1 values (51, 3000);Query OK, 1 row affected (0.00 sec)mysql> select * from t1;+----+------+| a | b |+----+------+| 51 | 3000 |+----+------+1 row in set (0.00 sec)mysql> savepoint tx1;Query OK, 0 rows affected (0.00 sec)mysql> insert into t1 values (52, 3000);Query OK, 1 row affected (0.00 sec)mysql> select * from t1;+----+------+| a | b |+----+------+| 51 | 3000 || 52 | 3000 |+----+------+2 rows in set (0.00 sec)mysql> rollback to tx1;Query OK, 0 rows affected (0.00 sec)mysql> select * from t1;+----+------+| a | b |+----+------+| 51 | 3000 |+----+------+1 row in set (0.00 sec)mysql> commit;Query OK, 0 rows affected (0.04 sec)
以上是mysql命令列是對交易隔離等級和事務提交的基本操作。現在說明以下操作基於可重複復讀的隔離等級,事務不自動認可。
mysql> set global transaction isolation level repeatable read;Query OK, 0 rows affected (0.00 sec)mysql> set session transaction isolation level repeatable read;Query OK, 0 rows affected (0.00 sec)mysql> set autocommit = 0;Query OK, 0 rows affected (0.00 sec)
====END====