Skip to content

Commit 252ed1c

Browse files
authored
PYTHON-3247 Mitigate user issues caused by change in directConnection defaults in 4.x (#935)
1 parent ede07f4 commit 252ed1c

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

doc/changelog.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@ Changes in Version 4.0
120120

121121
.. warning:: PyMongo 4.0 drops support for MongoDB 2.6, 3.0, 3.2, and 3.4.
122122

123+
.. warning:: PyMongo 4.0 changes the default value of the ``directConnection`` URI option and
124+
keyword argument to :class:`~pymongo.mongo_client.MongoClient`
125+
to ``False`` instead of ``None``, allowing for the automatic
126+
discovery of replica sets. This means that if you
127+
want a direct connection to a single server you must pass
128+
``directConnection=True`` as a URI option or keyword argument.
129+
For more details, see the relevant section of the PyMongo 4.x migration
130+
guide: :ref:`pymongo4-migration-direct-connection`.
131+
123132
PyMongo 4.0 brings a number of improvements as well as some backward breaking
124133
changes. For example, all APIs deprecated in PyMongo 3.X have been removed.
125134
Be sure to read the changes listed below and the :doc:`migrate-to-pymongo4`

doc/migrate-to-pymongo4.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ get the same behavior.
6565
MongoClient
6666
-----------
6767

68+
.. _pymongo4-migration-direct-connection:
69+
6870
``directConnection`` defaults to False
6971
......................................
7072

@@ -74,6 +76,28 @@ allowing for the automatic discovery of replica sets. This means that if you
7476
want a direct connection to a single server you must pass
7577
``directConnection=True`` as a URI option or keyword argument.
7678

79+
If you see any :exc:`~pymongo.errors.ServerSelectionTimeoutError`'s after upgrading from PyMongo 3 to 4.x, you likely
80+
need to add ``directConnection=True`` when creating the client.
81+
Here are some example errors:
82+
83+
.. code-block::
84+
85+
pymongo.errors.ServerSelectionTimeoutError: mongo_node2: [Errno 8] nodename nor servname
86+
provided, or not known,mongo_node1:27017
87+
88+
.. code-block::
89+
90+
ServerSelectionTimeoutError: No servers match selector "Primary()", Timeout: 30s,
91+
Topology Description: ...
92+
93+
94+
Additionally, the "isWritablePrimary" attribute of a hello command sent back by the server will
95+
always be True if ``directConnection=False``::
96+
97+
>>> client.admin.command('hello')['isWritablePrimary']
98+
True
99+
100+
77101
The waitQueueMultiple parameter is removed
78102
..........................................
79103

pymongo/mongo_client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ def __init__(
137137
) -> None:
138138
"""Client for a MongoDB instance, a replica set, or a set of mongoses.
139139
140+
.. warning:: Starting in PyMongo 4.0, ``directConnection`` now has a default value of
141+
False instead of None.
142+
For more details, see the relevant section of the PyMongo 4.x migration guide:
143+
:ref:`pymongo4-migration-direct-connection`.
144+
140145
The client object is thread-safe and has connection-pooling built in.
141146
If an operation fails because of a network error,
142147
:class:`~pymongo.errors.ConnectionFailure` is raised and the client

0 commit comments

Comments
 (0)