MongoDB - Java驱动程序中的内存泄漏问题

时间:2014-11-06 18:02:55

标签: java mongodb java-ee mongodb-java

我正在运行一个java项目并使用mongodb作为后端数据库。每次运行Web应用程序时,我都会收到以下警告

SEVERE: The web application [/Padua] appears to have started a thread named [MongoCleaner576026458]     but has failed to stop it. This is very likely to create a memory leak.
Aug 29, 2014 11:41:14 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads

我知道这个错误是因为mongoclient未正确关闭。但我确保在每个实例中关闭连接

以下是我的编码

public class DBUtils {
MongoClient mongoClient;
DB db;
DBCollection messages;
DBCollection customerDetails;
public MongoClient connectToDB(String dBName){
    try {
        mongoClient = new MongoClient("XX.YYY.ZZZ.AAA" , 27017 );
        db = mongoClient.getDB(dBName);
        db.createCollection("A", null);
        db.createCollection("B", null);         
        db.createCollection("C", null); 
        BasicDBObject msgindexobj = new BasicDBObject();
        msgindexobj.put("processedword", 1);
        msgindexobj.put("mobileID", 1);
        db.getCollection("Messages").ensureIndex(new BasicDBObject("location", "2dsphere"), "geospatial");
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return mongoClient;
}


public void disConnectFromDB(MongoClient mongoclient){
    if (mongoclient!=null){
        mongoclient.close();
        mongoclient=null;
    }       
}
}

我正在调用这种类型的代码来访问DB

DBUtils dbUtils=new DBUtils();
      mongoclient= dbUtils.connectToDB("pingpong");
      DB db = mongoclient.getDB("pingpong");
dbUtils.disConnectFromDB(mongoclient);  

我在这里做错了什么......请帮我解决这个问题。谢谢你的时间。

0 个答案:

没有答案