Overview
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:
Configuración de pools de conexiones
Puede especificar los siguientes ajustes del grupo de conexiones en su objeto MongoClient o en su URI de conexión:
Configuración | Descripción |
|---|---|
| The maximum amount of time that the .NET/C# Driver waits when establishing a new
connection before timing out. Data Type: TimeSpanDefault: 30 seconds Connection URI Example: connectTimeoutMS=0 |
| 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: integerDefault: 2Connection URI Example: maxConnecting=3 |
| 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: TimeSpanDefault: 10 minutes Connection URI Example: maxIdleTimeMS=60000 |
| 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: TimeSpanDefault: 30 minutes Connection URI Example: maxLifeTimeMS=50000 |
| 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: integerDefault: 100Connection URI Example: maxPoolSize=150 |
| 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: integerDefault: 0Connection URI Example: minPoolSize=3 |
| The length of time that the .NET/C# Driver waits for a response from the server
before timing out. Data Type: TimeSpanDefault: OS default Connection URI Example: socketTimeoutMS=100000 |
| How long a thread waits for a connection to become available in the connection pool
before timing out. Data Type: TimeSpanDefault: 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);
Información Adicional
Para aprender más sobre los pools de conexión, consulta Descripción general del pool de conexiones en el manual del MongoDB Server.
Documentación de la API
Para aprender más sobre cualquiera de los métodos o tipos analizados en esta guía, consulta la siguiente documentación de API: