MongoDB Java Driver database connection pooling with Tomcat MongoDB Java Driver database connection pooling with Tomcat mongodb mongodb

MongoDB Java Driver database connection pooling with Tomcat


We've been using the Java drivers via the CFMongoDB project and we use it as you describe, but in a ColdFusion application rather then in Java. Same idea though: one object is created and we reuse it and that object maintains the one connection to the Mongo server.

You can create one Mongo Java instance and it will maintain an internal pool of connections (default size of 10) - to you it's hidden and you don't need to worry about it. The Mongo Java docs recommend this:

http://www.mongodb.org/display/DOCS/Java+Driver+Concurrency

We have it running in production now and there have been no issues. Multiple web request threads use the same Mongo instance and Mongo is quick enough to deal with this using it's internal pool (we're doing logging so it can write very fast!).

It is worth remembering to call close() on any instances that you are finished with - this will stop connections getting used up on the Mongo server over time:

http://api.mongodb.org/java/2.5-pre-/com/mongodb/Mongo.html#close()

So in summary, don't worry about configuring Tomcat.

Hope that helps!