2

I am trying to connect MongoDB using MongoClientURI(URL) my URL is mongodb://userName:Password@host:PortNumber/DBName?connectTimeoutMS=10000 when my MongoDB is Down i try to Post Request but it take default time 30 sec. Can any one help me solve the problem

Thanks in Advance.

Narasimha
  • 109
  • 2
  • 13
  • Are you using a client driver or making a direct connection to Mongo? If you are using a client driver (e.g. Java, Node.js etc) then the driver may be applying a default on your behalf or ignoring the value you supplied for connectionTimeoutMS or it might be serving you up a connection from a pool rather than attempting to create a new connection each time. – glytching Jul 24 '17 at 10:00
  • MongoClient mongoClient = new MongoClient(new MongoClientURI(URL)); MongoDatabase mongodatabase = mongoClient.getDatabase(dbName); MongoCollection coll = mongodatabase.getCollection(collection); FindIterable cursor1 =coll.find(searchQuery) my code is like this and i am using driver is "mongo-java-driver-3.2.2" Please suggest how to put connection timeout for MongoDB. Thanks in Advnce. – Narasimha Jul 27 '17 at 13:46
  • If you're using MongoClientURI, you can set custom timeout like this. https://stackoverflow.com/a/41803317/9498804 – Muaz May 24 '21 at 07:42

1 Answers1

1

You can set timeouts by using the Mongo Java client's MongoClientOptions. For example:

    MongoClientOptions clientOptions = MongoClientOptions.builder()
            .connectTimeout(...)
            .socketTimeout(...)
            .serverSelectionTimeout(...)
            .build();

    MongoClient mongoClient = new MongoClient(new ServerAddress(host, port), clientOptions);

Examining mongoClient.getMongoClientOptions() after the above line of code clearly shows that the created client is faithful to the supplied config values. By contrast, if you do not set these values via MongoClientOptions then mongoClient.getMongoClientOptions() shows that the default values have been chosen.

Based on your updated comments I think the situation you are trying to cater for is this:

Creating a connection against a server instance which does not exists / is unavailable should fail sooner that the default of 30s.

If so then the configuration parameter you want to use is serverSelectionTimeout. The following invocation ...

    MongoClientOptions clientOptions = MongoClientOptions.builder()
            .serverSelectionTimeout(2000)
            .build();

    MongoClient mongoClient = new MongoClient(new ServerAddress(host, port), clientOptions);

... will cause this exception to be thrown:

com.mongodb.MongoTimeoutException: Timed out after 2000 ms while waiting to connect.

Note: serverSelectionTimeout is available in the version of the MongoDB Java driver which you are using (3.2.2 according to the comment you posted on your question).

glytching
  • 44,936
  • 9
  • 114
  • 120
  • creds.add(MongoCredential.createCredential(username, dbName, password.toCharArray()));MongoClientOptions.Builder optionsBuilder = MongoClientOptions.builder(); optionsBuilder.connectTimeout(Integer.parseInt(connectionTimeOut)); MongoClientOptions options = optionsBuilder.build(); mongoClient = new MongoClient(new ServerAddress(hostname , Integer.parseInt(port)), creds, options); mongodatabase = mongoClient.getDatabase(dbName); ...Now my Mongo server is Down and i configure connectionTime=10000 but it is still taking 30s time(DefaultTime) – Narasimha Jul 27 '17 at 20:53
  • I have updated the answer in a bid to address your latest comment. If this does not work for you, could you please describe exactly what connection failure scenario(s) you are trying to cater for. – glytching Jul 28 '17 at 07:43
  • Thank you very much for your response .ConnectionTimeOut working fine but i am trying to put socketTimeOut it is not working on the same code.optionsBuilder.socketTimeout(Integer.parseInt(socketTimeOut)); it is not taking that value .it is taking default value infinity .can you help me to put socketTimeOut . – Narasimha Jul 28 '17 at 10:28
  • Could you explain **why** you want to set socketTimeout? Perhaps you could explain exactly what you are trying to achieve then I may be able to suggest a solution. – glytching Jul 28 '17 at 10:32
  • some cases from MongoDB we are trying to fetching like more than 100 records due to network slow the reading time very slow .suppose north bound system is expected the response and suppose my application receive ( 200 -300) per sec all 200 sec expect the response from my application and all threads in waiting state. this there is a chance to down the application server because of above scenario i need to put the socket Timeout. can you explain how to solve this problem – Narasimha Jul 28 '17 at 14:45
  • Perhaps you should throttle traffic coming through your application server rather than letting it all stack up at the Mongo layer? I'm not very confident making that statement though since I don't think I fully understand your last comment. Anyway, I think my answer accurately describes how to set connectTimeout and serverSelectionTimeout on the Mongo client using the Mongo Java 3.2.2 driver. – glytching Jul 28 '17 at 15:08