How to use Cashbah MongoDB connections?

Note: I understand that there is a similar question about SO , but it talks about the old version of Casbah, plus, the behavior explained in the answer is not what I see!

I got the impression that Casbah MongoClient processed the connection pool. However, in my process, I see a large and growing number of mongodb connections, which makes me doubt that this pool really exists.

Basically, this is what I am doing:

class MongodbDataStore { val mongoClient = MongoClient("host",27017)("database") var getObject1(): Object1 = { val collection = mongoClient("object1Collection") ... } var getObject2(): Object2 = { val collection = mongoClient("object2Collection") ... } } 

So, I never close MongoClient.

Should I close it after every request? Implement my own pool? What then?

thanks

+6
source share
1 answer

Casbah is a wrapper around the MongoDB Java client, so the connection is actually controlled by it.

According to the Java driver documentation ( http://docs.mongodb.org/ecosystem/drivers/java-concurrency/ ):

If you use web services in your environment, for example, you must create a single instance of MongoClient, and you can use it in every request. The MongoClient object supports an internal database connection pool (the default maximum pool size is 100). For each query to the database (find, insert, etc.), the Java thread will receive a connection from the pool, perform an operation and release the connection. This means that the connection used (socket) can be every time every time.

By the way, this is what I experienced in production. I have not seen any problems with this.

+7
source

Source: https://habr.com/ru/post/973306/


All Articles