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)
不是那些正确的方法名称!;)
再次感谢.
对于长时间运行的进程和连接,这是一个相当常见的问题.
解决方案是检索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)