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实例都管理自己的连接池,连接到创建MongoClient时指定的MongoDB集群或节点。MongoClient objects are thread-safe in most drivers.MongoClient对象在大多数驱动程序中都是线程安全的。
Note注意
Store your MongoClient instance in a place that is globally accessible by your application.将MongoClient实例存储在应用程序可全局访问的位置。
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连接池配置设置
To configure the connection pool, set the options:要配置连接池,请设置以下选项:
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的值。
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.请参阅驱动程序文档。
Amount of time that a connection can be idle in the pool before closing. 连接在关闭之前在池中空闲的时间。Idle connections close until the number of open connections equals minPoolSize.空闲连接关闭,直到打开的连接数等于minPoolSize。
Default:默认:See your driver documentation.请参阅驱动程序文档。
Maximum 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.。请参阅驱动程序文档。
Minimum number of outbound connections each TaskExecutor connection pool can open to any given mongod instance.每个TaskExecutor连接池可以打开到任何给定mongod实例的最小出站连接数。
Maximum number of outbound connections each TaskExecutor connection pool can open to any given mongod instance.每个TaskExecutor连接池可以打开到任何给定mongod实例的最大出站连接数。