2016-05-29 60 views
0

我有一個ThruWay路由器(長時間運行的進程),客戶端使用wampcra進行身份驗證。我使用從Symfony服務容器中獲取的EntityManager來驗證密碼。當mySQL的wait_timeout達到時,客戶端不能再被認證。Doctrine EntityManager在mySQL連接超時後未關閉,如何重新連接?

我看過其他類似的問題進行解答,並嘗試了所有我能找到的,我有下面的代碼片斷到位:

if(!$this->em->isOpen()) { 
    $this->console->debug('EntityManager->isOpen() returned false.'); 
    $this->em = $this->registry->resetManager(); 
} else { 
    $this->console->debug('EntityManager->isOpen() returned true.'); 
} 
$b = $this->em->getConnection()->connect(); 
if($b) { 
    $this->console->debug('EntityManager reconnected.'); 
} else { 
    $this->console->debug('EntityManager still connected. Good.'); 
} 

我設置MySQL的WAIT_TIMEOUT 120(2分鐘)來調試這個問題。事實上,在這段時間之後,客戶端不能再連接來驗證它確實是超時問題的罪魁禍首。然而,就是isOpen()調用返回true和的getConnection() - >連接()返回false超時到期後也:

EntityManager->isOpen() returned true. 
EntityManager still connected. Good. 

超時後,即使forementioned代碼發生在地方的異常:

Warning: PDOStatement::execute(): MySQL server has gone away 

我在這裏做錯了什麼?如何檢測連接已關閉並重新打開?

回答

0

我或許有點快問這個問題,因爲我剛剛找到了答案,但別人可能會受益,所以我回答:

if(!$this->em->getConnection()->ping() || !$this->em->isOpen()) 

的中國平安()會如果MySQL返回false 「走開」並且運行的後續代碼將有效地重新建立連接。