Application using MongoJava Sync driver v4.7.2 and public subnet can't connect to Atlas Cluster (Timeout)

When trying to connect to a v5.0.18 M10 Tier Atlas cluster (populated only with sample dataset) through a public subnet without Privatelink or VPC Peering set up I get this error:

Timed out after 60000 ms while waiting for a server that matches com.mongodb.client.internal.MongoClientDelegate$1@74123110. Client view of cluster state is {type=REPLICA_SET, servers=[{address=testcluster-shard-00-00.ihdih.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}, {address=testcluster-shard-00-01.ihdih.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}, {address=testcluster-shard-00-02.ihdih.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}]

It used to be at 30000 ms but I doubled it in case the connection attempt didn’t have enough time to find the server. Here’s a code snippet for how I’m handling the connection in my application:

ConnectionString connectionString = new ConnectionString(url);
        MongoClientSettings settings = MongoClientSettings.builder()
            .applyConnectionString(connectionString)
            .applyToClusterSettings(
                builder -> builder.serverSelectionTimeout(1, TimeUnit.MINUTES)
            )
            .build();
        MongoClient client = MongoClients.create(settings);
        return client;

I’ve also made sure that the network access settings on the Atlas project include the IP address as well as the security group from which the application is being executed. Is there something I’m not taking into account here? Any advice would be very helpful.

Hello @Julio_Montes_de_Oca ,

I saw that you haven’t got a response to this topic yet, were you able to find a solution?
If not, then could you please confirm and share few things for me to understand your use-case better?

  • Were you able to connect to this cluster before or is this the first time you are trying to connect and getting this error?
  • Could you please try connecting to your Atlas cluster via Mongo Shell or Compass or any other drivers? If you have already tried, was the connection successful??
  • Can you share your connection string? (Kindly Redact any username/password from this)
  • Any active firewall that might be blocking access?
  • Can you please confirm if your IP address or a CIDR-notated range of addresses is whitelisted in the Atlas? Kindly refer Configure IP Access List Entries
  • Please confirm if port 27017 is not blocked at your cloud provider’s end.
  • Check that the database exists and you are connecting to the correct one in the yourDatabaseName slot.
  • Kindly check Atlas Cluster connection graph of the cluster you are trying to connect with as different Atlas tiers have different connection limits; clusters of size M0/M2/M5 , for example, are limited to 500 connections, and clusters of size M10 are limited to 1500 connections.
  • Lastly, please share the version of the MongoDB driver you are using? It’s possible that the driver version is not compatible with the MongoDB version you are using.

The most common reason for someone to get this error if a DAO(Data Access Object) tries to connect using an invalid mongo URI. Why it’s invalid can be many different reasons, but some common ones are:

  • Check for typos in the connection string (missing srv, copy/paste errors, missing mongodb://)
  • Check that the domain for the private endpoint is formatted correctly. In the middle of the connection string, there should be <cluster name>-pl-<number>-lb. For example ia-prod-regional-cluste-pl-0-lb.fu9ds.mongo.com
  • Check that the private endpoint is in the same region of the cluster that it’s for (something is probably wrong with the fad service file if this is the case)
  • Check that the security groups in the VPC for the endpoint allow inbound communication from the kube deployment that is trying to connect to it
  • Invalid credentials should cause a different error message, but check that the credentials have been encrypted correctly

Attaching a few documents and threads you can refer to troubleshoot/fix connection error.

Regards,
Tarun

Hello,

Thank you for your response. I will answer your questions:

  • I am able to successfully connect to this cluster from Mongo shell locally and even from the AWS EC2 instance with the same VPC/subnet/security group settings my application uses to connect.
  • Connection string: mongodb+srv://:@crawlertestcluster.ihdih.mongodb.net/?retryWrites=true&w=majority
  • No firewall that I’m aware of, as the instance that hosts the application is able to connect to the cluster.
  • In Atlas I have the Security Group explicitly included in the network access whitelist, as well as the 0.0.0.0/0 IP address (everyone).
  • The database does exist: sample_airbnb
  • Since this is in the testing phase, my individual attempts to connect are the only ones for the cluster in question (ie no more than 10 connections at a time).
  • I am using MongoJava-sync-driver v4.7.2
  • I am not using privatelink or VPC Peering for these tests. I think it would be helpful to note that the application can connect successfully when using either of those, but fails with the above error when it doesn’t use PrivateLink or VPC Peering to establish connection.

I hope these details help.
Thank you.

Hello @Julio_Montes_de_Oca ,

As you are able to connect to cluster even from your EC2 instance so it should not be any network/connectivity issues. Moreover, your URL seems correct and the error seems to come from the driver/application side.

I would recommend you to upgrade your MongoJava-sync-driver v4.7.2 to MongoJava-sync-driver v 4.10.2 as there are a lot of bug fixes, improvements and new features available.

Have you tried below recommended methods provided in the Timeout Error section of Java Sync Connection troubleshooting?

Timeout Error

Sometimes when you send messages through the driver to the server, the messages take a while to respond. When this happens, you might receive an error message similar to one of the following error messages:

Timed out after 30000 ms while waiting for a server that matches >ReadPreferenceServerSelector{readPreference=primary}.
No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description

If you receive one of these errors, try the following methods to resolve the issue.

Set maxConnectionTimeoutMS

The maxConnectionTimeoutMS option indicates the amount of time the Java driver waits for a connection before timing out. The default value is 10000. You can increase this value or set it to 0 if you want the driver to never timeout.

Set maxConnectionLifeTime and maxConnectionIdleTime

Consider setting maxConnectionLifeTime and maxConnectionIdleTime. These parameters configure how long a connection can be maintained with a MongoDB instance. For more information about these parameters, see Connection Pool Settings.

Check the Number of Connections

You might have too many open connections. The solution to this is described under Error Sending Message.

Install Certificate

If you are using an older version of Java, you might need to manually install some certificates as described under Error Sending Message.