When using a mongodb+srv connection string with the replicaSet parameter, the driver stops monitoring the SRV record after a DNS error and does not attempt to refresh the seed list again.
- Use a connection string with
replicaSetparameter, for example:mongodb+srv://user:[email protected]/db1?replicaSet=db1-rs0 - Simulate a DNS resolution failure for the SRV record (eg. by providing invalid domain or overriding
DnsResolver). - Observe that after the first DNS error, the driver does not retry SRV lookups.
For comparison, repeat the test without the
replicaSetparameter and note that the driver retries SRV lookups every 10 seconds as expected.
-
Monitoring SRV record until DNS is available
The driver could continue retrying SRV lookups at the regular interval after a DNS error, regardless of whether
replicaSetis specified. SRV monitoring should only stop after a successful resolution that yields a valid seed list. -
Fail creation of
MongoClientThe creation of
MongoClientinstance could fail when DNS error occurs to avoid creation of stale instance
When a replicaSet parameter is specified, SRV monitoring stops after the first DNS error and does not resume.
This leaves the driver with a stale or empty seed list, preventing discovery of replica set members.
This behavior appears related to:
com.mongodb.internal.connection.DefaultDnsSrvRecordMonitor.DnsSrvRecordMonitorRunnableclusterType = dnsSrvRecordInitializer.getClusterType()callshouldContinueMonitoringmethod
clusterTypevalue derived fromClusterSettings#getRequiredClusterType
When a replicaSet parameter is present, ClusterSettings#getRequiredClusterType sets requiredClusterType to REPLICA_SET, which appears to cause shouldContinueMonitoring to return false after a DNS error
if (builder.requiredReplicaSetName != null) {
if (builder.requiredClusterType == ClusterType.UNKNOWN) {
builder.requiredClusterType = ClusterType.REPLICA_SET;
} else if (builder.requiredClusterType != ClusterType.REPLICA_SET) {
throw new IllegalArgumentException(...);
}
}As a result, the DnsSrvRecordMonitorRunnable thread terminates and no further SRV refreshes are attempted.
Java driver version: 5.5.1
Java version: 21