Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
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 abiertas a base de datos mantenido por el controlador .NET/C#. Cuando la aplicación solicita una conexión a MongoDB, el controlador de .NET/C# obtiene sin problemas una conexión desde el pool, realiza las operaciones y devuelve la conexión al pool para su reutilización.

Los grupos de conexiones ayudan a reducir la latencia de la aplicación y la cantidad de veces que se crean nuevas conexiones por el Driver de .NET/C#. El siguiente diagrama ilustra una visión de alto nivel de cómo la MongoClient administra un pool de conexiones:

Diagrama CMAP

Puede especificar los siguientes ajustes del 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 pool 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 un URI de conexión:

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

Para aprender más sobre los pools de conexión, consulta Descripción general del pool de conexiones en el manual del MongoDB Server.

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