Mongo Connection Pooling(Changing the size of connection pool)
Asked Answered
R

3

3

How to change the mongo connection pool size?

I have seen it is 100 by default. Is there a way to change this value?

I dont want to do it via spring, is there a way to configure it via MongoClient?

There is an option i see about mongoClientOptions but i dont see options to set connection pool

Rhinology answered 3/7, 2014 at 15:28 Comment(0)
A
6

You can build your own MongoClient instance using the MongoClientOptions.Builder.

MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
MongoClientOptions options = builder.connectionsPerHost(10).build();
MongoClient client = new MongoClient(listOfServers, options);
Authenticate answered 3/7, 2014 at 15:32 Comment(2)
so this no 10 is a connection pool? which is by default 100? Also if I have say 3 hosts i will have 30 connections in total in your case?Rhinology
Default 100 is probably the implementer think that's the best default setting for most people - I can't answer why. The above will set 10 connections (yes, pool) per host. If you have 3 hosts, it will be 10 per host and 30 in total.Authenticate
A
4

As another option(and more convenient for me), connection pool size can be changed via MongoDb URI. Sample:

 MONGODB_URI (mongo):   mongodb://user:password@localhost:27017/users_db?ssl=true&maxPoolSize=10&readPreference=primaryPreferred&replicaSet=Dev-shard-0&authSource=admin

Where maxPoolSize=10 param is max amount of connections. There are also some additional parameters for configuring connection pool in such way, for details - refer to the documentation - https://docs.mongodb.com/manual/reference/connection-string/

Across answered 18/5, 2018 at 8:46 Comment(0)
V
1

Extra options can also be set using MongoClientURI :

MongoClientOptions.Builder builder = new MongoClientOptions.Builder().connectionsPerHost(10));
MongoClientURI clientURI = new MongoClientURI(connectionURL, builder);
MongoClient mongoClient = new MongoClient(clientURI);
Vidal answered 14/6, 2019 at 12:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.