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

HTML form tag tutorial (2):

This tutorial introduces the application of vario...

JavaScript String Object Methods

Table of contents Methods of String Object Method...

MySQL partition table is classified by month

Table of contents Create a table View the databas...

How to use Volume to transfer files between host and Docker container

I have previously written an article about file t...

JavaScript+HTML to implement student information management system

Table of contents 1. Introduction 2. Rendering 3....

MySQL 8.0.15 download and installation detailed tutorial is a must for novices!

This article records the specific steps for downl...

How to view Linux ssh service information and running status

There are many articles about ssh server configur...

MySQL master-slave data is inconsistent, prompt: Slave_SQL_Running: No solution

This article uses an example to describe the solu...

Vue implements weather forecast function

This article shares the specific code of Vue to r...

MySQL series: redo log, undo log and binlog detailed explanation

Implementation of transactions The redo log ensur...