守护进程中的 Doctrine2 连接超时

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

问题描述

我有一个长时间运行的守护进程(Symfony2 命令),它从 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.

无论如何我可以让学说自动重新连接吗?或者有什么方法可以手动捕获异常并重新连接学说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 has 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 连接超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)