ZF2 + Doctrine2:服务器已经消失 - 如何慢跑旧连接?

Sae*_*ven 2 doctrine-orm zend-framework2

在此先感谢您的帮助.

我想知道是否有人很快就知道要在Entity Repository上调用什么函数来重新启动其重新连接(如果它已经死了).我正在通过ZF2 CLI路由运行一些可能花费一段时间超过wait_timeout的作业,不幸的是,ER的连接在需要使用时(作业完成时)就会消失.

需要:

// do the long job

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

不是那些正确的方法名称!;)

再次感谢.

Ocr*_*ius 5

对于长时间运行的进程和连接,这是一个相当常见的问题.

解决方案是检索ORM的DBAL连接,并在连接丢失时重新创建它(确保它在事务期间没有死亡).这显然很烦人,但这是现在做的唯一方法:

// 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);
Run Code Online (Sandbox Code Playgroud)

  • 作为对此的更新,DBAL连接现在支持`Doctrine\DBAL\Connection #ping()`,它允许在不使用特定于平台的SQL的情况下检查连接状态. (2认同)