[英]Already active transaction in PDO exception handling
I have a method which is called in a loop to store person objects in a database. 我有一个在循环中调用的方法,用于将人对象存储在数据库中。 Sometimes the loop breaks with the error: 有时循环会因错误而中断:
There is already an active transaction.
I think it has something to do with my exception handling: 我认为这与我的异常处理有关:
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;
}
}
}
Fix at $this->pdo->roolBack();
修复$this->pdo->roolBack();
to $this->pdo->rollBack();
到$this->pdo->rollBack();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.