-
Notifications
You must be signed in to change notification settings - Fork 20
DOCSP-46691: Connection Pools #157
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
mcmorisi
merged 6 commits into
mongodb:master
from
mcmorisi:DOCSP-46691-connection-pools
Feb 3, 2025
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b93aa0d
DOCSP-46691: Connection Pools
mcmorisi e9dac33
Fix
mcmorisi 108e3a1
Fix
mcmorisi 0264d2d
JS feedback
mcmorisi b41a340
Technical feedback
mcmorisi 34cb440
Merge branch 'master' into DOCSP-46691-connection-pools
mcmorisi 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,106 @@ | ||
.. _pymongo-connection-pools: | ||
|
||
================ | ||
Connection Pools | ||
================ | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
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+}. | ||
|
||
Configuring Connection Pools | ||
---------------------------- | ||
|
||
You can specify the following connection pool settings in your ``MongoClient`` object or in | ||
your connection URI: | ||
|
||
.. list-table:: | ||
:widths: 30 70 | ||
:header-rows: 1 | ||
|
||
* - Setting | ||
- Description | ||
|
||
* - ``connectTimeoutMS`` | ||
- | Sets the time that {+driver-short+} waits when connecting a new | ||
socket before timing out. | ||
| Defaults to ``20000`` | ||
|
||
* - ``maxConnecting`` | ||
- | Sets the maximum number of connections that each pool can have at once. | ||
| Defaults to ``2`` | ||
|
||
* - ``maxIdleTimeMS`` | ||
- | Sets the maximum time that a connection can remain idle in the pool. | ||
| Defaults to ``None`` (no limit) | ||
|
||
* - ``maxPoolSize`` | ||
- | Sets the maximum number of concurrent connections that the pool maintains. | ||
If the maximum pool size is reached, further requests wait until a connection | ||
becomes available. | ||
| Defaults to ``100`` | ||
|
||
* - ``minPoolSize`` | ||
- | Sets the minimum number of concurrent connections that the pool maintains. If | ||
the number of open connections falls below this value due to network errors, | ||
{+driver-short+} attempts to create new connections to maintain this minimum. | ||
| Defaults to ``0`` | ||
|
||
* - ``socketTimeoutMS`` | ||
- | Sets the length of time that {+driver-short+} waits for a response from the server | ||
before timing out. | ||
| Defaults to ``None`` (no timeout) | ||
|
||
* - ``waitQueueTimeoutMS`` | ||
- | Sets how long a thread waits for a connection to become available in the connection pool | ||
before timing out. | ||
| Defaults to ``None`` (no timeout) | ||
|
||
The following code creates a client with a maximum connection pool size of ``50`` by using the | ||
``maxPoolSize`` parameter: | ||
|
||
.. code-block:: python | ||
|
||
client = MongoClient(host, port, maxPoolSize=50) | ||
|
||
The following code creates a client with the same configuration as the preceding example, | ||
but uses a connection URI: | ||
|
||
.. code-block:: python | ||
|
||
client = MongoClient("mongodb://<host>:<port>/?maxPoolSize=50") | ||
|
||
Additional Information | ||
---------------------- | ||
|
||
To learn more about connection pools, see :manual:`Connection Pool Overview </administration/connection-pool-overview/>` | ||
in the {+mdb-server+} manual. | ||
|
||
API Documentation | ||
~~~~~~~~~~~~~~~~~ | ||
|
||
To learn more about any of the methods or types discussed in this | ||
guide, see the following API documentation: | ||
|
||
- `MongoClient <{+api-root+}pymongo/mongo_client.html#pymongo.mongo_client.MongoClient>`__ |
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.
The maximum number of connections that each pool can establish concurrently. If this limit is reached, further requests wait until one of these connections are established or another in use connection is checked back into the pool.