Overview
In this guide, you can learn about how the Java Reactive Streams driver uses connection pools to manage connections to a MongoDB deployment and how you can configure connection pool settings in your application.
A connection pool is a cache of open database connections maintained by the Java Reactive Streams driver. When your application requests a connection to MongoDB, the Java Reactive Streams driver seamlessly gets a connection from the pool, performs operations, and returns the connection to the pool for reuse.
Connection pools help reduce application latency and the number of times new connections are created by the Java Reactive Streams driver.
Create a Connection Pool
Every MongoClient instance has a built-in connection pool for each server in your MongoDB topology. Connection pools open sockets on demand to support concurrent MongoDB operations in your multi-threaded application.
The maxPoolSize option sets the maximum size of each connection pool, which defaults to 100. If the number of in-use connections to a server reaches the value of maxPoolSize, the next request to that server will wait until a connection becomes available.
Each MongoClient instance opens two more sockets per server in your MongoDB topology for monitoring the server's state.
Configure a Connection Pool
You can specify settings for your connection pool by using either a connection string or by passing a MongoClientSettings object to the MongoClients.create() method.
Select the Connection String or MongoClientSettings tab to see the corresponding syntax:
The following are connection string settings you can use to configure your connection pool:
Setting | Description |
|---|---|
| Maximum number of connections a pool may establish concurrently. Default: |
| The maximum number of milliseconds that a connection can remain idle in the pool before being removed and closed. Set this value higher than your application's expected idle period but lower than firewall or proxy connection timeouts to prevent unexpected disconnections. Default: |
| Maximum number of connections opened in the pool. If an operation needs a new connection while the connection pool has Default: |
| Minimum number of connections opened in the pool. The value of Default: |
| This option is deprecated. You can configure this timeout by setting the client-level timeout instead. Maximum wait time in milliseconds that an operation can wait for a connection to become available. A value of Default: |
| Specifies the maximum amount of time, in milliseconds, the Java Reactive Streams driver will continue to use a pooled connection before closing the connection. A value of Default: |
For more information about these parameters, see the ConnectionString API documentation.
Chain the applyToConnectionPoolSettings() method to modify the way the driver manages its connection pool.
The following table describes the methods you can chain to your settings to modify the driver's behavior:
Method | Description |
|---|---|
| Adds a listener for connection pool-related events. |
| Uses the settings from a |
| Uses the connection pool settings specified in a |
| Sets a list of connection pool listeners. |
| Sets the frequency for running a maintenance job. |
| Sets the time to wait before running the first maintenance job. |
| Sets the maximum time a connection can be idle before it's closed. |
| Sets the maximum time a pooled connection can be alive before it's closed. |
| Sets the maximum number of connections a pool can establish concurrently. |
| Sets the maximum number of connections associated with a connection pool. Default: |
| Sets the maximum time to wait for an available connection. Default: |
| Sets the minimum number of connections associated with a connection pool. Default: |
To learn more about these methods, see the ConnectionPoolSettings.Builder API documentation.
Example
The following example shows how to create a connection pool that has a maximum size of 50 connections.
Select the Connection String or MongoClientSettings tab to see the corresponding syntax:
ConnectionString connectionString = new ConnectionString("mongodb://<host>:<port>/?maxPoolSize=50"); try (MongoClient mongoClient = MongoClients.create(connectionString)) { }
try (MongoClient mongoClient = MongoClients.create( MongoClientSettings.builder().applyConnectionString( new ConnectionString("<your connection string>")) .applyToConnectionPoolSettings(builder -> builder.maxSize(50)) .build())) {
Additional Information
For more information on using a connection pool, see the Connection Pool documentation in the Server manual.