Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Menu Docs
Página inicial do Docs
/ / /
Driver C#/ .NET
/ /

Pool de Conexões

Neste guia, você aprenderá como o driver .NET/C# usa pools de conexões para gerenciar conexões com um MongoDB deployment e como definir as configurações do pool de conexões em seu aplicação.

Um pool de conexões é um cache de conexões de banco de dados abertas mantidas pelo driver .NET/C#. Quando seu aplicação solicita uma conexão com o MongoDB, o driver .NET/C# obtém uma conexão do pool sem problemas, executa operações e retorna a conexão ao pool para reutilização.

Os pools de conexões ajudam a reduzir a latência de aplicação e o número de vezes que novas conexões são criadas pelo driver .NET/C#. O diagrama a seguir ilustra uma visão de alto nível de como o MongoClient gerencia um pool de conexões:

Diagrama CMAP

Você pode especificar as seguintes configurações do pool de conexões no seu objeto MongoClient ou em seu URI de conexão:

Contexto
Descrição

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

O código a seguir cria um cliente com um tamanho máximo do pool de conexões de 50 usando o parâmetro MaxConnectionPoolSize:

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

O código a seguir cria um cliente com a mesma configuração do exemplo anterior, mas usa um URI de conexão:

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

Para saber mais sobre pools de conexões, consulte Visão geral do pool de conexões no manual do MongoDB Server .

Para saber mais sobre qualquer um dos métodos ou tipos discutidos neste guia, consulte a seguinte documentação da API:

Voltar

Stable API

Próximo

Conecte-se a partir do Amazon Web Services Lambda

Nesta página