Mongolian morphine leakage

I have Servletwith objects static Mongo = new Mongo()and Morphia morphia = new Morphia(). Each time it is called GET, I do the following:

doGet(...){
...
datastore = morphia.createDatastore(mongo, dbName);
...
}

I do not close datastorebecause there is no closing method. Every time I call a servlet, the number of connections used in mongo grows:

{ "current" : 113, "available" : 706, "totalCreated" : NumberLong(122) }
> db.serverStatus().connections { "current" : 115, "available" : 704, "totalCreated" : NumberLong(124) }
> db.serverStatus().connections { "current" : 116, "available" : 703, "totalCreated" : NumberLong(125) }
> db.serverStatus().connections { "current" : 121, "available" : 698, "totalCreated" : NumberLong(130) }
> db.serverStatus().connections { "current" : 122, "available" : 697, "totalCreated" : NumberLong(131) }
> db.serverStatus().connections { "current" : 128, "available" : 691, "totalCreated" : NumberLong(137) }

What is the right way to close ties with mongo and morphine and where does the leak really occur? Thank.

+3
source share
1 answer

You can create an object singleton Datastore

public enum MongoDBHelper { // the best way to implement singletons, due to the author of Effective Java  
INSTANCE;

private DB db;
private Datastore datastore;

private final String SERVER_URL = "...";
private final int SERVER_PORT = ...;
private final String USERNAME= "...";
private final String PASSWORD = "...";
private final String DATABASE_NAME = "...";

private MongoDBHelper() {

    try {

        MongoClient mongoClient = new MongoClient(SERVER_URL, SERVER_PORT);

        this.db = mongoClient.getDB(DATABASE_NAME);
        this.db.authenticate(USERNAME,PASSWORD.toCharArray());

        Morphia morphia = new Morphia();

        this.datastore = morphia.createDatastore(mongoClient, DATABASE_NAME);

        morphia.mapPackage("package");
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }

}

public DB getDB() {
    return this.db;
}

public Datastore getDatastore() {
    return this.datastore;
}
}

and now you can reuse the same object Datastorein your application

 Datastore datastore = MongoDBHelper.INSTANCE.getDatastore()
+6
source

All Articles