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! ;)
再次感谢.
推荐答案
这是一个相当常见的问题 具有长时间运行的进程和连接.
This is a fairly common problem with long running processes and connections.
解决方案是检索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 (DoctrineDBALDBALException $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屋!