ZF2 + Doctrine2:服务器已消失-如何启动旧连接? [英] ZF2 + Doctrine2: Server has gone away - how to jog an old connection?
问题描述
在此先感谢您的帮助。
我想知道是否有人快速知道在实体存储库上调用什么功能,如果它已死,则可以手动重新连接。我正在运行一些作业,通过ZF2 CLI路由可能要花费超过wait_timeout的时间,不幸的是,ER的连接在需要使用时(工作完成时)就断开了。
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).
需要:
// 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! ;)
再次感谢。
推荐答案
解决方案是检索ORM的 DBAL连接并在连接丢失的情况下重新创建它(确保它不会在事务处理期间死亡)。这显然很烦人,但这是目前唯一的方法:
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);
这篇关于ZF2 + Doctrine2:服务器已消失-如何启动旧连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!