Django “偶尔”;ConnectionError:无法连接到数据库";到蒙哥
我们目前正在测试一个基于django的项目,该项目使用MongoEngine作为持久层。MongoEngine基于pymongo,我们使用的是1.6版,我们正在运行mongo的单实例设置Django “偶尔”;ConnectionError:无法连接到数据库";到蒙哥,django,mongodb,pymongo,Django,Mongodb,Pymongo,我们目前正在测试一个基于django的项目,该项目使用MongoEngine作为持久层。MongoEngine基于pymongo,我们使用的是1.6版,我们正在运行mongo的单实例设置 我们注意到,有时,大约5分钟内,无法建立到mongo实例的连接。有没有人见过这种行为?关于如何提高可靠性有什么建议吗 我们在AutoReconnect上遇到了一个问题,它听起来与您描述的内容类似。我最后在我的/\uuuu init\uuuuu.py文件中修补了pymongo: from pymongo.curs
我们注意到,有时,大约5分钟内,无法建立到mongo实例的连接。有没有人见过这种行为?关于如何提高可靠性有什么建议吗 我们在
AutoReconnect
上遇到了一个问题,它听起来与您描述的内容类似。我最后在我的/\uuuu init\uuuuu.py
文件中修补了pymongo:
from pymongo.cursor import Cursor
from pymongo.errors import AutoReconnect
from time import sleep
import sys
AUTO_RECONNECT_ATTEMPTS = 10
AUTO_RECONNECT_DELAY = 0.1
def auto_reconnect(func):
"""
Function wrapper to automatically reconnect if AutoReconnect is raised.
If still failing after AUTO_RECONNECT_ATTEMPTS, raise the exception after
all. Technically this should be handled everytime a mongo query is
executed so you can gracefully handle the failure appropriately, but this
intermediary should handle 99% of cases and avoid having to put
reconnection code all over the place.
"""
def retry_function(*args, **kwargs):
attempts = 0
while True:
try:
return func(*args, **kwargs)
except AutoReconnect, e:
attempts += 1
if attempts > AUTO_RECONNECT_ATTEMPTS:
raise
sys.stderr.write(
'%s raised [%s] -- AutoReconnecting (#%d)...\n' % (
func.__name__, e, attempts))
sleep(AUTO_RECONNECT_DELAY)
return retry_function
# monkeypatch: wrap Cursor.__send_message (name-mangled)
Cursor._Cursor__send_message = auto_reconnect(Cursor._Cursor__send_message)
# (may need to wrap some other methods also, we'll see...)
这为我们解决了问题,但您可能描述了一些不同的内容?我们遇到了与您描述的内容类似的
AutoReconnect
问题。我最后在我的/\uuuu init\uuuuu.py
文件中修补了pymongo:
from pymongo.cursor import Cursor
from pymongo.errors import AutoReconnect
from time import sleep
import sys
AUTO_RECONNECT_ATTEMPTS = 10
AUTO_RECONNECT_DELAY = 0.1
def auto_reconnect(func):
"""
Function wrapper to automatically reconnect if AutoReconnect is raised.
If still failing after AUTO_RECONNECT_ATTEMPTS, raise the exception after
all. Technically this should be handled everytime a mongo query is
executed so you can gracefully handle the failure appropriately, but this
intermediary should handle 99% of cases and avoid having to put
reconnection code all over the place.
"""
def retry_function(*args, **kwargs):
attempts = 0
while True:
try:
return func(*args, **kwargs)
except AutoReconnect, e:
attempts += 1
if attempts > AUTO_RECONNECT_ATTEMPTS:
raise
sys.stderr.write(
'%s raised [%s] -- AutoReconnecting (#%d)...\n' % (
func.__name__, e, attempts))
sleep(AUTO_RECONNECT_DELAY)
return retry_function
# monkeypatch: wrap Cursor.__send_message (name-mangled)
Cursor._Cursor__send_message = auto_reconnect(Cursor._Cursor__send_message)
# (may need to wrap some other methods also, we'll see...)
这为我们解决了问题,但您可能描述了一些不同的内容?这里是另一个解决方案,它使用子类化而不是猴子补丁,并处理在建立初始连接或访问数据库时可能出现的错误。我只是将Connection/ReplicateSetConnection子类化,并在实例化和任何方法调用期间处理引发的AutoReconnect错误。您可以在构造函数中指定重试次数和两次重试之间的睡眠时间
您可以在这里看到要点:这里是另一个解决方案,它使用子类而不是猴子补丁,并处理在建立初始连接或访问数据库时可能引发的错误。我只是将Connection/ReplicateSetConnection子类化,并在实例化和任何方法调用期间处理引发的AutoReconnect错误。您可以在构造函数中指定重试次数和两次重试之间的睡眠时间
你可以在这里看到要点:似乎可以解决我们的问题。。我会让你知道我们是否注意到了一个不同点。最近的发展有什么帮助吗?似乎它可以解决我们的问题。。我会让你知道,如果我们注意到一个不同的地方,最近的发展有帮助吗?