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

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.