Connection Pool Overview连接池概述
On this page本页内容
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
对象的实例
MongoClient
ObjectMost 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
对象在大多数驱动程序中都是线程安全的。
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, orMongoClient
实例时,或者in your application framework's configuration files.在应用程序框架的配置文件中。
Settings设置
maxPoolSize | waitQueueTimeoutMS .waitQueueTimeoutMS 的值。Default: 100 |
minPoolSize | minPoolSize must be less than the value of maxPoolSize .minPoolSize 的值必须小于maxPoolSize 的数值。Default: 0 |
connectTimeoutMS | 10 .10 。Default: 0 for most drivers. 0 。 |
socketTimeoutMS | socketTimeoutMS as a mechanism for preventing long-running server operations.socketTimeoutMS 用作防止长时间运行的服务器操作的机制。Default: 0 |
maxIdleTimeMS | Default: |
waitQueueTimeoutMS | 0 means there is no limit.0 表示没有限制。Default: 0 |
ShardingTaskExecutorPoolMinSize | mongod instance.mongod 实例的最小出站连接数。Default: 1 ShardingTaskExecutorPoolMinSize .ShardingTaskExecutorPoolMinSize |
ShardingTaskExecutorPoolMinSizeForConfigServers | ShardingTaskExecutorPoolMinSize to set the minimum number of outbound connections each TaskExecutor connection pool can open to a configuration server.ShardingTaskExecutorPoolMinSize 的可选替代设置每个TaskExecutor连接池可以打开到配置服务器的最小出站连接数。
Default: -1 |
ShardingTaskExecutorPoolMaxSize | mongod instance.mongod 实例的最大出站连接数。Default: 2 64 - 1. ShardingTaskExecutorPoolMaxSize .ShardingTaskExecutorPoolMaxSize 。 |
ShardingTaskExecutorPoolMaxSizeForConfigServers | ShardingTaskExecutorPoolMaxSize to set the maximum number of outbound connections each TaskExecutor connection pool can open to a configuration server.ShardingTaskExecutorPoolMaxSize 的可选替代设置每个TaskExecutor连接池可以打开到配置服务器的最大出站连接数。
Default: -1 |