PDO事务是否会阻塞,直到所有其他事务都被提交?

时间:2022-01-13 19:23:53

If I am correct, PDO transactions are not really transactions: they merely turn off autocommits (See documentation).

如果我是正确的,PDO事务并不是真正的事务:它们只是关闭自动提交(参见文档)。

So if I have a transaction A which deletes a row and transaction B which modifes the same row, the following could happen.

因此,如果我有一个删除行的事务A和修改同一行的事务B,则可能发生以下情况。

  1. Transaction A starts and turns off autocommit.
  2. 事务A启动并关闭自动提交。

  3. Transaction B starts and turns off autocommit.
  4. 事务B启动并关闭自动提交。

  5. Transaction A deletes a row.
  6. 事务A删除一行。

  7. Transaction B modifies the same row.
  8. 事务B修改同一行。

  9. Transaction A finishes and commits changes.
  10. 事务A完成并提交更改。

  11. Transaction B finishes and commits changes.
  12. 事务B完成并提交更改。

But at step 6, the row has been already deleted in step 5. What would happen?

但是在第6步,该行已在步骤5中删除。会发生什么?

Also, how can I make sure to let Transaction B to block until Transaction A is committed? FYI, I am using PDO with mysql with innoDB.

另外,如何确保在事务A提交之前让事务B阻塞?仅供参考,我正在使用带有innoDB的mysql的PDO。

Thank you.

1 个解决方案

#1


0  

As @eggyal pointed out, transactions can occur at the same time but a DELETE statment puts an exclusive lock on the row until the transaction is committed, so Transaction B will automatically block at step 4 until Transaction A is committed. An exclusive lock block all writes and also reads.

正如@eggyal指出的那样,事务可以同时发生,但DELETE语句会在事务提交之前对该行进行独占锁定,因此事务B将在步骤4自动阻塞,直到提交事务A.独占锁定块所有写入和读取。

#1


0  

As @eggyal pointed out, transactions can occur at the same time but a DELETE statment puts an exclusive lock on the row until the transaction is committed, so Transaction B will automatically block at step 4 until Transaction A is committed. An exclusive lock block all writes and also reads.

正如@eggyal指出的那样,事务可以同时发生,但DELETE语句会在事务提交之前对该行进行独占锁定,因此事务B将在步骤4自动阻塞,直到提交事务A.独占锁定块所有写入和读取。