首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >偶尔对mongo说"ConnectionError:无法连接到数据库“

偶尔对mongo说"ConnectionError:无法连接到数据库“
EN

Stack Overflow用户
提问于 2011-03-13 13:19:27
回答 2查看 492关注 0票数 0

我们目前正在测试一个基于django的项目,该项目使用MongoEngine作为持久层。MongoEngine是基于pymongo的,我们使用的是1.6版,我们运行的是mongo的单实例安装。

我们注意到的是,偶尔,在大约5分钟内,无法与mongo实例建立连接。有没有人遇到过这样的行为?关于如何提高可靠性有什么建议吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-03-14 07:10:12

我们在AutoReconnect上遇到了一个问题,听起来和你描述的很相似。我最终在我的<project>/__init__.py文件中添加了pymongo:

代码语言:javascript
复制
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...) 

这为我们解决了问题,但您可能会描述一些不同的东西?

票数 2
EN

Stack Overflow用户

发布于 2012-05-24 03:54:07

这里是另一个解决方案,它使用子类化而不是猴子补丁,并处理在建立初始连接或访问数据库时可能引发的错误。我只是对Connection/ReplicasetConnection进行了子类化,并在实例化和任何方法调用期间处理了引发的AutoReconnect错误。您可以在构造函数中指定重试次数和重试之间的休眠时间。

你可以在这里看到要点:https://gist.github.com/2777345

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5287621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档