Docs Menu
Docs Home
/ /

Pools de conexiones

En esta guía, puedes aprender cómo el driver .NET/C# utiliza pools de conexiones para gestionar conexiones para una implementación de MongoDB y cómo puedes configurar los ajustes de pools de conexiones en tu aplicación.

Un pool de conexiones es un caché de conexiones de base de datos abiertas, mantenido por el controlador .NET/C#. Cuando su aplicación solicita una conexión a MongoDB, el controlador .NET/C# obtiene una conexión del pool sin problemas, realiza operaciones y la devuelve al pool para su reutilización.

Los grupos de conexiones ayudan a reducir la latencia de las aplicaciones y la cantidad de veces que el controlador .NET/C# crea nuevas conexiones. El siguiente diagrama ilustra una vista general de cómo... MongoClient administra un pool de conexiones:

Diagrama CMAP

Puede especificar las siguientes configuraciones de grupo de conexiones en su objeto MongoClient o en su URI de conexión:

Configuración
Descripción

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

El siguiente código crea un cliente con un tamaño máximo de grupo de conexiones de 50 utilizando el parámetro MaxConnectionPoolSize:

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

El siguiente código crea un cliente con la misma configuración que el ejemplo anterior, pero utiliza una URI de conexión:

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

Para obtener más información sobre los grupos de conexiones, consulte Descripción general del grupo de conexiones en el manual del servidor MongoDB.

Para aprender más sobre cualquiera de los métodos o tipos analizados en esta guía, consulta la siguiente documentación de API:

Volver

Stable API

En esta página