Overview
In this guide, you can learn about how the Kotlin Sync driver uses connection pools to manage connections to a MongoDB deployment. You can specify connection pool settings in your application to configure this behavior.
A connection pool is a cache of open database connections maintained by the Kotlin Sync driver. When your application requests a connection to MongoDB, the driver retrieves 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 the driver creates new connections.
Create a Connection Pool
Each MongoClient
instance has a built-in connection pool for each server in your
MongoDB topology. If you do not configure the minPoolSize
option, connection
pools open sockets on demand. These sockets support concurrent MongoDB
operations in your application.
When you instantiate a new MongoClient
, the client opens two sockets per server
in your MongoDB topology to monitor the server's state.
For example, a client connected to a three-node replica set opens six monitoring
sockets. If the application uses the default setting for maxPoolSize
and
only queries the primary node, there can be at most 106
open
sockets and 100
connections in the connection pool. If the application uses
a read preference to query the secondary nodes, there
can be 306
total connections.
For efficiency, create a client once for each process and reuse it for all operations. Avoid creating a new client for each request because this will increase latency.
Configure a Connection Pool
You can specify settings for your connection pool by using either a connection
string or a MongoClientSettings
object.
Select the Connection String or MongoClientSettings tab to see the corresponding syntax:
The following table describes connection pool options that you can set in your connection string:
Option | Description |
---|---|
| Sets the maximum number of connections a pool may establish concurrently. Default: |
| Sets the maximum number of milliseconds that a connection can remain idle in the pool before it is removed and closed. Default: |
| Sets the maximum number of connections that can be open in a pool. If an operation needs a new connection while the connection pool has the maximum number of open connections, the operation waits for a new connection to open. To limit this waiting time, use the single timeout setting. To learn more, see the Limit Server Execution Time guide. Default: |
| Sets the minimum number of connections that can be open in a pool.
The value of Default: |
| Sets the maximum amount of time, in milliseconds, the driver
can continue to use a pooled connection before closing the
connection. A value of Default: |
To learn more about these options, see the ConnectionString API documentation.
To specify connection pool settings in a MongoClientSettings
object,
chain the applyToConnectionPoolSettings()
method to the MongoClientSettings
builder.
Pass a ConnectionPoolSettings.Builder
block as a parameter to the
applyToConnectionPoolSettings()
method.
The following table describes the setter methods you can use in a
ConnectionPoolSettings.Builder
block to configure the connection pool:
Method | Description |
---|---|
| Adds a listener for connection pool-related events. |
| Applies the settings from a |
| Uses the connection pool settings specified in a
|
| Sets the frequency for running connection pool maintenance jobs. |
| Sets the time to wait before running the first maintenance job on the connection pool. |
| Sets the maximum time a connection can be idle before it's closed. |
| Sets the maximum time a pooled connection can be open before it's closed. |
| Sets the maximum number of connections that can be open in a pool. Default: |
| Sets the maximum time to wait for an available connection. Default: |
| Sets the minimum number of connections that can be open in a 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 maximum size of 50
connections.
Select the Connection String or MongoClientSettings tab to see the corresponding syntax:
val uri = "mongodb://<host>:<port>/?maxPoolSize=50" val client = MongoClient.create(uri)
val mongoClient = MongoClient.create( MongoClientSettings.builder() .applyConnectionString(ConnectionString("mongodb://<host>:<port>/")) .applyToConnectionPoolSettings { builder -> builder.maxSize(50) } .build() )
Additional Information
To learn more about using a connection pool, see Connection Pool Overview in the MongoDB Server manual.