守护进程中的Doctrine2连接超时 [英] Doctrine2 connection timeout in daemon

查看:145
本文介绍了守护进程中的Doctrine2连接超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个长时间运行的守护进程(Symfony2 Command),可以在Redis中工作,并使用orm执行这些作业并写入数据库。

I have a long running daemon (Symfony2 Command) that gets work off a work queue in Redis, and performs those jobs and writes to the database using the orm.

我注意到,当工作人员等待工作时,工作人员因为连接到MySQL而退出时,会有一个倾向。

I noticed that when that there is a tendency for the worker to die because the connection to MySQL timed out when worker is idling waiting for work.

具体来说,我在日志中看到这一点:MySQL服务器已经消失。

Specifically, I see this in the log: MySQL Server has gone away.

有没有可以有原则自动重新连接?还是有一些方法我可以手动捕获异常并重新连接doctrine orm?

Is there anyway I can have doctrine automatically reconnect? Or is there some way I can manually catch the exception and reconnect the doctrine orm?

谢谢

推荐答案

看来,当Doctrine中的EntityManager遇到任何错误/异常时,连接被关闭,EntityManager已经死了。

It appears that whenever there is any error/exception encountered by the EntityManager in Doctrine, the connection is closed and the EntityManager is dead.

因为一般来说,所有东西都被包装在一个事务中,并且当$ entityManager-> flush()被调用时执行该事务,你可以尝试捕获该异常并尝试重新排除或放弃。

Since generally everything is wrapped in a transaction and that transaction is executed when $entityManager->flush() is called, you can try and catch the exception and attempt to re-excute or give up.

您可能希望检查异常的确切性质,更具体的捕获类型,无论是PDOException还是其他内容。

You may wish to examine the exact nature of the exception with more specific catch on the type, whether PDOException or something else.

对于MySQL Gone Away例外,您可以通过重置EntityManager来尝试重新连接。

For a MySQL has Gone Away exception, you can try to reconnect by resetting the EntityManager.

$managerRegistry = $this->getContainer()->get('doctrine');
$em = $managerRegistry->getEntityManager();
$managerRegistry->resetEntityManager();

这应该使$ em再次可用。请注意,您必须重新保留所有内容,因为这个$ em是新的。

This should make the $em usable again. Note that you would have to re-persist everything again, since this $em is new.

这篇关于守护进程中的Doctrine2连接超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆