|
| 1 | +.. _pymongo-connection-pools: |
| 2 | + |
| 3 | +================ |
| 4 | +Connection Pools |
| 5 | +================ |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. contents:: On this page |
| 12 | + :local: |
| 13 | + :backlinks: none |
| 14 | + :depth: 2 |
| 15 | + :class: singlecol |
| 16 | + |
| 17 | +Overview |
| 18 | +-------- |
| 19 | + |
| 20 | +In this guide, you can learn about how {+driver-short+} uses connection pools to manage |
| 21 | +connections to a MongoDB deployment and how you can configure connection pool settings |
| 22 | +in your application. |
| 23 | + |
| 24 | +A connection pool is a cache of open database connections maintained by {+driver-short+}. |
| 25 | +When your application requests a connection to MongoDB, {+driver-short+} seamlessly |
| 26 | +gets a connection from the pool, performs operations, and returns the connection |
| 27 | +to the pool for reuse. |
| 28 | + |
| 29 | +Connection pools help reduce application latency and the number of times new connections |
| 30 | +are created by {+driver-short+}. |
| 31 | + |
| 32 | +Configuring Connection Pools |
| 33 | +---------------------------- |
| 34 | + |
| 35 | +You can specify the following connection pool settings in your ``MongoClient`` object or in |
| 36 | +your connection URI: |
| 37 | + |
| 38 | +.. list-table:: |
| 39 | + :widths: 30 70 |
| 40 | + :header-rows: 1 |
| 41 | + |
| 42 | + * - Setting |
| 43 | + - Description |
| 44 | + |
| 45 | + * - ``connectTimeoutMS`` |
| 46 | + - | Sets the time that {+driver-short+} waits when connecting a new |
| 47 | + socket before timing out. |
| 48 | + | Defaults to ``20000`` |
| 49 | + |
| 50 | + * - ``maxConnecting`` |
| 51 | + - | Sets the maximum number of connections that each pool can establish concurrently. |
| 52 | + If this limit is reached, further requests wait until a connection is established |
| 53 | + or another in-use connection is checked back into the pool. |
| 54 | + | Defaults to ``2`` |
| 55 | + |
| 56 | + * - ``maxIdleTimeMS`` |
| 57 | + - | Sets the maximum time that a connection can remain idle in the pool. |
| 58 | + | Defaults to ``None`` (no limit) |
| 59 | + |
| 60 | + * - ``maxPoolSize`` |
| 61 | + - | Sets the maximum number of concurrent connections that the pool maintains. |
| 62 | + If the maximum pool size is reached, further requests wait until a connection |
| 63 | + becomes available. |
| 64 | + | Defaults to ``100`` |
| 65 | + |
| 66 | + * - ``minPoolSize`` |
| 67 | + - | Sets the minimum number of concurrent connections that the pool maintains. If |
| 68 | + the number of open connections falls below this value due to network errors, |
| 69 | + {+driver-short+} attempts to create new connections to maintain this minimum. |
| 70 | + | Defaults to ``0`` |
| 71 | + |
| 72 | + * - ``socketTimeoutMS`` |
| 73 | + - | Sets the length of time that {+driver-short+} waits for a response from the server |
| 74 | + before timing out. |
| 75 | + | Defaults to ``None`` (no timeout) |
| 76 | + |
| 77 | + * - ``waitQueueTimeoutMS`` |
| 78 | + - | Sets how long a thread waits for a connection to become available in the connection pool |
| 79 | + before timing out. |
| 80 | + | Defaults to ``None`` (no timeout) |
| 81 | + |
| 82 | +The following code creates a client with a maximum connection pool size of ``50`` by using the |
| 83 | +``maxPoolSize`` parameter: |
| 84 | + |
| 85 | +.. code-block:: python |
| 86 | + |
| 87 | + client = MongoClient(host, port, maxPoolSize=50) |
| 88 | + |
| 89 | +The following code creates a client with the same configuration as the preceding example, |
| 90 | +but uses a connection URI: |
| 91 | + |
| 92 | +.. code-block:: python |
| 93 | + |
| 94 | + client = MongoClient("mongodb://<host>:<port>/?maxPoolSize=50") |
| 95 | + |
| 96 | +Additional Information |
| 97 | +---------------------- |
| 98 | + |
| 99 | +To learn more about connection pools, see :manual:`Connection Pool Overview </administration/connection-pool-overview/>` |
| 100 | +in the {+mdb-server+} manual. |
| 101 | + |
| 102 | +API Documentation |
| 103 | +~~~~~~~~~~~~~~~~~ |
| 104 | + |
| 105 | +To learn more about any of the methods or types discussed in this |
| 106 | +guide, see the following API documentation: |
| 107 | + |
| 108 | +- `MongoClient <{+api-root+}pymongo/mongo_client.html#pymongo.mongo_client.MongoClient>`__ |
0 commit comments