Skip to content

Commit ee8e6e0

Browse files
authored
DOCSP-47031 Connection Pools (#620)
* DOCSP-47031 Connection Pools * code and table * new line * add code file * fix tabs * edit * edits * add to toc * edit title * another small edit * rachel feedback * edit tab copy * format * remove unnecessary links to server options * vale * tech review * small edits
1 parent 79722ed commit ee8e6e0

File tree

4 files changed

+178
-2
lines changed

4 files changed

+178
-2
lines changed

source/connection.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Connection Guide
1010
Connection Options </connection/connection-options>
1111
MongoClient Settings </connection/mongoclientsettings>
1212
Stable API </connection/stable-api>
13+
Connection Pools </connection/connection-pools>
1314
Network Compression </connection/network-compression>
1415
JNDI Datasource </connection/jndi>
1516
Connection Troubleshooting </connection/connection-troubleshooting>
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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(
24+
new ConnectionString("<your connection string>"))
25+
.applyToConnectionPoolSettings(builder ->
26+
builder.maxSize(50))
27+
.build());
28+
//end MongoSettings
29+
mongoClient.listDatabaseNames().forEach(n -> System.out.println(n));
30+
mongoClient.close();
31+
} finally {
32+
System.out.print("---------------------------------------");
33+
}
34+
}
35+
36+
}

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)