2013-03-12 46 views
2

在此先感谢您的帮助。ZF2 + Doctrine2:服务器已经消失 - 如何慢跑旧连接?

我想知道如果有人很快知道什么函数调用实体库来缓慢重新连接,如果它是死的。我正在运行一些可能需要超过wait_timeout的时间(通过ZF2 CLI路由)的一些作业,不幸的是,ER的连接在需要使用时(作业完成时)就会消亡。

极品:

// do the long job 

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

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

再次感谢。

回答

5

这是一个fairly common problem长时间运行的进程和连接。

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

// 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

感谢Ocramius。我想我必须重构使用长期服务的工厂才能让SM进入。现在,我通过实现ServiceLocatorAwareInterface将SL放入,并在module.config.php中将其声明为一个Invokable。我会给它一个镜头并发回。非常感谢。 – Saeven 2013-03-13 03:28:05

+0

@Saeven主服务经理实际上是一个服务定位器。虽然你*可以*现在直接使用它,我建议有正确的类型暗示,以避免在未来的问题:) – Ocramius 2013-03-13 09:53:06

+1

作为后续问题,我昨天才解决 - 这是一个小写,可能有帮助。源文件,解释,以及为什么奥克拉米乌斯的帖子是一个垫脚石。值得称赞的是,他实际上通过IRC引导我走向最后的方向。 http://circlical.com/blog/2013/9/12/mysql-server-has-gone-away-atop-doctrine2-and-zend-framework-2 – Saeven 2013-09-13 12:37:11

相关问题