Hi Team,
We are using spring boot version 2.7.1 and java version 11 and MongoDB Atlas 5 version..
Now, normally when a spring/boot application starts up, it tries to connect to dependent resources. in our case MongoDB Atlas, and if the credentials are wrong then the startup fails. This is expected behavior.
But in our case for the given spring boot version the startup does not fails if the creds are wrong. In logs (root level info mode) it shows successfully connected to given Mongodb Atlas server and even listed down the different host information in our sharded cluster.
But when you hit the health endpoint, it will show application status as down.
Note, we have defined a custom MongoClient as the the uri for Managed MongoDB (Atlas) start as mongodb+srv://
@Bean
public MongoClient mongoClient() {
return MongoClients.create(mongoClientSettings());
}
private MongoClientSettings mongoClientSettings() {
return MongoClientSettings.builder()
.applyConnectionString(new ConnectionString(generateConnectionURI()))
.build();
}
private String generateConnectionURI() {
return "mongodb+srv://"
+ username
+ ":"
+ password
+ "@"
+ hostname
+ "/?retryWrites="
+ true
+ "&w="
+ majority;
}
Regarding sample code, You can simple replicate this issue by creating a demo project from start.spring.io having dependency of mongo, rest, actuator and creating a custom MongoClient as shown above and provide a valid Mongo Atlas uri and wrong creds. Same problem is there with other version of Spring boot. You can test it with 2.7.9 version of spring boot as well
Comment From: wilkinsona
For startup to fail, either creation of the MongoClient bean would have to fail or creation of another bean would have to fail due to the MongoClient having invalid credentials. If neither of those things happen the startup will succeed. Put the other way, startup will fail if something tries to connect to Mongo during the startup process.
https://github.com/spring-projects/spring-boot/issues/11325 is tracking some improvements in this area but it isn't a high priority for the team at this time.
Comment From: sanjaykmruk
Hi @wilkinsona
Thanks for reverting.
I understand what you are saying, but don't fully agree with it.
Reason for it is, if I provide the wrong creds it fails silently and prints successfully connected message in the logs
2023-03-20 18:03:41.033 INFO 61240 --- [ngodb.net:27016] org.mongodb.driver.connection : Opened connection [connectionId{localValue:11, serverValue:201556}] to dev-shard-01-00.bgj1m.mongodb.net:27016
2023-03-20 18:03:41.033 INFO 61240 --- [ngodb.net:27016] org.mongodb.driver.connection : Opened connection [connectionId{localValue:8, serverValue:193485}] to dev-shard-00-01.bgj1m.mongodb.net:27016
2023-03-20 18:03:41.038 INFO 61240 --- [ngodb.net:27016] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=dev-shard-00-01.bgj1m.mongodb.net:27016, type=SHARD_ROUTER, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=17, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=402444800}
2023-03-20 18:03:41.038 INFO 61240 --- [ngodb.net:27016] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=dev-shard-00-02.bgj1m.mongodb.net:27016, type=SHARD_ROUTER, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=17, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=407949900}
2023-03-20 18:03:41.045 INFO 61240 --- [ngodb.net:27016] org.mongodb.driver.cluster : Discovered cluster type of SHARDED
2023-03-20 18:03:41.724 INFO 61240 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint(s) beneath base path '/actuator'
2023-03-20 18:03:41.797 INFO 61240 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
But if I provide wrong host (MongoDB Atlas uri) it fails with unknown host, which implies that the mongo client did tried to reach out to the host.
023-03-20 18:07:30.906 INFO 84452 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2057 ms
2023-03-20 18:07:31.344 INFO 84452 --- [j1m.mongodb.net] org.mongodb.driver.cluster : Exception while resolving SRV records
com.mongodb.MongoConfigurationException: Failed looking up SRV record for '_mongodb._tcp.dev123-002.bgj1m.mongodb.net'.
at com.mongodb.internal.dns.DefaultDnsResolver.resolveHostFromSrvRecords(DefaultDnsResolver.java:92) ~[mongodb-driver-core-4.6.1.jar:na]
at com.mongodb.internal.connection.DefaultDnsSrvRecordMonitor$DnsSrvRecordMonitorRunnable.run(DefaultDnsSrvRecordMonitor.java:80) ~[mongodb-driver-core-4.6.1.jar:na]
at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
Caused by: com.mongodb.spi.dns.DnsWithResponseCodeException: DNS name not found [response code 3]
at com.mongodb.internal.dns.JndiDnsClient.getResourceRecordData(JndiDnsClient.java:52) ~[mongodb-driver-core-4.6.1.jar:na]
at com.mongodb.internal.dns.DefaultDnsResolver.resolveHostFromSrvRecords(DefaultDnsResolver.java:74) ~[mongodb-driver-core-4.6.1.jar:na]
... 2 common frames omitted
Caused by: javax.naming.NameNotFoundException: DNS name not found [response code 3]
at jdk.naming.dns/com.sun.jndi.dns.DnsClient.checkResponseCode(DnsClient.java:661) ~[jdk.naming.dns:na]
at jdk.naming.dns/com.sun.jndi.dns.DnsClient.isMatchResponse(DnsClient.java:579) ~[jdk.naming.dns:na]
at jdk.naming.dns/com.sun.jndi.dns.DnsClient.doUdpQuery(DnsClient.java:427) ~[jdk.naming.dns:na]
at jdk.naming.dns/com.sun.jndi.dns.DnsClient.query(DnsClient.java:212) ~[jdk.naming.dns:na]
at jdk.naming.dns/com.sun.jndi.dns.Resolver.query(Resolver.java:81) ~[jdk.naming.dns:na]
at jdk.naming.dns/com.sun.jndi.dns.DnsContext.c_getAttributes(DnsContext.java:434) ~[jdk.naming.dns:na]
at java.naming/com.sun.jndi.toolkit.ctx.ComponentDirContext.p_getAttributes(ComponentDirContext.java:235) ~[na:na]
at java.naming/com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:141) ~[na:na]
at java.naming/com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:129) ~[na:na]
at java.naming/javax.naming.directory.InitialDirContext.getAttributes(InitialDirContext.java:142) ~[na:na]
at com.mongodb.internal.dns.JndiDnsClient.getResourceRecordData(JndiDnsClient.java:41) ~[mongodb-driver-core-4.6.1.jar:na]
Caused by: com.mongodb.spi.dns.DnsWithResponseCodeException: DNS name not found [response code 3]
Comment From: wilkinsona
That doesn't look like a startup failure to me. An exception has been logged but startup appears to have succeeded. That behaviour is described in the table in https://github.com/spring-projects/spring-boot/issues/11325#issuecomment-355069234. If MongoClient doesn't log such an exception when the credentials are wrong, that is out of Spring Boot's control and you may want to raise it with the maintainers of the Mongo driver.
Comment From: rohitmeh
A workaround for this can be this https://stackoverflow.com/questions/56183731/is-there-way-to-check-correctness-of-mongodb-connection/77876167#77876167