脚本宝典收集整理的这篇文章主要介绍了php – 如何使用Doctrine在死锁后重试事务?,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
$retry = 0; $done = false; while (!$done and $retry < 3) { try { $this->entITyManager->flush(); $done = true; } catch (\Exception $e) { sleep(1); $retry++; } } if ($retry == 3) { throw new Exception( "[Exception: MysqL Deadlock] Too many PEople accessing the server at the same time. Try again in few minutes" ); }
请注意,死锁和锁定等待是不同的事情.陷入僵局,没有“失败”的交易:他们都有罪.无法保证哪一个将被回滚.
您必须使用回滚,您的样式代码将插入重复.例如你应该:
$retry = 0; $done = false; $this->entityManager->getConnection()->beginTransaction(); // suspend auto-commit while (!$done and $retry < 3) { try { $this->entityManager->flush(); $this->entityManager->getConnection()->commit(); // commit if succesfull $done = true; } catch (\Exception $e) { $this->entityManager->getConnection()->rollback(); // transaction marked for rollback only $retry++; } }
希望这有帮助.
以上是脚本宝典为你收集整理的php – 如何使用Doctrine在死锁后重试事务?全部内容,希望文章能够帮你解决php – 如何使用Doctrine在死锁后重试事务?所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。