我有一个循环调用的方法,用于将人物对象存储在数据库中。有时循环会出现错误:
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 :(得分:1)
修复$this->pdo->roolBack();
至$this->pdo->rollBack();