Skip to content

Commit 36fd4f2

Browse files
committed
code and table
1 parent 81b74fc commit 36fd4f2

File tree

3 files changed

+175
-2
lines changed

3 files changed

+175
-2
lines changed

source/connection/connection-pools.txt

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,141 @@ Connection Pools
1414
:name: genre
1515
:values: reference
1616

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.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import static java.util.concurrent.TimeUnit.*;
2+
3+
import com.mongodb.client.MongoClient;
4+
import com.mongodb.client.MongoClients;
5+
import com.mongodb.connection.ClusterConnectionMode;
6+
import com.mongodb.ConnectionString;
7+
import com.mongodb.MongoClientSettings;
8+
9+
public class ConnectionPool {
10+
11+
public static void main(String[] args) {
12+
13+
System.out.println("MongoSettings:");
14+
createMongoSettings();
15+
System.out.println();
16+
17+
}
18+
19+
private static void createMongoSettings() {
20+
try {
21+
//begin MongoSettings
22+
MongoClient mongoClient = MongoClients.create(
23+
MongoClientSettings.builder().applyConnectionString(new ConnectionString("<your connection string>"))
24+
.applyToConnectionPoolSettings(builder ->
25+
builder.maxSize(50))
26+
.build());
27+
//end MongoSettings
28+
mongoClient.listDatabaseNames().forEach(n -> System.out.println(n));
29+
mongoClient.close();
30+
} finally {
31+
System.out.print("---------------------------------------");
32+
}
33+
}
34+
35+
}

source/includes/fundamentals/code-snippets/mcs.java renamed to source/includes/fundamentals/code-snippets/MCSettings.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private static void createClusterSettings() {
5858
MongoClient mongoClient = MongoClients.create(
5959
MongoClientSettings.builder()
6060
.applyToClusterSettings(builder ->
61-
builder.mode(ClusterConnectionMode.SINGLE)
61+
builder.mode(ClusterConnectionMode.SINGLE))
6262
.build());
6363
//end ClusterSettings
6464
mongoClient.listDatabaseNames().forEach(n -> System.out.println(n));
@@ -109,7 +109,7 @@ private static void createConnectionPoolSettings() {
109109
MongoClientSettings.builder().applyConnectionString(new ConnectionString("<your connection string>"))
110110
.applyToConnectionPoolSettings(builder ->
111111
builder.maxWaitTime(10, SECONDS)
112-
.maxSize(200)
112+
.maxSize(200))
113113
.build());
114114
//end ConnectionPoolSettings
115115
mongoClient.listDatabaseNames().forEach(n -> System.out.println(n));

0 commit comments

Comments
 (0)