4

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)
Deepak Singh
  • 411
  • 1
  • 5
  • 13
  • Does this answer your question? [Spring Boot and how to configure connection details to MongoDB?](https://stackoverflow.com/questions/23515295/spring-boot-and-how-to-configure-connection-details-to-mongodb) – Marc Tarin Jun 26 '20 at 08:17
  • 1
    It is resolved now, Actual problem was instance was taking more than expected time to connect to the Mongo Atlas server. and instance was stoping before connection is successful and which was ending with Connection Timeout issue. I am keeping this question, so that if someone face this kind of issue they should look into it from this perspective as well. – Deepak Singh Jun 26 '20 at 08:34

3 Answers3

2

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;
    }
Deepak Singh
  • 411
  • 1
  • 5
  • 13
  • Hello Deepak, I am seeing the same exception. The istio enabled service (springboot app) is trying to call the istio enabled mogodb in another pod in the same cluster. When istio enabled App container comes up it says connection to Mongo is established. But When a request is sent time excepion is seen. Any input from you. – Sanjeev Jun 14 '21 at 12:38
  • There are couple of points: 1. Is your db sharded? 2. Are you using any java annotations which might be causing the problem. I have faced one issue where I have used @Indexed annotation and I started getting the same exception, after removing that it got resolved. 3. Also make sure MongoDB/mongos service are running. – Deepak Singh Jun 14 '21 at 17:00
  • No it is not sharded. No java specific annotation & No Indexes. – Sanjeev Jun 15 '21 at 21:41
  • No it is not sharded, no java specific annotation& no Indexes. Mongod is running. In my case Istio service mesh is enabled(Envoy pxoxy)for mongodb container. In same cluster when the request from istio enabled pod hits that isto enabled mongodb pod, that error comes up. In another way How to configure service mesh for in-cluster mongodb ?. – Sanjeev Jun 15 '21 at 21:53
  • Ok, it seems to me a configuration issue then, if you are running MongoDB in different container, then service which is trying to connect to MongoDB service should be in same network. I don't have any experience in ISTIO enabled services. – Deepak Singh Jun 17 '21 at 00:44
  • 1
    Thank you. They are in same N/W & Mongodb (istioservice mesh enabled)is running as saperate container. It works after I used *appProtocol* and port name as *mongo-mongodb* in its service defination. – Sanjeev Jun 18 '21 at 10:23
2

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
0

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://"
Vladi
  • 1,662
  • 19
  • 30