mySQL - Set isolation level using PHP's mysqli

倾然丶 夕夏残阳落幕 提交于 2019-12-05 13:15:00

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!