MySQL 8.0.24 – Change transaction_isolation based on incoming session

All we need is an easy explanation of the problem, so here it is.

I have a requirement for a newly added DB in MySQL instance to have READ-COMMITED as the transaction isolation level. While the rest of the DBs require the default REPEATABLE-READ.

I am trying to leverage init_connect system variable to check incoming session and based on that setting the transaction-isolation at session level.

Added below line to my.cnf and restarted the service and I can see it is reflecting post reboot, but when i logged in as test user i still see default transaction isolation which means my condition in init_connect is not working. What am i doing wrong here

init_connect = "SET @@transaction_isolation = CASE CURRENT_USER() WHEN '[email protected]' THEN 'READ-COMMITTED' ELSE @@transaction_isolation END;"

Thanks

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Don’t use the @@ sigil when you set the variable. You can use this when you use the value of a variable in an expression, but not when you set it.

Example:

mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.23    |
+-----------+

mysql> select current_user();
+----------------+
| current_user() |
+----------------+
| [email protected]%         |
+----------------+

mysql> select @@transaction_isolation;
+-------------------------+
| @@transaction_isolation |
+-------------------------+
| REPEATABLE-READ         |
+-------------------------+

mysql> select current_user();
+----------------+
| current_user() |
+----------------+
| [email protected]%         |
+----------------+

mysql> SET @@transaction_isolation = CASE CURRENT_USER() WHEN '[email protected]%' THEN 'READ-COMMITTED' ELSE @@transaction_isolation END;
Query OK, 0 rows affected (0.00 sec)

mysql> select @@transaction_isolation;
+-------------------------+
| @@transaction_isolation |
+-------------------------+
| REPEATABLE-READ         |
+-------------------------+

This does not change the variable. But when I omit the @@ sigil:

mysql> SET transaction_isolation = CASE CURRENT_USER() WHEN '[email protected]%' THEN 'READ-COMMITTED' ELSE @@transaction_isolation END;
Query OK, 0 rows affected (0.00 sec)

mysql> select @@transaction_isolation;
+-------------------------+
| @@transaction_isolation |
+-------------------------+
| READ-COMMITTED          |
+-------------------------+

It works!

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply