新建表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====
MySql命令行控制事务
时间: 2024-10-10 18:37:32