mySQL - Set isolation level using PHP's mysqli
Asked Answered
P

2

5

How do I set the isolation level of a transaction to 'SERIALIZABLE' in PHP using mysqli? I have looked everywhere and I can't find any information on it.

Here is an explanation of the isolation levels.

Prehension answered 13/11, 2009 at 20:28 Comment(0)
H
9

You can just set the isolation level in a query before you run your statements. This assume that you do everything using the same session:

$mysqli = new mysqli('localhost', 'user', 'pass', 'db');
$mysqli->query("SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE");
...

You may also want to turn off autocommit before hand since it changes the way serializable isolation works.

Holleran answered 13/11, 2009 at 20:50 Comment(2)
Is there some more information regarding the 'You may also want to turn off autocommit before hand since it changes the way serializable isolation works.' comment?Bornie
dba.stackexchange.com/questions/175550/…Jink
J
0

Short answer:

$mysqli = new mysqli('localhost', 'user', 'pass', 'db');
$mysqli->query("SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE");

Long Answer: Unless you use the SESSION or GLOBAL modifier, setting the transaction level will only apply to the very next query.

tldr;

According to the MySQL documentation with InnoDB tables:

Without any SESSION or GLOBAL keyword:

The statement applies only to the next single transaction performed within the session.

Subsequent transactions revert to using the session value of the named characteristics.

The statement is not permitted within transactions

Note that setting the GLOBAL flag will affect all subsequent queries Existing sessions will not be affected.

Jink answered 31/10, 2018 at 14:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.