I have a Servlet
with static Mongo = new Mongo()
and Morphia morphia = new Morphia()
objects. Each time GET
is invoked, I do the following:
doGet(...){
...
datastore = morphia.createDatastore(mongo, dbName);
...
}
I don't close datastore
, because there is no close method. Each time i invoke the servlet, number of used connections in mongo grow:
{ "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 connections with mongo and morphia and where the connection is really leaking? Thanks.
You can create a
singleton
Datastore
objectand now you can reuse the same
Datastore
object in your application