Overview
In this guide, you can learn about how the Scala 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 Scala driver. Your application retrieves a connection from the pool, performs operations, and returns the connection for reuse.
Using a connection pool reduces latency and minimizes the number of times the driver creates new connections.
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 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 waits 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 configure a 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 the connection string options for configuring a connection pool:
Option | Description |
|---|---|
| The maximum number of connections the driver can establish concurrently. Default: |
| The maximum number of milliseconds a connection can remain idle in the pool before the driver closes it. A value of Default: |
| The maximum number of connections the driver can have open at one time in a single connection pool. Default: |
| The minimum number of connections the driver maintains in a single connection pool. This value must be less than Default: |
| This option is deprecated. Use Maximum wait time in milliseconds that an operation can wait for a connection to become available. A value of Default: |
| The maximum number of milliseconds a connection can remain open. A value of Default: |
For more information, see the ConnectionString API documentation.
Chain the applyToConnectionPoolSettings() method to the MongoClientSettings.Builder class to configure a connection pool. The following table describes the ConnectionPoolSettings.Builder methods you can chain to configure the pool:
Method | Description |
|---|---|
| Adds a listener for connection pool events. |
| Applies settings from a |
| Applies settings from a |
| Sets a list of connection pool listeners. |
| Sets the frequency of the periodic job that manages idle and expired connections. |
| Sets the period of time to wait before running the first maintenance job on a connection pool. |
| Sets the maximum idle time for a pooled connection. A value of |
| Sets the maximum lifetime of a pooled connection. A value of |
| Sets the maximum number of connections a pool can establish concurrently. Default: |
| Sets the maximum number of connections allowed in the pool. Default: |
| Sets the maximum time to wait for an available connection. Default: |
| Sets the minimum number of connections the driver maintains in the pool. Default: |
Note
The maxSize() and minSize() settings apply to each server in your deployment individually, not to the pool as a whole.
To learn more, 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:
val mongoClient = MongoClient("mongodb://<host>:<port>/?maxPoolSize=50")
val settings = MongoClientSettings.builder() .applyConnectionString(ConnectionString("<connection string>")) .applyToConnectionPoolSettings( (builder: ConnectionPoolSettings.Builder) => builder.maxSize(50)) .build() val mongoClientWithSettings = MongoClient(settings)
Additional Information
For more information on using a connection pool, see the Connection Pool documentation in the Server manual.
API Documentation
To learn more about the types and methods discussed in this guide, see the following API documentation: