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.

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")

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: