已经在PDO异常处理中处于活动状态

时间:2017-08-10 18:01:29

标签: php 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;
    }
  }
}

1 个答案:

答案 0 :(得分:1)

修复$this->pdo->roolBack();$this->pdo->rollBack();