ZF2 + Doctrine2: Server has gone away - how to jog an old connection?

后端 未结 1 1391
梦谈多话
梦谈多话 2021-01-15 13:10

Thanks in advance for your help.

I\'m wondering if anyone quickly knows what functions to call on the Entity Repository to jog its reconnection if it is dead. I am

相关标签:
1条回答
  • 2021-01-15 13:57

    This is a fairly common problem with long running processes and connections.

    The solution is to retrieve the ORM's DBAL connection and re-create it if the connection was lost (ensuring it didn't die during a transaction). This is obviously annoying, but it's the only way of doing it right now:

    // note - you need a ServiceManager here, not just a generic service locator
    $entityMAnager = $serviceManager->get('entity_manager');
    $connection    = $entityManager->getConnection();
    
    try {
        // dummy query
        $connection->query('SELECT 1');
    } catch (\Doctrine\DBAL\DBALException $e) {
        if ($connection->getTransactionIsolation()) {
            // failed in the middle of a transaction - this is serious!
            throw $e;
        }
    
        // force instantiation of a new entity manager
        $entityManager = $serviceManager->create('entity_manager');
    }
    
    $this->doFoo($entityManager);
    
    0 讨论(0)
提交回复
热议问题