如何处理MongoDB中的陈旧连接 [英] How to handle stale connections in MongoDB

查看:128
本文介绍了如何处理MongoDB中的陈旧连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Mongo中自动刷新过期连接的最佳方式是什么?

What is the best way of automatically refreshing stale connections in Mongo?

回收mongod服务后,我从Liferay Portlets获取此异常:

After recycling the mongod service I get this exception from my Liferay Portlets:

com.mongodb.MongoException$Network: can't call something : /127.0.0.1:27017/connection_test


推荐答案

我最后编写代码在每个 DBCollection 请求。

I ended up writing code that polls the connection before each DBCollection is requested.

private DBCollection safeColl(String pCollectionName, DBCollection pColl) {
    try {
        if (log.isDebugEnabled()) {
            log.debug("getting safe coll count on coll: " + pColl.getName());
        }
        pColl.count();
    } catch (MongoException e) {
        if (e.getMessage().startsWith("can't call something")) {
            pColl = getCollection(pCollectionName, true);
            return pColl;
        } else {
            throw e;
        }
    }
    return pColl;
}

这篇关于如何处理MongoDB中的陈旧连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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