MySQL command line control transaction

Source: Internet
Author: User
Tags savepoint mysql command line

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====


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.