編輯:問題是關於爲什麼使用setAutoCommit(false)
是「沒有活動事務」例外的解決方案。 忘記這個問題,因爲這不是正確的解決方案(至少在我的情況下)。如果有人遇到同樣的問題,我會在這裏留下問題。有關更多詳情,請參閱下面的答案。學說setAutoCommit(false)方法和「沒有活動事務」消息
================
下面的代碼中的Symfony 2.7工作得很好,但一個更新的Symfony 2.8後(併到最新版本DoctrineBundle
),一There is no active transaction
拋出異常:
private function getSynchronization() {
$lock_repo = $this->entityManager->getRepository('MyAppBundle\Entity\DBLock');
$this->entityManager->getConnection()->beginTransaction();
try {
$sync = $lock_repo->findOneByUser($this->getUser());
if (!$lock) {
$lock = new DBLock();
} else {
if ($lock->isActive()) {
// ... Exception: Process already running
}
$expected_version = $lock->getVersion();
$this->entityManager->lock($lock, LockMode::OPTIMISTIC, $expected_version);
}
$sync->setActive(false);
$this->entityManager->persist($sync);
$this->entityManager->flush();
$this->entityManager->getConnection()->commit();
// EXCEPTION on this line
$this->entityManager->lock($lock, LockMode::NONE);
}
catch(\Exception $e) {
$this->entityManager->getConnection()->rollback();
throw new ProcessException($e->getMessage());
}
...
}
經過一番搜索我another post找到了解決辦法。加入後,以下行的一切工作正常:
private function getSynchronization() {
$lock_repo = $this->entityManager->getRepository('MyAppBundle\Entity\DBLock');
$this->entityManager->getConnection()->beginTransaction();
// ADDED LINE
$this->entityManager->getConnection()->setAutoCommit(false);
try {
...
所以,問題不在於如何解決問題,但解決方案是如何工作的...
我感到相當的困惑Doctrine docs該setAutoCommit()
方法:
爲了有一個連接上
connect()
自動打開了一個新的交易,並commit()
或後,您可以禁用與setAutoCommit(false)
自動提交模式下,我不明白這一點。
這是否意味着使用commit()
時,開始/創建beginTransaction()
的交易現在自動關閉?所以爲了能夠在使用commit()
後能夠使用lock(...)
,我必須首先開始一個新的交易。我可以通過再次調用beginTransaction()
來手動執行此操作,或者通過之前設置setAutoCommit(false)
來自動執行此操作。 這是正確的嗎?
這是最新的學說版本的變化嗎?我沒有在更新說明中找到任何關於更新Symfony/Doctrine之前的代碼工作得很好。
非常感謝!