Is it still good practice to use and share a single instance of MongoClient?

66 views
Skip to first unread message

JohnF

unread,
Nov 18, 2015, 2:30:28 PM11/18/15
to mongodb-casbah-users
Several (old) answers on Stackoverflow contain the following quote of the casbah docs:

If you are using in a web serving environment, for example, you should create a single MongoClient instance, and you can use it in every request. The MongoClient object maintains an internal pool of connections to the database (default maximum pool size of 100). For every request to the DB (find, insert, etc) the Java thread will obtain a connection from the pool, execute the operation, and release the connection.

However, I'n not able to find this text in the current casbah docs.

I'm wondering if it is still best practice to use one single MongoClient instance (e.g. via singleton) wihtin the whole application? Another answer I found claims that several MongoClient instances share the same connection pool anyway.
What's the best approach? Thanks.


Ross Lawley

unread,
Nov 18, 2015, 2:35:04 PM11/18/15
to mongodb-ca...@googlegroups.com
Hi,

Casbah wraps the Java driver, which creates a collection pool per MongoClient instance.  So a single instance is preferred as its more efficient, than spinning one up per thread.  The Java driver docs covers this: http://mongodb.github.io/mongo-java-driver/3.0/driver/getting-started/quick-tour/#mongoclient

I hope that helps!

Ross

--
You received this message because you are subscribed to the Google Groups "mongodb-casbah-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-casbah-u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--


{ name     : "Ross Lawley",
  title    : "Senior Software Engineer",
  location : "London, UK",
  twitter  : ["@RossC0", "@MongoDB"],
  facebook :"MongoDB"}

JohnF

unread,
Nov 18, 2015, 2:57:11 PM11/18/15
to mongodb-casbah-users
Helps a lot, thanks Ross


Am Mittwoch, 18. November 2015 20:35:04 UTC+1 schrieb Ross Lawley:
Hi,

Casbah wraps the Java driver, which creates a collection pool per MongoClient instance.  So a single instance is preferred as its more efficient, than spinning one up per thread.  The Java driver docs covers this: http://mongodb.github.io/mongo-java-driver/3.0/driver/getting-started/quick-tour/#mongoclient

I hope that helps!

Ross
On Wed, Nov 18, 2015 at 7:30 PM, JohnF <felixl...@gmail.com> wrote:
Several (old) answers on Stackoverflow contain the following quote of the casbah docs:

If you are using in a web serving environment, for example, you should create a single MongoClient instance, and you can use it in every request. The MongoClient object maintains an internal pool of connections to the database (default maximum pool size of 100). For every request to the DB (find, insert, etc) the Java thread will obtain a connection from the pool, execute the operation, and release the connection.

However, I'n not able to find this text in the current casbah docs.

I'm wondering if it is still best practice to use one single MongoClient instance (e.g. via singleton) wihtin the whole application? Another answer I found claims that several MongoClient instances share the same connection pool anyway.
What's the best approach? Thanks.


--
You received this message because you are subscribed to the Google Groups "mongodb-casbah-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-casbah-users+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages