Change default Mongo connection pool size in spring-boot
Asked Answered
T

3

10

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!

Triangulate answered 7/11, 2017 at 15:2 Comment(5)
Actually that a "maximum 100" and it does not go and create 100 connections unless your application actually needs them. There is a "default" pool size ( typically 10, and this comes from the underlying driver ) which in most cases you really do not want to mess with as "connection pools are a good thing". You appear to be trying to set this to 1, and that would be bad. If you are seeing "lots of connections" then "these are not the settings you are looking for" and you should be looking "elsewhere" as to why your code is flooding the server with connections.Skillless
@NeilLunn so that means I don't have the control of connection pool size at this level? thanks lot for your input.Triangulate
@Triangulate sorry to pick on you, but why haven't you selected an answer for this question?Sematic
@Sematic this was a very old question and I got it resolved from outside. The answers have been received several years after and I wasn't following thread. I just marked one answer as accepted. Thanks for pointing out.Triangulate
@Triangulate no worries, and thanks for dealing with this!Sematic
P
15

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

Pirandello answered 17/2, 2019 at 9:23 Comment(0)
S
5

With updated Spring boot(2.0.0 +) and Mongo DB java(3.9 +) driver versions following code can be used for creating configurable mongo template in spring boot.

Most of the configurations that were earlier part of MongoClientOptions are moved to MongoClientSettings.

import com.mongodb.*;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;

import com.mongodb.connection.*;
import org.springframework.data.mongodb.core.MongoTemplate;

@Configuration
public class MongoConfig {

    //-- variables 

    @Bean(name = "mongoTemplate")
    public MongoTemplate getMongoTemplate(){
        MongoTemplate mongoTemplate = new MongoTemplate(getMongoClient(), mongoDatabaseName);
        return mongoTemplate;
    }

    private MongoClient getMongoClient(){
        List<ServerAddress> serverAddressList = new ArrayList<>();
        String[] hostPortList = mongoHostPortList.split(",");
        for (String serverAddress : hostPortList) {
            String[] hostPortArr = serverAddress.split(":");
            serverAddressList.add(new ServerAddress(hostPortArr[0], Integer.parseInt(hostPortArr[1])));
        }

        MongoClientSettings mongoSettingsProperties = getMongoClientSettings();
        MongoClient mongoClient = MongoClients.create(mongoSettingsProperties);
        return mongoClient;
    }

    private MongoClientSettings getMongoClientSettings() {
        return MongoClientSettings.builder()
                .applicationName(appName)
                .applyToSslSettings(sslBuilder ->
                        SslSettings.builder().
                                enabled(sslEnabled).
                                invalidHostNameAllowed(false).build())
                .applyToConnectionPoolSettings(connPoolBuilder ->
                        ConnectionPoolSettings.builder().
                                maxWaitTime(maxWaitTime, MILLISECONDS).
                                maxSize(connectionPoolMinSize).
                                maxSize(connectionPoolMaxSize).build())
                .applyToSocketSettings(socketBuilder ->
                        SocketSettings.builder().
                                connectTimeout(connectionTimeout,MILLISECONDS).build())
                .readPreference(ReadPreference.secondaryPreferred())
                .build();
    }
}

Above code is verified with dependencies -

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
        <version>2.3.1.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>3.11.2</version>
    </dependency>
Santana answered 10/7, 2020 at 7:33 Comment(0)
U
0

You can configure connection pool size via MongoDb uri parameters. Details - https://mcmap.net/q/1162326/-mongo-connection-pooling-changing-the-size-of-connection-pool

Underthecounter answered 18/5, 2018 at 8:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.