我正在寫一個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"
);
}
我的問題:是有機會這一做法將在數據庫中插入重複?如果是這樣,我該如何強制Doctrine回滾交易?
謝謝:)這至少給了我一個如何進行的想法。 – Rorchackh
事實上,這不再是事實,因爲在Exception'EntityManager'進入關閉狀態的情況下,它會拋出一個'ORMException',表示實體管理器在第二次嘗試時關閉。 Doctrine 2.4版本。* – Mantas
這是關閉時如何重置entitymanager https://codedump.io/share/rjB45oiwtqwo/1/doctrine2-the-entitymanager-is-closed-how-to-reset-entity-manager-in- Symfony2的 –