偶尔出现“连接错误:无法连接到数据库”到蒙戈
我们目前正在测试一个基于 django 的项目,该项目使用 MongoEngine 作为持久层。 MongoEngine 基于 pymongo,我们使用 1.6 版本,并且运行 mongo 的单实例设置。
我们注意到,偶尔会在大约 5 分钟内无法与 mongo 实例建立连接。有人遇到过这样的行为吗?关于如何提高可靠性有什么建议吗?
We are currently testing a django based project that uses MongoEngine as the persistence layer. MongoEngine is based on pymongo and we're using version 1.6 and we are running a single instance setup of mongo.
What we have noticed is that occasionally, and for about 5 minutes, connections cannot be established to the mongo instance. Has anyone come across such behavior? any tips on how to improve reliability?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们遇到了
AutoReconnect
问题,听起来与您所描述的类似。我最终在我的/__init__.py
文件中对 pymongo 进行了猴子修补:这为我们解决了问题,但您可能会描述不同的东西?
We had an issue with
AutoReconnect
which sounds similar to what you are describing. I ended up monkeypatching pymongo in my<project>/__init__.py
file:This resolved the issue for us, but you might be describing something different?
这是另一种解决方案,它使用子类化而不是猴子修补,并处理建立初始连接或访问数据库时可能引发的错误。我只是对 Connection/ReplicasetConnection 进行子类化,并处理实例化和任何方法调用期间引发的 AutoReconnect 错误。您可以在构造函数中指定重试次数和重试之间的睡眠时间。
您可以在此处查看要点:https://gist.github.com/2777345
Here is another solution that uses subclassing instead of monkey patching, and handles errors that might be raised while establishing the initial connection or while accessing a database. I just subclassed Connection/ReplicasetConnection, and handled raised AutoReconnect errors during instantiation and any method invocations. You can specify the number of retries and sleep time between retries in the constructor.
You can see the gist here: https://gist.github.com/2777345