Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs 菜单
Docs 主页
/ / /
C#/ .NET驱动程序
/ /

连接池

在本指南中,您可以学习;了解.NET/ C#驱动程序如何使用连接池来管理与MongoDB 部署的连接,以及如何在应用程序中配置连接池设置。

连接池是由.NET/ C#驱动程序维护的打开数据库连接的缓存。当应用程序请求连接到MongoDB时, .NET/ C#驱动程序会无缝地从池中获取连接,执行操作,然后将连接返回到池中以供重用。

连接池有助于减少应用程序延迟和.NET/ C#驱动程序创建新连接的次数。下图从宏观角度说明了 MongoClient 如何管理连接池:

CMAP 图

您可以在 MongoClient对象或连接 URI 中指定以下连接池设置:

设置
说明

ConnectTimeout

The maximum amount of time that the .NET/C# Driver waits when establishing a new connection before timing out.

Data Type: TimeSpan
Default: 30 seconds
Connection URI Example: connectTimeoutMS=0

MaxConnecting

The maximum number of connections that each pool can establish concurrently. If this limit is reached, further requests wait until a connection is established or another in-use connection is checked back into the pool.

Data Type: integer
Default: 2
Connection URI Example: maxConnecting=3

MaxConnectionIdleTime

The maximum time that a connection can remain idle in the pool. When a connection exceeds this limit, the .NET/C# Driver closes the connection and removes it from the pool.

Data Type: TimeSpan
Default: 10 minutes
Connection URI Example: maxIdleTimeMS=60000

MaxConnectionLifeTime

The maximum time that a connection can be pooled. When a connection exceeds this limit, the .NET/C# Driver closes the connection and removes it from the pool.

Data Type: TimeSpan
Default: 30 minutes
Connection URI Example: maxLifeTimeMS=50000

MaxConnectionPoolSize

The maximum number of concurrent connections that the pool maintains. If the maximum pool size is reached, further requests wait until a connection becomes available.

Data Type: integer
Default: 100
Connection URI Example: maxPoolSize=150

MinConnectionPoolSize

The minimum number of concurrent connections that the pool maintains. If the number of open connections falls below this value due to network errors, the .NET/C# Driver attempts to create new connections to maintain this minimum.

Data Type: integer
Default: 0
Connection URI Example: minPoolSize=3

SocketTimeout

The length of time that the .NET/C# Driver waits for a response from the server before timing out.

Data Type: TimeSpan
Default: OS default
Connection URI Example: socketTimeoutMS=100000

WaitQueueTimeout

How long a thread waits for a connection to become available in the connection pool before timing out.

Data Type: TimeSpan
Default: 2 minutes
Connection URI Example: waitQueueTimeoutMS=100000

以下代码使用 MaxConnectionPoolSize 参数创建最大连接池大小为 50 的客户端:

var settings = MongoClientSettings.FromConnectionString("<connection URI>");
settings.MaxConnectionPoolSize = 50;
var client = new MongoClient(settings);

以下代码创建一个配置与前面的示例相同的客户端,但使用了连接 URI:

var settings = MongoClientSettings.FromConnectionString("<hostname>?maxPoolSize=50");
var client = new MongoClient(settings);

要学习;了解有关连接池的更多信息,请参阅MongoDB Server手册中的 连接池概述。

要进一步了解本指南所讨论的任何方法或类型,请参阅以下 API 文档:

后退

Stable API

来年

从Amazon Web Services Lambda连接

在此页面上