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实例都管理自己的连接池,连接到创建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 URIYour application's应用程序的MongoClientinstanceMongoClient实例Your application framework's configuration files应用程序框架的配置文件
Settings设置
connectTimeoutMS |
|
maxConnecting |
|
maxIdleTimeMS |
|
maxPoolSize |
|
minPoolSize |
Default: |
socketTimeoutMS |
|
waitQueueTimeoutMS |
|