Not able to connect mongodb atlas with java

following is the code.

public static void main(String[] args) {
	
	ConnectionString connectionString = new ConnectionString("mongodb+srv://demo:demo123@cluster0.f02.mongodb.net/test?retryWrites=true&w=majority");
	MongoClientSettings settings = MongoClientSettings.builder()
	        .applyConnectionString(connectionString)
	        .build();
	MongoClient mongoClient = MongoClients.create(settings);
	MongoDatabase database = mongoClient.getDatabase("test");
	
	System.out.println(database.getName());
}

while running this it gives below error

Exception in thread "main" com.mongodb.MUQrUTqDch7niLusZ4SxSTAcoawWJK91eT: Unable to look up TXT record for host cluster0.f02ax.mongodb.net
	at com.mongodb.internal.dns.DefaultDnsResolver.resolveAdditionalQueryParametersFromTxtRecords(DefaultDnsResolver.java:131)
	at com.mongodb.ConnectionString.<init>(ConnectionString.java:381)
	at com.example.demo.em.main(em.java:13)
Caused by: javax.naming.CommunicationException: DNS error [Root exception is java.net.SocketTimeoutException: Receive timed out]; remaining name 'cluster0.f02ax.mongodb.net'
	at jdk.naming.dns/com.sun.jndi.dns.DnsClient.query(DnsClient.java:316)
	at jdk.naming.dns/com.sun.jndi.dns.Resolver.query(Resolver.java:81)
	at jdk.naming.dns/com.sun.jndi.dns.DnsContext.c_getAttributes(DnsContext.java:434)
	at java.naming/com.sun.jndi.toolkit.ctx.ComponentDirContext.p_getAttributes(ComponentDirContext.java:235)
	at java.naming/com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:141)
	at java.naming/com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.getAttributes(PartialCompositeDirContext.java:129)
	at java.naming/javax.naming.directory.InitialDirContext.getAttributes(InitialDirContext.java:171)
	at com.mongodb.internal.dns.DefaultDnsResolver.resolveAdditionalQueryParametersFromTxtRecords(DefaultDnsResolver.java:114)
	... 2 more
Caused by: java.net.SocketTimeoutException: Receive timed out
	at java.base/sun.nio.ch.DatagramChannelImpl.trustedBlockingReceive(DatagramChannelImpl.java:700)
	at java.base/sun.nio.ch.DatagramChannelImpl.blockingReceive(DatagramChannelImpl.java:630)
	at java.base/sun.nio.ch.DatagramSocketAdaptor.receive(DatagramSocketAdaptor.java:239)
	at java.base/java.net.DatagramSocket.receive(DatagramSocket.java:569)
	at jdk.naming.dns/com.sun.jndi.dns.DnsClient.doUdpQuery(DnsClient.java:426)
	at jdk.naming.dns/com.sun.jndi.dns.DnsClient.query(DnsClient.java:214)
	... 9 more
1 Like

The TXT DNS entry is fine. Try switching DNS provider by using 8.8.8.8.

How to change dns provider…can you elaborate?

I am also facing a similar issue. Could you please let me know how was the issue resolved for you?

If it is exactly the same issue you should first try what was already proposed.

Personally, I am not assuming that this is the same issue, so please post a screenshot of what you are doing that shows the exact issue you are having. The important thing to see is your connection string. That is the only way we can tell you if the TXT DNS entry is right or not.

I’m experiencing the exact same issue but can’t seem to find a resolution to it. I’ve tried changing my DNS provider to Google DNS (8.8.8.8 & 8.8.4.4) but I’m still failing to connect via my application to Mongo. Does anyone know what is going wrong and what the fix to this issue is?