2

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

1 Answer 1

3

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() |
+----------------+
| root@%         |
+----------------+

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

mysql> select current_user();
+----------------+
| current_user() |
+----------------+
| root@%         |
+----------------+

mysql> SET @@transaction_isolation = CASE CURRENT_USER() WHEN 'root@%' 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 'root@%' 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!

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.