2017-08-10 94 views
0

我有一個方法在循環中調用以將人員對象存儲在數據庫中。有時有錯誤的循環中斷:在PDO異常處理中已經有活動的事務

There is already an active transaction. 

我認爲這是與我的異常處理:

class MainClass { 
    public function doWork() { 
    foreach ($persons as $person) { 
     try { 
     $importer->storePerson($person); 
     } catch (RuntimeException $e) { 
     Log::err($e->getMessage()); 
     return; 
     } 
    } 
    } 
} 

class Importer { 
    public function storePerson(Person $person) { 
    try { 
     $this->pdo->beginTransaction(); 
     $this->executeInsert($person); 
     $this->executeAnotherInsert($person); 
     $this->pdo->commit(); 
    } catch (PDOException $e) { 
     $this->pdo->rollBack(); 
     throw $e; 
    } 
    } 
} 
+0

你應該檢查[提交] (http://php.net/manual/en/pdo.commit.php)成功與否。也許[關閉遊標](http://php.net/manual/en/pdostatement.closecursor.php)。它是[rollBack()](http://php.net/manual/en/pdo.rollback.php)。 – Mikey

+0

你能解釋一下你的意思嗎?哪個光標? – altralaser

+0

或者是異常處理是正確的,並且我開始我的應用程序兩次以使導入運行並行是一個問題? – altralaser

回答

1

修復在$this->pdo->roolBack();$this->pdo->rollBack();