1

For my Application, I am trying to set min and maximum pool size for the connection. So, Can anyone help me how to do it with mongo Client.

Also not I saw the options through the MOngoClientURI but is there any other option with MongoClientOption or MongoClient.

My Current Code:

public void buildMongoClient() {
        mongoClient = new MongoClient(dbHostName, dbPort);
        mongoDatabase = mongoClient.getDatabase(DATABASE);
}

1 Answer 1

4

You can try something like this.

public void buildMongoClient() {
    MongoClientOptions.Builder clientOptions = new MongoClientOptions.Builder();
    clientOptions.minConnectionsPerHost();//min
    clientOptions.connectionsPerHost();//max
    mongoClient = new MongoClient(new ServerAddress(dbHostName, dbPort), clientOptions.build);
    mongoDatabase = mongoClient.getDatabase(DATABASE);
 }
Sign up to request clarification or add additional context in comments.

7 Comments

But here I am not getting option to define my own parameters. How to mention the connection pool I want like 500.
I'm not following you. clientOptions.connectionsPerHost(500) something like this.
Actually, I want to define the timeout and min max connection pool at time of application startup for mongo db.
but your post specifically asks about setting it through MongoClientOptions .Did I miss something ? I'm not sure what kind of application you have.
Nope. This answer solve my problem. Thanks for the help. Just one more thing. On my system start up I have created only one mongoDatabase and on every concurrent request I am using the same Mongo Dataabse object. So, is connection pool maintained within that object and on concurrent request serviing connection from that pool?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.