Example of viewing and modifying MySQL transaction isolation level

Example of viewing and modifying MySQL transaction isolation level

Check the transaction isolation level

In MySQL, you can view the current transaction isolation level through show variables like '%tx_isolation%' or select @@tx_isolation; statement.

The SQL statements and running results for viewing the current transaction isolation level are as follows:

mysql> show variables like '%tx_isolation%';
+---------------+-----------------+
| Variable_name | Value |
+---------------+-----------------+
| tx_isolation | REPEATABLE-READ |
+---------------+-----------------+
1 row in set, 1 warning (0.17 sec)
mysql> select @@tx_isolation;
+-----------------+
| @@tx_isolation |
+-----------------+
| REPEATABLE-READ |
+-----------------+
1 row in set, 1 warning (0.00 sec)

The results show that the current transaction isolation level of MySQL is REPEATABLE-READ.

In addition, you can use the following statements to query the global and session transaction isolation levels respectively:

SELECT @@global.tx_isolation;
SELECT @@session.tx_isolation;

Tip: In MySQL 8.0.3, the tx_isolation variable was replaced by the transaction_isolation variable. To query the transaction isolation level in MySQL 8.0.3, just replace the tx_isolation variable in the above query with the transaction_isolation variable.

Change the transaction isolation level

MySQL provides the SET TRANSACTION statement, which can change the transaction isolation level for a single session or globally. The syntax format is as follows:

SET [SESSION | GLOBAL] TRANSACTION ISOLATION LEVEL {READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ | SERIALIZABLE}

The SESSION and GLOBAL keywords are used to specify the scope of the modified transaction isolation level:

  • SESSION: Indicates that the modified transaction isolation level will be applied to all transactions in the current session (current cmd window);
  • GLOBAL: Indicates that the modified transaction isolation level will be applied to all transactions in all sessions (global), and the existing sessions will not be affected;
  • If SESSION and GLOBAL are omitted, the modified transaction isolation level will be applied to the next transaction that has not yet started in the current session.

Any user can change the transaction isolation level for a session, but only users with the SUPER privilege can change the global transaction isolation level.

If you use a normal user to modify the global transaction isolation level, you will be prompted with an error message that super privileges are required to perform this operation. The SQL statement and running results are as follows:

C:\Users\leovo>mysql -utestuser -p
Enter password: ******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 41
Server version: 5.7.29-log MySQL Community Server (GPL)
 
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> SET GLOBAL TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
ERROR 1227 (42000): Access denied; you need (at least one of) the SUPER privilege(s) for this operation
mysql> SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
Query OK, 0 rows affected (0.00 sec)

Example 1

Use the SET TRANSACTION statement to modify the session and global transaction isolation levels. The SQL statements and running results are as follows:

mysql> select @@session.tx_isolation;
+------------------------+
| @@session.tx_isolation |
+------------------------+
| SERIALIZABLE |
+------------------------+
1 row in set, 1 warning (0.00 sec)

mysql> SET GLOBAL TRANSACTION ISOLATION LEVEL REPEATABLE READ;
Query OK, 0 rows affected (0.00 sec)

mysql> select @@global.tx_isolation;
+-----------------------+
| @@global.tx_isolation |
+-----------------------+
| REPEATABLE-READ |
+-----------------------+
1 row in set, 1 warning (0.00 sec)

You can also use the set tx_isolation command to directly modify the transaction isolation level of the current session. The SQL statement and running results are as follows:

mysql> set tx_isolation='READ-COMMITTED';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> select @@session.tx_isolation;
+------------------------+
| @@session.tx_isolation |
+------------------------+
| READ-COMMITTED |
+------------------------+
1 row in set, 1 warning (0.00 sec)

Knowledge points supplement

Mysql transaction isolation level read commit flow chart

The above is the detailed content of the example of viewing and modifying the transaction isolation level of MySQL. For more information about viewing and modifying the transaction isolation level of MySQL, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of Mysql transaction isolation level read commit
  • Detailed explanation of the four transaction isolation levels in MySQL
  • Detailed explanation and comparison of the four transaction isolation levels in MySQL
  • In-depth analysis of MySQL transaction isolation and its impact on performance
  • Tutorial on the relationship between Innodb transaction isolation level and lock in MySQL
  • Introduction to MySQL database transaction isolation level (Transaction Isolation Level)
  • In-depth explanation of the locking mechanism in MySQL InnoDB
  • Analysis of MySQL lock mechanism and usage
  • In-depth understanding of Mysql transaction isolation level and locking mechanism issues

<<:  The order of event execution in the node event loop

>>:  Example of how to build a Harbor public repository with Docker

Recommend

How to configure MGR single master and multiple slaves in MySQL 8.0.15

1. Introduction MySQL Group Replication (MGR for ...

Implementation of HTML command line interface

HTML Part Copy code The code is as follows: <!D...

A brief talk about the diff algorithm in Vue

Table of contents Overview Virtual Dom principle ...

CSS3 speeds up and delays transitions

1. Use the speed control function to control the ...

Basic tutorial on controlling Turtlebot3 mobile robot with ROS

Chinese Tutorial https://www.ncnynl.com/category/...

Why TypeScript's Enum is problematic

Table of contents What happened? When to use Cont...

Implementation of waterfall layout in uni-app project

GitHub address, you can star it if you like it Pl...

The three new indexes added in MySQL 8 are hidden, descending, and functions

Table of contents Hidden, descending, and functio...

Front-end JavaScript operation principle

Table of contents 1. What is a JavaScript engine?...

Intellij IDEA quick implementation of Docker image deployment method steps

Table of contents 1. Docker enables remote access...

Detailed explanation of the process of building an MQTT server using Docker

1. Pull the image docker pull registry.cn-hangzho...

JavaScript object-oriented class inheritance case explanation

1. Object-oriented class inheritance In the above...