Database Manual / Administration / Performance

Connection Pool Overview连接池概述

This document describes how to use a connection pool to manage connections between applications and MongoDB instances.本文档描述了如何使用连接池来管理应用程序和MongoDB实例之间的连接。

What is a Connection Pool?什么是连接池?

Definition定义

A connection pool is a cache of open, ready-to-use database connections maintained by the driver. 连接池是由驱动程序维护的开放、即用型数据库连接的缓存。Your application can seamlessly get connections from the pool, perform operations, and return connections back to the pool. Connection pools are thread-safe.应用程序可以无缝地从池中获取连接,执行操作,并将连接返回到池中。连接池是线程安全的。

Benefits of a Connection Pool连接池的好处

A connection pool helps reduce application latency and the number of times new connections are created.连接池有助于减少应用程序延迟和创建新连接的次数。

A connection pool creates connections at startup. Applications do not need to manually return connections to the pool. Instead, connections return to the pool automatically.连接池在启动时创建连接。应用程序不需要手动将连接返回到池。相反,连接会自动返回到池中。

Some connections are active and some are inactive but available. If your application requests a connection and there's an available connection in the pool, a new connection does not need to be created.有些连接处于活动状态,有些连接处于非活动状态但可用。如果应用程序请求连接,并且池中有可用连接,则不需要创建新连接。

Create and Use a Connection Pool创建和使用连接池

Use an Instance of your Driver's MongoClient Object使用驱动程序的MongoClient对象的实例

Most drivers provide an object of type MongoClient.大多数驱动程序都提供MongoClient类型的对象。

Use one MongoClient instance per application unless the application is connecting to many separate clusters. 每个应用程序使用一个MongoClient实例,除非该应用程序连接到许多单独的集群。Each MongoClient instance manages its own connection pool to the MongoDB cluster or node specified when the MongoClient is created. MongoClient objects are thread-safe in most drivers.每个MongoClient实例都管理自己的连接池,连接到创建MongoClient时指定的MongoDB集群或节点。MongoClient对象在大多数驱动程序中都是线程安全的。

Note

Store your MongoClient instance in a place that is globally accessible by your application.MongoClient实例存储在应用程序可全局访问的位置。

Authentication认证

To use a connection pool with LDAP, see LDAP Connection Pool Behavior.要将连接池与LDAP一起使用,请参阅LDAP连接池行为

Sharded Cluster Connection Pooling分片群集连接池

mongos routers have connection pools for each node in the cluster. The availability of connections to individual nodes within a sharded cluster affects latency. 路由器为集群中的每个节点都有连接池。分片集群内单个节点的连接可用性会影响延迟。Operations must wait for a connection to be established.操作必须等待连接建立。

Connection Pool Configuration Settings连接池配置设置

You can specify connection pool settings in these locations:您可以在以下位置指定连接池设置:

  • The MongoDB URIMongoDB URI
  • Your application's MongoClient instance应用程序的MongoClient实例
  • Your application framework's configuration files应用程序框架的配置文件

Settings设置

Setting设置Description描述
connectTimeoutMS

Most drivers default to never time out. Some versions of the Java drivers (for example, version 3.7) default to 10.大多数驱动程序默认从不超时。某些版本的Java驱动程序(例如3.7版)默认为10

Default: 默认值:0 for most drivers. See your driver documentation.0适用于大多数驱动程序。请参阅驱动程序文档。

maxConnecting

Maximum number of connections a pool may be establishing concurrently.池可以同时建立的最大连接数。

maxConnecting is supported for all drivers except the Rust Driver.除Rust驱动程序外,所有驱动程序都支持maxConnecting

Raising the value of maxConnecting allows the client to establish connection to the server faster, but increases the chance of connection storms. 提高maxConnecting的值可以让客户端更快地建立与服务器的连接,但会增加连接风暴的可能性。If the value of maxConnecting is too low, your connection pool may experience heavy throttling and increased tail latency for clients checking out connections.如果maxConnecting的值太低,连接池可能会遇到严重的限制,并且客户端签出连接的尾部延迟会增加。

Default: 默认值:2

maxIdleTimeMS

The maximum number of milliseconds that a connection can remain idle in the pool before being removed and closed.在删除和关闭连接之前,连接在池中可以保持空闲的最大毫秒数。

Default: 默认值:See your driver documentation.请参阅驱动程序文档。

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.池中打开的最大连接数。当连接池达到最大连接数时,新连接将等待到waitQueueTimeoutMS的值。

Default: 默认值:100

minPoolSize

Minimum number of connections opened in the pool. 池中打开的最小连接数。The value of minPoolSize must be less than the value of maxPoolSize.minPoolSize值必须小于maxPoolSize值。

Default: 0

socketTimeoutMS

Number of milliseconds to wait before timeout on a TCP connection.TCP连接超时前等待的毫秒数。

Do not use socketTimeoutMS as a mechanism for preventing long-running server operations.不要使用socketTimeoutMS作为防止长时间运行服务器操作的机制。

Setting low socket timeouts may result in operations that error before the server responds.设置低套接字超时可能会导致操作在服务器响应之前出错。

Default: 0, which means no timeout.默认值:0,表示没有超时。

waitQueueTimeoutMS

Maximum wait time in milliseconds that a thread can wait for a connection to become available. A value of 0 means there is no limit.线程可以等待连接可用的最长等待时间(毫秒)。值为0表示没有限制。

Default: 默认值:0