com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[]

Viewed 15833

We are trying to connect to Mongo Atlas from Java code using

URI "mongodb+srv::@serveraddress/dbname?retryWrites=true&w=1" but getting MongoTimeoutException exception.

Strange thing here is in exception trace servers=[] list is empty.

What could be the possible reasons for this error?

MongoDB Server and Java Driver Details:

MongoDB Server: 4.2

MongoDB java Driver: 3.12.5

spring-data-mongodb: 2.1.5.RELEASE

Please let me know if any further information needed.

Thank you for the help.

com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[]
    at com.mongodb.internal.connection.BaseCluster.getDescription(BaseCluster.java:182)
    at com.mongodb.internal.connection.AbstractMultiServerCluster.getDescription(AbstractMultiServerCluster.java:54)
    at com.mongodb.client.internal.MongoClientDelegate.getConnectedClusterDescription(MongoClientDelegate.java:152)
    at com.mongodb.client.internal.MongoClientDelegate.createClientSession(MongoClientDelegate.java:103)
    at com.mongodb.Mongo.createClientSession(Mongo.java:832)
    at com.mongodb.MongoClient.startSession(MongoClient.java:582)
    at com.mongodb.MongoClient.startSession(MongoClient.java:569)
    at com.shutterfly.services.project.InitMongoDBCollection.onApplicationEvent(InitMongoDBCollection.java:36)
    at com.shutterfly.services.project.InitMongoDBCollection.onApplicationEvent(InitMongoDBCollection.java:20)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:402)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:359)
    at org.springframework.boot.context.event.EventPublishingRunListener.running(EventPublishingRunListener.java:105)
    at org.springframework.boot.SpringApplicationRunListeners.running(SpringApplicationRunListeners.java:78)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:332)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
    at com.shutterfly.services.project.Application.main(Application.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
    at java.lang.Thread.run(Thread.java:748)
3 Answers

Adding the answer so that it should be helpful for others, who might see similar issue in future.

We have resolved this issue, The problem was it was taking time to find the list of clusters and connect to that with default time, we can use "MongoClientOptionsFactoryBean" to customised the parameters and can override the default configuration.

@Bean
public MongoClientOptionsFactoryBean getMongoClientOptionsFactoryBean() {
        MongoClientOptionsFactoryBean mongoClientOptionsFactoryBean = new MongoClientOptionsFactoryBean();
        mongoClientOptionsFactoryBean.setConnectTimeout(timeout);
        mongoClientOptionsFactoryBean.setConnectionsPerHost(connectionsPerHost);
        mongoClientOptionsFactoryBean.setMaxWaitTime(maxWaitTime);
        mongoClientOptionsFactoryBean.setServerSelectionTimeout(ServerSelectionTimeout);
        mongoClientOptionsFactoryBean.setThreadsAllowedToBlockForConnectionMultiplier(connMultiplier);
        return mongoClientOptionsFactoryBean;
    }

in my case mongo was simply not running :)

please, check it with:

systemctl status mongod.service

and make sure it is enabled before rebooting the server:

systemctl enable mongod.service

so I have same problem I fix it by this code use this import

        import com.mongodb.client.MongoClient;

and this is the code

        String web="mongodb://";
        MongoClient mongoClient = MongoClients.create(web+ENVIRONMENT);
        database = mongoClient.getDatabase("PUT_HERE_YOUR_DB_NAME");

MAYBE in some place you will need change the web

      String web="mongodb+srv://"
Related