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