2016-10-12 29 views
0

編輯:問題是關於爲什麼使用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 docssetAutoCommit()方法:

爲了有一個連接上 connect()自動打開了一個新的交易,並commit()或後,您可以禁用與setAutoCommit(false)

自動提交模式下,我不明白這一點。

這是否意味着使用commit()時,開始/創建beginTransaction()的交易現在自動關閉?所以爲了能夠在使用commit()後能夠使用lock(...),我必須首先開始一個新的交易。我可以通過再次調用beginTransaction()來手動執行此操作,或者通過之前設置setAutoCommit(false)來自動執行此操作。 這是正確的嗎?

這是最新的學說版本的變化嗎?我沒有在更新說明中找到任何關於更新Symfony/Doctrine之前的代碼工作得很好。

非常感謝!

回答

0

如前所述我遇到了這個問題,在從Doctrine 2.4更新到2.5之後,調用lock($lock, LockMode::NONE)突然拋出了There is no active transaction異常。

我的解決方案是添加setAutoCommit(false),它在呼叫commit()後自動創建一個新的交易。它的工作,並不例外再次發生。 但是,這不是真正的/正確的解決方案,它會產生其他問題作爲副作用。

重新讀取Doctrine Update Notes後,我發現,正確的解決方案是使用lock($lock, null)而不是lock($lock, LockMode::NONE)。這是學說2.4和2.5之間的BC斷點。

也許我的問題和答案可以幫助遇到同樣問題的其他人。

相關問題