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 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:
Configuración de grupos de conexiones
Puede especificar las siguientes configuraciones de 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 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);
Información Adicional
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.
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: