-
Notifications
You must be signed in to change notification settings - Fork 43
DOCSP-47031 Connection Pools #620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
81b74fc
DOCSP-47031 Connection Pools
lindseymoore 36fd4f2
code and table
lindseymoore c5c6abc
new line
lindseymoore 2571150
add code file
lindseymoore 89b304f
fix tabs
lindseymoore 5ebc72b
edit
lindseymoore af5da6b
edits
lindseymoore ff6deeb
add to toc
lindseymoore b24c1b9
edit title
lindseymoore 8266c19
another small edit
lindseymoore 3740ec8
rachel feedback
lindseymoore 0a0a036
edit tab copy
lindseymoore 2e62d52
format
lindseymoore b284011
remove unnecessary links to server options
lindseymoore c6a0815
vale
lindseymoore 7bd7662
tech review
lindseymoore 50e502b
small edits
lindseymoore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
.. _java-connection-pools: | ||
|
||
================ | ||
Connection Pools | ||
================ | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
Overview | ||
-------- | ||
|
||
In this guide, you can learn about how {+driver-short+} uses connection pools to manage | ||
connections to a MongoDB deployment and how you can configure connection pool settings | ||
in your application. | ||
|
||
A connection pool is a cache of open database connections maintained by {+driver-short+}. | ||
When your application requests a connection to MongoDB, {+driver-short+} seamlessly | ||
gets a connection from the pool, performs operations, and returns the connection | ||
to the pool for reuse. | ||
|
||
Connection pools help reduce application latency and the number of times new connections | ||
are created by {+driver-short+}. | ||
|
||
Create a Connection Pool | ||
------------------------ | ||
|
||
Every ``MongoClient`` instance has a built-in connection pool for each server | ||
in your MongoDB topology. Connection pools open sockets on demand to support | ||
concurrent MongoDB operations in your multi-threaded application. | ||
|
||
The ``maxPoolSize`` option sets the maximum size of each connection pool, which | ||
defaults to 100. If the number of in-use connections to a server reaches the | ||
value of maxPoolSize, the next request to that server will wait until a | ||
connection becomes available. | ||
|
||
Each ``MongoClient`` instance opens two additional sockets per server in your MongoDB | ||
Check failure on line 44 in source/connection/connection-pools.txt
|
||
topology for monitoring the server's state. | ||
|
||
Configure a Connection Pool | ||
--------------------------- | ||
|
||
You can specify settings for your connection pool using either a connection | ||
string or by passing a ``MongoClientSettings`` object to the | ||
``MongoClients.create()`` method. | ||
|
||
The following code creates a client with a maximum connection pool size of ``50`` | ||
by using either a :guilabel:`Connection String` or :guilabel:`MongoClientSettings` | ||
object: | ||
|
||
.. tabs:: | ||
|
||
.. tab:: Connection String | ||
:tabid: uri | ||
|
||
.. code-block:: java | ||
|
||
ConnectionString connectionString = "mongodb://<host>:<port>/?maxPoolSize=50" | ||
MongoClient mongoClient = MongoClients.create(connectionString) | ||
|
||
.. tab:: MongoClientSettings | ||
:tabid: MongoClient | ||
|
||
.. literalinclude:: /includes/fundamentals/code-snippets/ConnectionPool.java | ||
:start-after: begin MongoSettings | ||
:end-before: end MongoSettings | ||
:language: java | ||
:dedent: | ||
|
||
Connection Pool Settings | ||
~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
The following are connection string settings you can use to configure your | ||
connection pool: | ||
|
||
.. list-table:: | ||
lindseymoore marked this conversation as resolved.
Show resolved
Hide resolved
|
||
:widths: 25,75 | ||
:header-rows: 1 | ||
|
||
* - Setting | ||
- Description | ||
|
||
* - :urioption:`connectTimeoutMS` | ||
|
||
- Specifies the maximum amount of time, in milliseconds, the Java driver | ||
waits for a connection to open before timing out. A value of 0 instructs | ||
the driver to never time out while waiting for a connection to open. | ||
|
||
*Default:* ``10000`` (10 seconds) | ||
|
||
* - :urioption:`maxConnecting` | ||
|
||
- Maximum number of connections a pool may establish | ||
concurrently. | ||
|
||
.. include:: /includes/connection-pool/max-connecting-use-case.rst | ||
|
||
*Default:* ``2`` | ||
|
||
* - :urioption:`maxIdleTimeMS` | ||
|
||
- The maximum number of milliseconds that a connection can | ||
remain idle in the pool before being removed and closed. | ||
|
||
*Default:* ``0`` | ||
|
||
* - :urioption:`maxPoolSize` | ||
|
||
- Maximum number of connections opened in the pool. When the | ||
connection pool reaches the maximum number of connections, new | ||
connections wait up until to the value of | ||
:urioption:`waitQueueTimeoutMS`. | ||
|
||
*Default:* ``100`` | ||
|
||
* - :urioption:`minPoolSize` | ||
|
||
- Minimum number of connections opened in the pool. | ||
The value of :urioption:`minPoolSize` must be less than | ||
the value of :urioption:`maxPoolSize`. | ||
|
||
*Default*: ``0`` | ||
|
||
* - :urioption:`socketTimeoutMS` | ||
|
||
- Number of milliseconds to wait before timeout on a TCP | ||
connection. | ||
|
||
Do *not* use :urioption:`socketTimeoutMS` as a mechanism for | ||
preventing long-running server operations. | ||
|
||
Setting low socket timeouts may result in operations that error | ||
before the server responds. | ||
|
||
*Default*: ``0``, which means no timeout. | ||
|
||
* - :urioption:`waitQueueTimeoutMS` | ||
|
||
- Maximum wait time in milliseconds that a thread can wait for | ||
a connection to become available. A value of ``0`` means there | ||
is no limit. | ||
|
||
*Default*: ``120000`` (120 seconds) | ||
|
||
For more information on these connection string options, see the | ||
:ref:`Connection Options <connection-options>` | ||
guide. | ||
|
||
For more information on configuring you connection pool by using a | ||
``MongoClientSettings`` object see the Connection Pool Settings section | ||
of the :ref:`<specify-mongoclient-settings>` guide. | ||
lindseymoore marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Additional Information | ||
---------------------- | ||
|
||
For more information on using a connection pool, see the | ||
:manual:`Connection Pool </administration/connection-pool-overview>` | ||
documentation in the Server manual. |
36 changes: 36 additions & 0 deletions
36
source/includes/fundamentals/code-snippets/ConnectionPool.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import static java.util.concurrent.TimeUnit.*; | ||
|
||
import com.mongodb.client.MongoClient; | ||
import com.mongodb.client.MongoClients; | ||
import com.mongodb.connection.ClusterConnectionMode; | ||
import com.mongodb.ConnectionString; | ||
import com.mongodb.MongoClientSettings; | ||
|
||
public class ConnectionPool { | ||
|
||
public static void main(String[] args) { | ||
|
||
System.out.println("MongoSettings:"); | ||
createMongoSettings(); | ||
System.out.println(); | ||
|
||
} | ||
|
||
private static void createMongoSettings() { | ||
try { | ||
//begin MongoSettings | ||
MongoClient mongoClient = MongoClients.create( | ||
MongoClientSettings.builder().applyConnectionString( | ||
new ConnectionString("<your connection string>")) | ||
.applyToConnectionPoolSettings(builder -> | ||
builder.maxSize(50)) | ||
.build()); | ||
//end MongoSettings | ||
mongoClient.listDatabaseNames().forEach(n -> System.out.println(n)); | ||
mongoClient.close(); | ||
} finally { | ||
System.out.print("---------------------------------------"); | ||
} | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't seen
:guilabel:
used before? I guess it just bold the text?Anyway, would make these links to the
Connection Options
andMongoClient Settings
pages.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rachel-mack Thanks for the below comments! The links to these pages can be found in the tabs. Since the links are now in a more content relevant place in the tabs, I think leaving without a link here is appropriate.
guilabel can be used when referring to UI components on the page (such as the tabs) or in the Atlas UI. Here are a few examples I've seen:
Updated the copy to specifically mention the tabs.