New Table T1
CREATE TABLE ' t1 ' (' a ' int (one) not null, ' B ' int (one) default NULL, PRIMARY KEY (' a ')) Engine=innodb default Charset=la Tin1;
To view the transaction isolation level at the MySQL system level:
Mysql> SELECT @ @global. tx_isolation;+-----------------------+| @ @global. tx_isolation |+-----------------------+| Repeatable-read |+-----------------------+1 row in Set (0.00 sec)
View the transaction isolation level at the MySQL session level
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)
Set the transaction isolation level at the system level
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)
To set the session-level transaction isolation LEVEL
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 default is to commit transactions automatically, view the autocommit variable
Mysql> SELECT @ @autocommit; +--------------+| @ @autocommit |+--------------+| 1 |+--------------+1 row in Set (0.01 sec)
Set MySQL not to commit transactions automatically
mysql> Set autocommit = 0; Query OK, 0 rows Affected (0.00 sec) mysql> SELECT @ @autocommit; +--------------+| @ @autocommit |+--------------+| 0 |+--------------+1 row in Set (0.01 sec)
Rolling back a transaction using 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 | |+----+------+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>
Use start transaction; begin; display to open a transaction
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 | |+----+------+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)
Create a savepoint in a transaction using SavePoint (you can create multiple savepoint in one transaction)
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)
The above is the basic operation of the MySQL command line for transaction isolation levels and transactional commits. It is now stated that the following actions are based on the isolation level of repeatable repetition and the transaction is not committed automatically.
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====