Change default Mongo connection pool size in spring-boot

wannix picture wannix · Nov 7, 2017 · Viewed 18.7k times · Source

I want to change the default size of connection pool provided by java mongodb driver which is 100 according to mongo docs.

Below is the mongo client bean which I used to customize the connection pool size (refered this question). I set both min and max connectionPerHost attributes to 1 and ran 10 parallel worker threads which interact with the DB to make sure that my change is applied.

@Bean
public Mongo mongo() throws Exception {
    MongoClientOptions.Builder clientOptions = new MongoClientOptions.Builder();
    clientOptions.minConnectionsPerHost(1);
    clientOptions.connectionsPerHost(1);
    MongoClient mongoClient = new MongoClient(new MongoClientURI(env.getProperty("mongodbhost"), clientOptions));
    return mongoClient;
}

Then I calculated the starting and ending time spots of each worker thread. So that I know for sure the threads are working parallely and my connection pool size haven't changed by these configuration. Could anyone help me to get through this please? any help would be highly appreciated!

Answer

Namjith Aravind picture Namjith Aravind · Feb 17, 2019

You can configure connection parameters by uri.

spring.data.mongodb.uri=mongodb://localhost:27017/?connectTimeoutMS=300000&minPoolSize=0&maxPoolSize=10&maxIdleTimeMS=900000

Please see the following documentation for other parameters.

https://docs.mongodb.com/manual/reference/connection-string/#connections-connection-options