|
| 1 | +.. _java-connection-pools: |
| 2 | + |
| 3 | +================ |
| 4 | +Connection Pools |
| 5 | +================ |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +Overview |
| 18 | +-------- |
| 19 | + |
| 20 | +In this guide, you can learn about how the {+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 the {+driver-short+}. |
| 25 | +When your application requests a connection to MongoDB, the {+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 | +Create a Connection Pool |
| 33 | +------------------------ |
| 34 | + |
| 35 | +Every ``MongoClient`` instance has a built-in connection pool for each server |
| 36 | +in your MongoDB topology. Connection pools open sockets on demand to support |
| 37 | +concurrent MongoDB operations in your multi-threaded application. |
| 38 | + |
| 39 | +The ``maxPoolSize`` option sets the maximum size of each connection pool, which |
| 40 | +defaults to 100. If the number of in-use connections to a server reaches the |
| 41 | +value of ``maxPoolSize``, the next request to that server will wait until a |
| 42 | +connection becomes available. |
| 43 | + |
| 44 | +Each ``MongoClient`` instance opens two more sockets per server in your MongoDB |
| 45 | +topology for monitoring the server's state. |
| 46 | + |
| 47 | +Configure a Connection Pool |
| 48 | +--------------------------- |
| 49 | + |
| 50 | +You can specify settings for your connection pool using either a connection |
| 51 | +string or by passing a ``MongoClientSettings`` object to the |
| 52 | +``MongoClients.create()`` method. |
| 53 | + |
| 54 | +The following code creates a client with a maximum connection pool size of ``50``. |
| 55 | +Select the :guilabel:`Connection String` or :guilabel:`MongoClientSettings` tab to |
| 56 | +see the corresponding syntax: |
| 57 | + |
| 58 | +.. tabs:: |
| 59 | + |
| 60 | + .. tab:: Connection String |
| 61 | + :tabid: uri |
| 62 | + |
| 63 | + .. code-block:: java |
| 64 | + |
| 65 | + ConnectionString connectionString = "mongodb://<host>:<port>/?maxPoolSize=50" |
| 66 | + MongoClient mongoClient = MongoClients.create(connectionString) |
| 67 | + |
| 68 | + The following are connection string settings you can use to configure your |
| 69 | + connection pool: |
| 70 | + |
| 71 | + .. list-table:: |
| 72 | + :widths: 25,75 |
| 73 | + :header-rows: 1 |
| 74 | + |
| 75 | + * - Setting |
| 76 | + - Description |
| 77 | + |
| 78 | + * - ``maxConnecting`` |
| 79 | + |
| 80 | + - Maximum number of connections a pool may establish |
| 81 | + concurrently. |
| 82 | + |
| 83 | + *Default:* ``2`` |
| 84 | + |
| 85 | + * - ``maxIdleTimeMS`` |
| 86 | + |
| 87 | + - The maximum number of milliseconds that a connection can |
| 88 | + remain idle in the pool before being removed and closed. |
| 89 | + |
| 90 | + *Default:* ``0`` |
| 91 | + |
| 92 | + * - ``maxPoolSize`` |
| 93 | + |
| 94 | + - Maximum number of connections opened in the pool. When the |
| 95 | + connection pool reaches the maximum number of connections, new |
| 96 | + connections wait up until to the value of |
| 97 | + ``waitQueueTimeoutMS``. |
| 98 | + |
| 99 | + *Default:* ``100`` |
| 100 | + |
| 101 | + * - ``minPoolSize`` |
| 102 | + |
| 103 | + - Minimum number of connections opened in the pool. |
| 104 | + The value of ``minPoolSize`` must be less than |
| 105 | + the value of ``maxPoolSize``. |
| 106 | + |
| 107 | + *Default*: ``0`` |
| 108 | + |
| 109 | + * - ``waitQueueTimeoutMS`` |
| 110 | + |
| 111 | + - Maximum wait time in milliseconds that an operation can wait for |
| 112 | + a connection to become available. A value of ``0`` means there |
| 113 | + is no limit. |
| 114 | + |
| 115 | + *Default*: ``120000`` (120 seconds) |
| 116 | + |
| 117 | + To learn more about connection string options, see the |
| 118 | + :ref:`Connection Options <connection-options>` |
| 119 | + guide. |
| 120 | + |
| 121 | + .. tab:: MongoClientSettings |
| 122 | + :tabid: MongoClient |
| 123 | + |
| 124 | + .. literalinclude:: /includes/fundamentals/code-snippets/ConnectionPool.java |
| 125 | + :start-after: begin MongoSettings |
| 126 | + :end-before: end MongoSettings |
| 127 | + :language: java |
| 128 | + :dedent: |
| 129 | + |
| 130 | + For more information on configuring you connection pool by using a |
| 131 | + ``MongoClientSettings`` object see the Connection Pool Settings section |
| 132 | + of the :ref:`<specify-mongoclient-settings>` guide. |
| 133 | + |
| 134 | +Additional Information |
| 135 | +---------------------- |
| 136 | + |
| 137 | +For more information on using a connection pool, see the |
| 138 | +:manual:`Connection Pool </administration/connection-pool-overview>` |
| 139 | +documentation in the Server manual. |
0 commit comments