Search code examples
javamongodbmorphiamongo-java-driver

Setting Mongodb Connection Pool Options via mongo-java-driver Java API


Using Morphia and mongo-java-driver, I'm connecting to a mongo server:

MongoClientOptions.Builder builder = MongoClientOptions.builder();
        builder.sslEnabled(true);
        builder.sslInvalidHostNameAllowed(true);
        builder.maxConnectionIdleTime(0);
        MongoClientOptions sslOptions = builder.build();

        MongoClient mongoClient = new MongoClient(serverAddressList, Arrays.asList(mongoCredential), sslOptions);

Where serverAddressList is the host:port list and mongoCredential using kerberos auth are created as:

MongoCredential mongoCredential = MongoCredential.createGSSAPICredential("[email protected]");

I want to setup a custom Connection Pool Parameters using:

  1. maxPoolSize
  2. minPoolSize
  3. maxIdleTimeMS etc.

To achieve this I found ConnectionPoolSettings.Builder and com.mongodb.connection.ConnectionPoolSettings Classes.

I searched a sample code here:

ConnectionPoolSettings connectionPoolSettings = ConnectionPoolSettings
                .builder()
                .minSize(MIN_MONGO_POOL_SIZE)
                .maxSize(MONGO_POOL_SIZE)
                .build();
        MongoClientSettings settings = MongoClientSettings
                .builder()
                .readPreference(MONGO_READ_PREFERENCE)
                .credentialList(credentialsList).clusterSettings(clusterSettings)
                .connectionPoolSettings(connectionPoolSettings).build();

Can someone please suggest a way of applying these settings to get an Instance of MongoClient ?

Libraries which are being used:

dependencies {
    compile 'org.mongodb:mongodb-driver-sync:3.4.3'
  }

Solution

  • Finally figured out the correct way of setting the connection pool parameters, so answering my own question here.

    I browsed the source code for MongoClientOptions here where the connectionPool parameters are being set as follows:

    connectionPoolSettings = ConnectionPoolSettings
                        .builder()
                        .minSize(getMinConnectionsPerHost())
                        .maxSize(getConnectionsPerHost())
                        .maxWaitQueueSize(getThreadsAllowedToBlockForConnectionMultiplier()
                        * getConnectionsPerHost())
                        .maxWaitTime(getMaxWaitTime(), MILLISECONDS)
                        .maxConnectionIdleTime(getMaxConnectionIdleTime(), MILLISECONDS)
                        .maxConnectionLifeTime(getMaxConnectionLifeTime(), MILLISECONDS)
                        .build();
    

    We can see that the minSize and maxSize values are being fetched from getMinConnectionsPerHost(); and getConnectionsPerHost(); methods. So to change and customize the connection pool parameters at client code we can simply add maxConnectionIdleTime and minConnectionsPerHost values to the builder as follows:

    MongoClientOptions.Builder builder = MongoClientOptions.builder();
        builder.maxConnectionIdleTime(0);
        builder.minConnectionsPerHost(50);
        builder.connectionsPerHost(200);
        MongoClientOptions sslOptions = builder.build();
    

    This code successfully opens up the minimum specified connections to the mongodb server host.

    LOG : 00:20:00,714 INFO  [org.mongodb.driver.connection] (pool-1-thread-1) Opened connection [connectionId{localValue:50}] to server.com:27180