简体   繁体   English

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

[英]Do PDO transactions block until all other transactions are committed?

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. 事务A启动并关闭自动提交。
  2. Transaction B starts and turns off autocommit. 事务B启动并关闭自动提交。
  3. Transaction A deletes a row. 事务A删除一行。
  4. Transaction B modifies the same row. 事务B修改同一行。
  5. Transaction A finishes and commits changes. 事务A完成并提交更改。
  6. Transaction B finishes and commits changes. 事务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? 另外,如何确保在事务A提交之前让事务B阻塞? FYI, I am using PDO with mysql with innoDB. 仅供参考,我正在使用带有mysql的PDO和innoDB。

Thank you. 谢谢。

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. 正如@eggyal指出的那样,事务可以同时发生,但是DELETE语句会在事务提交之前对该行进行排他锁定,因此事务B将在步骤4自动阻塞,直到提交事务A. An exclusive lock block all writes and also reads. 独占锁定块所有写入和读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM