3

I am trying to set a connection timeout for the MongoClient (driver version 2.13).

It hangs for 10 seconds then it times out. I need a fast timeout because this code is used in test.

Here is my code:

String connectionUri = properties.getProperty("application.mongo.url");
System.out.println("******     "+  connectionUri  + "*******");
MongoClientOptions mongoClientOptions = MongoClientOptions.builder()
                                                .connectTimeout(500)
                                                .build();

MongoClient mongoClient = new MongoClient(connectionUri, mongoClientOptions);

The javadoc is describes the connectTimeout and this post describes how to set a timeout.

Has anyone had similar issues and resolved it?

Community
  • 1
  • 1
thomas77
  • 1,100
  • 13
  • 27
  • Can you provide an example of the Connection String you are using with the options? – Ross Apr 22 '15 at 09:55
  • Also does setting the `socketTimeout` as well, solve the quick fail issue? http://api.mongodb.org/java/2.13/com/mongodb/MongoClientOptions.Builder.html#socketTimeout-int- – Ross Apr 22 '15 at 10:04

1 Answers1

3

You should use MongoClientOptions.builder().serverSelectionTimeout(500).build() if you want to test server connection. other properties to set in case you want to test them are:

  • connectTimeout
  • socketTimeout
  • heartbeatConnectTimeout
  • heartbeatSocketTimeout
Mohammad Rafigh
  • 757
  • 9
  • 17