ChrisYez 2015-11-06 03:52 采纳率: 50%
浏览 2293

MongoDB 线程长连接问题

我在使用MongoDB的过程当中,遇到如下问题:
netstat -an|grep 27017

我的MongoDB一直存在4个ESTABLISHED 连接,一直未释放,即使任务已经执行完了。

代码如下,其中Client只会在系统启动的时候初始化一次,当然在系统停止contextDestroyed的时候Client也调用了close
public class ExportServer extends BasicMongoServer {

public final static Logger LOG = Logger.getLogger(ExportServer.class);

public static MongoClient mongoClient;
private static DB exportDB;
private static DBCollection exportCollection;

/**
初始化MongoDB Client
*/
public static void init(String add,String username,String password) throws UnknownHostException {

ArrayList<ServerAddress> addr = new ArrayList<ServerAddress>();
for (String s : add.split(",")) {
  addr.add(new ServerAddress(s.trim()));
}
if (addr.size() > 1) {
  mongoClient = new MongoClient(addr, getOptions());
} else {
  mongoClient = new MongoClient(add, getOptions());
}
mongoClient.setReadPreference(ReadPreference.secondaryPreferred());
exportDB = mongoClient.getDB("export");
exportDB.authenticate(username, password.toCharArray());
exportCollection = exportDB.getCollection("export");

if (exportCollection.getIndexInfo().size() == 0) {
  LOG.info("adding necessary indexes on export history collection");
  BasicDBObject compoundIndex = new BasicDBObject();
  compoundIndex.put("status", 1);
  exportCollection.ensureIndex(compoundIndex);
  compoundIndex.clear();
  compoundIndex.put("key", 1);
  exportCollection.ensureIndex(compoundIndex);
  compoundIndex.clear();
  compoundIndex.put("uid", 1);
  exportCollection.ensureIndex(compoundIndex);
}

}

/**
更新document
*/
public static void update(DBObject query, DBObject update) {
exportCollection.update(query, update);
}

/**
保存docuemnt
*/
public static void save(DBObject obj) {
exportCollection.save(obj);
}
}

}

这些代码当中也没有什么特别操作,包括在应用停止的时候我也会将Client关闭 

请问各位是否有任何建议或者解决思路。

谢谢。

  • 写回答

1条回答 默认 最新

  • oyljerry 2015-11-06 06:12
    关注

    用netstat -nlp等,查看一下对应的进程,然后就是对应的代码多看看是否有其他分支退出了,从而没有释放连接

    评论

报告相同问题?

悬赏问题

  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了