问题描述
- MongoDB 线程长连接问题
-
我在使用MongoDB的过程当中,遇到如下问题:
我的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关闭
请问各位是否有任何建议或者解决思路。
谢谢。
解决方案
用netstat -nlp等,查看一下对应的进程,然后就是对应的代码多看看是否有其他分支退出了,从而没有释放连接