For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Connection Pools

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.

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.

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

maxConnecting

The maximum number of connections the driver can establish concurrently.

Default: 2

maxIdleTimeMS

The maximum number of milliseconds a connection can remain idle in the pool before the driver closes it. A value of 0 indicates no limit.

Default: 0

maxPoolSize

The maximum number of connections the driver can have open at one time in a single connection pool.

Default: 100

minPoolSize

The minimum number of connections the driver maintains in a single connection pool. This value must be less than maxPoolSize.

Default: 0

waitQueueTimeoutMS (deprecated)

This option is deprecated. Use timeoutMS instead. To learn more, see Limit Server Execution Time.

Maximum wait time in milliseconds that an operation can wait for a connection to become available. A value of 0 means there is no limit.

Default: 120000

maxLifeTimeMS

The maximum number of milliseconds a connection can remain open. A value of 0 indicates no limit.

Default: 0

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

addConnectionPoolListener()

Adds a listener for connection pool events.

applyConnectionString()

Applies settings from a ConnectionString object.

applySettings()

Applies settings from a ConnectionPoolSettings object.

connectionPoolListenerList()

Sets a list of connection pool listeners.

maintenanceFrequency()

Sets the frequency of the periodic job that manages idle and expired connections.

maintenanceInitialDelay()

Sets the period of time to wait before running the first maintenance job on a connection pool.

maxConnectionIdleTime()

Sets the maximum idle time for a pooled connection. A value of 0 indicates no limit.

maxConnectionLifeTime()

Sets the maximum lifetime of a pooled connection. A value of 0 indicates no limit.

maxConnecting()

Sets the maximum number of connections a pool can establish concurrently.

Default: 2

maxSize()

Sets the maximum number of connections allowed in the pool.

Default: 100

maxWaitTime()

Sets the maximum time to wait for an available connection.

Default: 2 minutes

minSize()

Sets the minimum number of connections the driver maintains in the pool.

Default: 0

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.

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)

For more information on using a connection pool, see the Connection Pool documentation in the Server manual.

To learn more about the types and methods discussed in this guide, see the following API documentation: