tags:

views:

31

answers:

1

Hi,

Is there a "MongoUtil" class that allows me to get a new connection in a multi-threading environment (like the so famous HibernateUtil class)?

Thanks

A: 

The MongoDB Java driver internally manages a pool of connections (default size is 10). It can be accessed from multiple threads concurrently.

The normal scenario is that you have a single instance of the Mongo class, which all of your code uses (it is threadsafe).

Usually, you will get a different connection for every database call, if that is a problem (because you want to talk to the same node all the time), you can ask to get the same connection every time as well (requestStart/requestDone).

If you want to not use the pool at all, and get "your own" connection, you can create a new Mongo instance.

Thilo
So for example I create a static instance of Mongo that i share through all the DAOs, and then I ask for the specific DB at DAO level: DB db = MongoUtil.getInstance().getMongo.getDB("database"); - The problem then is that I have to authenticate the user to every DB instance every time (db.authenticate(username, password)). Isn't this a resource-consuming process?
Marco
Not sure about that. I assume you can authenticate just once per DB. But this is a rare use-case. Most users run MongoDB without authentication in a trusted environment.
Thilo