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

假如想象 提交于 2020-01-11 07:10:07

问题


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 running some jobs that can take a length of time that exceeds wait_timeout through a ZF2 CLI route, and unfortunately, the ER's connection dies by the time it needs to get used (when the job is done).

Need:

// do the long job

$sl = $this->getServiceLocator();
$mapper = $sl->get( 'doctrine_object_mapper' );
if( !$mapper->getRepository()->isAlive() ) // something like so
    $mapper->getRepository()->wakeTheHellUp();

Aren't those proper method names! ;)

Thanks again.


回答1:


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);


来源:https://stackoverflow.com/questions/15362070/zf2-doctrine2-server-has-gone-away-how-to-jog-an-old-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!