@@ -14,3 +14,141 @@ Connection Pools
14
14
:name: genre
15
15
:values: reference
16
16
17
+ Overview
18
+ --------
19
+
20
+ This guide shows you how to create and configure a connection pool associated
21
+ with a ``MongoClient``.
22
+
23
+ Create and Use a Connection Pool
24
+ --------------------------------
25
+
26
+ Every ``MongoClient`` instance has a built-in connection pool for each server
27
+ in your MongoDB topology. Connection pools open sockets on demand to support
28
+ concurrent MongoDB operations in your multi-threaded application.
29
+
30
+ The maximum size of each connection pool is set by the ``maxPoolSize`` option, which
31
+ defaults to 100. If the number of in-use connections to a server reaches the
32
+ value of maxPoolSize, the next request to that server will wait until a
33
+ connection becomes available.
34
+
35
+ Each ``MongoClient`` instance opens two additional sockets per server in your MongoDB
36
+ topology for monitoring the server's state.
37
+
38
+ Configure a Connection Pool
39
+ ---------------------------
40
+
41
+ You can specify settings for your connection pool using either a connection
42
+ string or by passing a ``MongoClientSettings`` object to the
43
+ ``MongoClients.create()`` method.
44
+
45
+ The following code creates a client with a maximum connection pool size of ``50``
46
+ by using either a connection string or ``MongoClientSettings`` object:
47
+
48
+ . tabs::
49
+
50
+ .. tab:: Connection String
51
+ :tabid: uri
52
+
53
+ .. code-block:: java
54
+
55
+ MongoClient mongoClient = MongoClients.create("mongodb://<host>:<port>/?maxPoolSize=50")
56
+
57
+ .. tab:: ``MongoClientSettings``
58
+ :tabid: MongoClient
59
+
60
+ .. literalinclude:: /includes/fundamentals/code-snippets/mcs.java
61
+ :start-after: begin MongoSettings
62
+ :end-before: end MongoSettings
63
+ :language: java
64
+ :dedent:
65
+
66
+ For more information on these connection string options, see the
67
+ :ref:`Connection Options <connection-options>`
68
+ guide.
69
+
70
+ For more information on configuring you connection pool by using a
71
+ ``MongoClientSettings`` object see the Connection Pool Settings section
72
+ of the :ref:`<specify-mongoclient-settings>` guide.
73
+
74
+ Connection Pool Settings
75
+ ~~~~~~~~~~~~~~~~~~~~~~~~
76
+
77
+ The following are connection string settings you can use to configure your
78
+ connection pool:
79
+
80
+ .. list-table::
81
+ :widths: 25,75
82
+ :header-rows: 1
83
+
84
+ * - Setting
85
+ - Description
86
+
87
+ * - :urioption:`connectTimeoutMS`
88
+
89
+ - Specifies the maximum amount of time, in milliseconds, the Java driver
90
+ waits for a connection to open before timing out. A value of 0 instructs
91
+ the driver to never time out while waiting for a connection to open.
92
+
93
+ *Default:* ``10000`` (10 seconds)
94
+
95
+ * - :urioption:`maxConnecting`
96
+
97
+ - Maximum number of connections a pool may be establishing
98
+ concurrently.
99
+
100
+ .. include:: /includes/connection-pool/max-connecting-use-case.rst
101
+
102
+ *Default:* ``2``
103
+
104
+ * - :urioption:`maxIdleTimeMS`
105
+
106
+ - The maximum number of milliseconds that a connection can
107
+ remain idle in the pool before being removed and closed.
108
+
109
+ *Default:* ``0``
110
+
111
+ * - :urioption:`maxPoolSize`
112
+
113
+ - Maximum number of connections opened in the pool. When the
114
+ connection pool reaches the maximum number of connections, new
115
+ connections wait up until to the value of
116
+ :urioption:`waitQueueTimeoutMS`.
117
+
118
+ *Default:* ``100``
119
+
120
+ * - :urioption:`minPoolSize`
121
+
122
+ - Minimum number of connections opened in the pool.
123
+ The value of :urioption:`minPoolSize` must be less than
124
+ the value of :urioption:`maxPoolSize`.
125
+
126
+ *Default*: ``0``
127
+
128
+ * - :urioption:`socketTimeoutMS`
129
+
130
+ - Number of milliseconds to wait before timeout on a TCP
131
+ connection.
132
+
133
+ Do *not* use :urioption:`socketTimeoutMS` as a mechanism for
134
+ preventing long-running server operations.
135
+
136
+ Setting low socket timeouts may result in operations that error
137
+ before the server responds.
138
+
139
+ *Default*: ``0``, which means no timeout.
140
+
141
+ * - :urioption:`waitQueueTimeoutMS`
142
+
143
+ - Maximum wait time in milliseconds that a can thread wait for
144
+ a connection to become available. A value of ``0`` means there
145
+ is no limit.
146
+
147
+ *Default*: ``120000`` (120 seconds)
148
+
149
+ Additional Information
150
+ ----------------------
151
+
152
+ For more information on using a connection pool, see the
153
+ :manual:`Connection Pool </administration/connection-pool-overview>`
154
+ documentation in the Server manual.
0 commit comments