Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SET tx_isolation versus SET TRANSACTION ISOLATION LEVEL

Tags:

mysql

In MySQL (notably 5.5), it seems that we have two different methods to set the transaction isolation level. I just would like to know if I am right in thinking that

SET TRANSACTION ISOLATION LEVEL REPEATABLE READ

does exactly the same as

SET tx_isolation = 'REPEATABLE-READ'

and that this is true for the other possible isolation levels as well.

EDIT 1

I have not been precise enough. @danihp's answer is correct given my question. But actually, I intended to ask about

SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ

versus

SET SESSION tx_isolation = 'REPEATABLE-READ'

(note the SET SESSION instead of SET).

Are they exactly the same?

like image 328
Binarus Avatar asked Oct 18 '22 02:10

Binarus


1 Answers

Server system variables:

You can use SET tx_isolation = 'REPEATABLE-READ' to set default isolation level on server system variables. "The MySQL server maintains many system variables that indicate how it is configured" This variable sets the default transaction level.

Current transaction:

You can use SET TRANSACTION ISOLATION LEVEL REPEATABLE READ to change isolation level from default to new level on your current transaction.

Notice:

"transaction_isolation was added in MySQL 5.7.20 as an alias for tx_isolation, which is now deprecated and is removed in MySQL 8.0. Applications should be adjusted to use transaction_isolation in preference to tx_isolation."

References:

  • 5.1.5 Server System Variables sysvar_tx_isolation
  • 5.1.5 Server System Variables sysvar_transaction_isolation
  • 13.3.6 SET TRANSACTION Syntax

Edited

You can use transactions to "encapsulate" several statements as a single operation. When you start a transaction, just at this time, you can change isolation level for this new transaction:

START TRANSACTION;
/** change isolation level here with SET TRANSACTION statement 
to avoid default isolation level **/
SELECT @A:=SUM(salary) FROM table1 WHERE type=1;
UPDATE table2 SET summary=@A WHERE type=1;
COMMIT;
like image 175
dani herrera Avatar answered Nov 03 '22 21:11

dani herrera