How do MySQLi transactions work?
好吧,假设我们有多个用户。
在每次会话开始时执行
其中一个用户触发了一个操作,应该执行对数据库的原子查询。 PHP 提供了类似
的功能
1 2 3 4 5 6 7 8 9 10 11 | $db = mysqli_connect(DB_HOST, DB_USER, DB_PASS); /* ATOMIC_BEGIN */ mysqli_autocommit($db, false); mysqli_query($db,"BLAH1"); mysqli_query($db,"BLAH2"); mysqli_commit($db); mysqli_autocommit($db, true); /* ATOMIC_END */ mysqli_close($db); |
好吧,这似乎很简单。然而,
To determine the current state of autocommit use the SQL command SELECT @@autocommit.
确定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | $db = mysqli_connect(DB_HOST, DB_USER, DB_PASS); // This one is on the another thread or whatever // Runned by another user $db2 = mysqli_connect(DB_HOST, DB_USER, DB_PASS); /* ATOMIC_BEGIN */ mysqli_autocommit($db, false); mysqli_query($db,"BLAH1"); // This one is on the another thread or whatever // Runned by another user // Not autocommitted since we turned it off // Can be rolled back mysqli_query($db2,"QUERY FOR THE ANOTHER USER WHICH BREAKS OUR ATOMICITY AND RESULTS OF THE BLAH2 (E. G. LOGIN)"); mysqli_query($db,"BLAH2"); // Commits both $db and $db2 queries mysqli_commit($db); mysqli_autocommit($db, true); /* ATOMIC_END */ mysqli_close($db); |
这是错误的并且完全没有意义:其他用户只有在
我想知道隔离用户之间的原子事务的正确方法或我在思想中犯的错误。我的目标是在
你称之为