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 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示