Configuring MongoDB Connection Pool Settings with the mongo-java-driver API in Java

I’m using Morphia alongside the mongo-java-driver to establish a connection to my MongoDB server. Here’s how I initialize the connection:

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

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

In the example above, serverAddressList represents the host and port configuration, while mongoCredential is defined using Kerberos authentication like this:

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

I want to define specific connection pool parameters such as:

  • maxPoolSize
  • minPoolSize
  • maxIdleTimeMS

I discovered the classes ConnectionPoolSettings.Builder and com.mongodb.connection.ConnectionPoolSettings to set these parameters. I found a snippet that demonstrates this:

ConnectionPoolSettings poolSettings = 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(poolSettings)
    .build();

Could anyone guide me on how to implement these settings to obtain a MongoClient instance? I’m utilizing the following library:

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

Hey Emma! To configure connection pool settings with your current setup from the mongo-java-driver, you'll need to modify your initialization to include ConnectionPoolSettings. Here’s a concise example:

// Create ConnectionPoolSettings ConnectionPoolSettings poolSettings = ConnectionPoolSettings.builder() .minSize(5) // e.g., set your minPoolSize .maxSize(20) // e.g., set your maxPoolSize .maxConnectionIdleTime(1000) // set maxIdleTimeMS in milliseconds .build();

// Construct MongoClientOptions with pool settings
MongoClientOptions.Builder optionsBuilder = MongoClientOptions.builder()
.sslEnabled(true)
.sslInvalidHostNameAllowed(true)
.maxConnectionIdleTime(0)
.connectionPoolSettings(poolSettings); // integrate settings
MongoClientOptions clientOptions = optionsBuilder.build();

// Initialize MongoClient
MongoClient mongoClient = new MongoClient(serverAddressList, Arrays.asList(mongoCredential), clientOptions);

This should help you set up the MongoDB connection with the required pool parameters. Adjust the values as needed for your use case!

In addition to what CharlieLion22 has provided, it's essential to address the version of the mongo-java-driver you are using, since the configuration might slightly differ. Given that you're using version 3.4.3, it is crucial to utilize the available settings tools correctly.

While the example provided by CharlieLion22 demonstrates the ConnectionPoolSettings, integrating it into your existing MongoClientOptions setup requires some modifications. However, there's a more contemporary approach you could consider, using MongoClientSettings as you're looking to utilize both Morphia and MongoDB Driver.

// Creating ConnectionPoolSettings ConnectionPoolSettings poolSettings = ConnectionPoolSettings.builder() .minSize(5) // Set your minPoolSize .maxSize(50) // Set your maxPoolSize .maxConnectionIdleTime(1000) // Set maxIdleTimeMS in milliseconds .build();

// Creating MongoClientSettings including connection pool settings
MongoClientSettings clientSettings = MongoClientSettings.builder()
.applyToSslSettings(builder → builder.enabled(true)
.invalidHostNameAllowed(true))
.applyToConnectionPoolSettings(builder → builder.applySettings(poolSettings))
.credential(mongoCredential)
.applyToClusterSettings(builder →
builder.hosts(Arrays.asList(new ServerAddress(“your-host”, your-port))))
.build();

// Initialize MongoClient
MongoClient mongoClient = MongoClients.create(clientSettings);

This approach is more aligned with the recommended practices of newer MongoDB Driver versions, providing a better structure for your connection settings. Of course, tailor the parameter values to fit your application's specific requirements for optimal performance. This solution not only integrates seamlessly into your application but also promotes code maintainability and clarity.