开发者

mongoDB utility class

开发者 https://www.devze.com 2023-01-15 00:54 出处:网络
Is there a \"MongoUtil\" class that allows me to get a 开发者_高级运维new connection in a multi-threading environment (like the so famous HibernateUtil class)?

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消