Docs HomeMongoDB Manual

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实例都管理自己与MongoDB集群或创建MongoClient时指定的节点的连接池。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. mongos路由器为集群中的每个节点都有连接池。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连接池配置设置

To configure the connection pool, set the options:要配置连接池,请设置选项:

  • through the MongoDB URI,通过MongoDB URI
  • programmatically when building the MongoClient instance, or以编程方式构建MongoClient实例时,或者
  • in your application framework's configuration files.在应用程序框架的配置文件中。

Settings设置

Setting设置Description描述
maxPoolSizeMaximum 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
minPoolSizeMinimum number of connections opened in the pool. 池中打开的最小连接数。The value of minPoolSize must be less than the value of maxPoolSize.minPoolSize的值必须小于maxPoolSize的数值。
Default: 0
connectTimeoutMSMost 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. 大多数驱动程序为0See your driver documentation. 请参阅您的驱动程序文档。
socketTimeoutMSNumber 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. See your driver documentation. ,这意味着没有超时。请参阅您的驱动程序文档。
maxIdleTimeMSThe maximum number of milliseconds that a connection can remain idle in the pool before being removed and closed.在删除和关闭连接之前,连接在池中可以保持空闲的最大毫秒数。
Default: See your driver documentation. 请参阅您的驱动程序文档。
waitQueueTimeoutMSMaximum wait time in milliseconds that a can thread wait for a connection to become available. can线程等待连接可用的最长等待时间(以毫秒为单位)。A value of 0 means there is no limit.值为0表示没有限制。
Default: 0. See your driver documentation. 。请参阅您的驱动程序文档。
ShardingTaskExecutorPoolMinSizeMinimum number of outbound connections each TaskExecutor connection pool can open to any given mongod instance.每个TaskExecutor连接池可以打开到任何给定mongod实例的最小出站连接数。
Default: 1. See ShardingTaskExecutorPoolMinSize.。请参阅ShardingTaskExecutorPoolMinSize
Parameter only applies to sharded deployments. 参数仅适用于分片部署。
ShardingTaskExecutorPoolMinSizeForConfigServersOptional override for ShardingTaskExecutorPoolMinSize to set the minimum number of outbound connections each TaskExecutor connection pool can open to a configuration server.ShardingTaskExecutorPoolMinSize的可选替代设置每个TaskExecutor连接池可以打开到配置服务器的最小出站连接数。
When set to: 当设置为:
  • -1, ShardingTaskExecutorPoolMinSize is used. This is the default.-1,使用ShardingTaskExecutorPoolMinSize。这是默认设置。
  • an integer value greater than -1, overrides the minimum number of outbound connections each TaskExecutor connection pool can open to a configuration server.大于-1的整数值会覆盖每个TaskExecutor连接池可以打开到配置服务器的最小出站连接数。
Parameter only applies to sharded deployments.参数仅适用于分片部署。
Default: -1
New in version 6.0. 6.0版新增。
ShardingTaskExecutorPoolMaxSizeMaximum number of outbound connections each TaskExecutor connection pool can open to any given mongod instance.每个TaskExecutor连接池可以打开到任何给定mongod实例的最大出站连接数。
Default: 2 64 - 1. See ShardingTaskExecutorPoolMaxSize.参阅ShardingTaskExecutorPoolMaxSize
Parameter only applies to sharded deployments. 参数仅适用于分片部署。
ShardingTaskExecutorPoolMaxSizeForConfigServersOptional override for ShardingTaskExecutorPoolMaxSize to set the maximum number of outbound connections each TaskExecutor connection pool can open to a configuration server.ShardingTaskExecutorPoolMaxSize的可选替代设置每个TaskExecutor连接池可以打开到配置服务器的最大出站连接数。
When set to: 当设置为:
  • -1, ShardingTaskExecutorPoolMaxSize is used. 时,使用ShardingTaskExecutorPoolMaxSizeThis is the default.这是默认设置。
  • an integer value greater than -1, overrides the maximum number of outbound connections each TaskExecutor connection pool can open to a configuration server.大于-1的整数值会覆盖每个TaskExecutor连接池可以打开到配置服务器的最大出站连接数。
Parameter only applies to sharded deployments.参数仅适用于分片部署。
Default: -1
New in version 6.0. 6.0版新增。