Java – mongodb connection pooling

connection-poolingjavamongodb

I am using Java driver to access mongodb. I assume the db connection pooling is internally handled by the driver.But my connection count getting increased every time I access db.

This is my serverStatus log.

"connections" : {
            "current" : 276,
            "available" : 543
    }

Do I need to explicitly close mongo connections? how should I manage connection pooling in java?

Best Solution

You should use a single Mongo object, so it will do pooling for you. However, if you do use multiple objects, you do need to call .close() explicitly.

From: http://www.mongodb.org/display/DOCS/Java+Tutorial

The Mongo class is designed to be thread safe and shared among threads. Typically you create only 1 instance for a given DB cluster and use it across your app. If for some reason you decide to create many mongo intances, note that:

all resource usage limits (max connections, etc) apply per mongo instance to dispose of an instance, make sure you call mongo.close() to clean up resources