Docs 菜单

连接池

In this guide, you can learn about how the Java 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 driver. When your application requests a connection to MongoDB, the Java 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 Java driver.

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.

You can specify settings for your connection pool 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:

设置
说明

maxConnecting

Maximum number of connections a pool may establish concurrently.

默认: 2

maxIdleTimeMS

The maximum number of milliseconds that a connection can remain idle in the pool before being removed and closed.

默认: 0

maxPoolSize

Maximum number of connections opened in the pool. When the connection pool reaches the maximum number of connections, new connections wait up until to the value of waitQueueTimeoutMS.

默认: 100

minPoolSize

Minimum number of connections opened in the pool. The value of minPoolSize must be less than the value of maxPoolSize.

默认: 0

waitQueueTimeoutMS

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.

默认: 120000 (120 seconds)

maxLifeTimeMS

Specifies the maximum amount of time, in milliseconds, the Java driver will continue to use a pooled connection before closing the connection. A value of 0 indicates that there is no upper bound on how long the driver can keep a pooled connection open.

默认: 0

The following code creates a client with a maximum connection pool size of 50.

ConnectionString connectionString = "mongodb://<host>:<port>/?maxPoolSize=50"
MongoClient mongoClient = MongoClients.create(connectionString)

有关这些参数的更多信息,请参阅 ConnectionString API文档。

Chain the applyToConnectionPoolSettings() method to modify the way the driver manages its connection pool.

下表描述了可以链接到设置以修改驱动程序行为的方法:

方法
说明

addConnectionPoolListener()

添加监听器,用于监听与连接池相关的事件。

applyConnectionString()

使用 ConnectionString 对象中的设置。

applySettings()

使用在 ConnectionPoolSettings 对象中指定的连接池设置。

maintenanceFrequency()

设置运行维护作业的频率。

maintenanceInitialDelay()

设置运行第一个维护作业之前的等待时间。

maxConnectionIdleTime()

设置连接在关闭之前可以空闲的最长时间。

maxConnectionLifeTime()

设置池式连接在关闭之前可以存活的最长时间。

maxSize()

Sets the maximum number of connections associated with a connection
pool.

默认: 100

maxWaitTime()

设置等待可用连接的最长时间。

默认: 2 minutes

minSize()

Sets the minimum number of connections associated with a connection
pool.

默认: 0

注意

This maxSize and minSize settings apply to each server in the cluster you connect the driver to.

For example, assume you connect the driver to a cluster with three mongos servers. This means that there can be at most maxSize connections and at least minSize connections to each mongos server.

The following example chains the applyToConnectionPoolSettings() method to set the thread to wait at most 10 SECONDS for an available connection, and the maxSize of the connection pool to 200:

MongoClient mongoClient = MongoClients.create(
MongoClientSettings.builder().applyConnectionString(
new ConnectionString("<your connection string>"))
.applyToConnectionPoolSettings(builder ->
builder.maxSize(50))
.build());

For more information on using a connection pool, see the 连接池 documentation in the Server manual.