thomas77
thomas77

Reputation: 1150

MongoClient not respecting connectTimeout

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?

Upvotes: 3

Views: 4930

Answers (1)

Mohammad Rafigh
Mohammad Rafigh

Reputation: 786

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

Upvotes: 3

Related Questions