@@ -204,6 +204,47 @@ Consider setting ``maxConnectionLifeTime`` and
204204maintains a connection to a MongoDB instance. For more information about these
205205parameters, see :ref:`Connection Pool Settings <mcs-connectionpool-settings>`.
206206
207+ .. _java-server-selection-errors:
208+
209+ Server Selection Timeout Exceptions
210+ -----------------------------------
211+
212+ Your application might not be able to complete a request even when some servers
213+ are available, causing the driver to return a server selection timeout
214+ exception.
215+
216+ This exception is of type ``MongoTimeoutException``. The following
217+ shows a sample of the exception that occurs if you attempt to send a
218+ request to a replica set in which the primary is not reachable:
219+
220+ .. code-block:: none
221+
222+ com.mongodb.MongoTimeoutException:
223+ Timed out while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}.
224+ Client view of cluster state is
225+ {type=REPLICA_SET,
226+ servers=[
227+ {address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused}},
228+ {address=localhost:27018, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused}},
229+ {address=localhost:27019, type=REPLICA_SET_SECONDARY, roundTripTime=15.0 ms, state=CONNECTED}
230+ ]
231+ }
232+
233+ The error includes a view of the cluster state that describes the
234+ connection state of each node, which can help you identify the source of
235+ your connection issue.
236+
237+ In the preceding error, the only connected server, ``localhost:27019``,
238+ is a secondary node. Because of this, the request times out as the
239+ driver is unable to select a server that satisfies the read preference
240+ of ``primary``. In this situation, you can still perform read operations
241+ against the connected secondary node if you set the read preference to
242+ ``secondary``, ``secondaryPreferred``, or ``nearest``.
243+
244+ You can also specify the ``serverSelectionTimeoutMS`` connection option
245+ to adjust the amount of time in which the driver must select a server. To
246+ learn more, see the :ref:`java-connection-options` guide.
247+
207248.. _java-miscellaneous-errors:
208249
209250Miscellaneous Errors
0 commit comments