Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB Java Driver 4.1.1 how to configure timeout settings

Tags:

java

mongodb

I am migrating an application from using MongoDB Java driver v. 3.6.4 to v. 4.1.1

In 3.6.4 configuration is passed via MongoClientOptions

@Bean
    public MongoClientOptions mongoOptions() {
        return MongoClientOptions.builder()
                .connectTimeout(...)
                .serverSelectionTimeout(..)
                .socketTimeout(...)
                .build();
    } 

In 4.1.1 MongoClientOptions has been deprecated, and I am utilizing MongoClientSettings class http://mongodb.github.io/mongo-java-driver/4.1/apidocs/mongodb-driver-core/com/mongodb/MongoClientSettings.Builder.html

   @Bean
    public MongoClientSettings mongoOptions() {
        return MongoClientSettings.builder()
                .applyToSocketSettings(builder ->
                        builder.applySettings(builder()
                                .connectTimeout(config.getConnectTimeout(), MILLISECONDS).build()))
                .applyToClusterSettings(builder ->
                        builder.serverSelectionTimeout(config.getServerSelectionTimeout(), MILLISECONDS).build())
                .build();

    }

However I can't find setting to configure connectTimeout (apart from supplying connection string via applyConnectionString method.

like image 491
fg78nc Avatar asked Dec 07 '20 21:12

fg78nc


People also ask

What is default timeout in MongoDB?

What is MongoDB connection timeout? The connection timeout determines the maximum amount of response time that waits for a connection established by the server. This value is used when making an initial connection to the MongoDB database. The default connection timeout value ranges from 1 second to 30 seconds.

How do I use MongoClientSettings?

To create a MongoClientSettings object, use the MongoClientSettings. builder() method and chain methods to specify your settings. After chaining them, use the build() method to create the MongoClientSettings object. Adds a listener for command events.

What is MongoDB driver legacy?

“The MongoDB Legacy driver mongodb-driver-legacy is the legacy synchronous Java driver whose entry point is com. mongodb. MongoClient and central classes include com. mongodb. DB , com.

What is the default timeout for a Mongo connection?

Mongo Java & Ruby latest driver versions have a 10s default timeout for connection establishments while the NodeJs driver has no timeout. If the timeout is too high, you risk stalling your application. If the timeout is too low, you can give up too quickly.

What to do when MongoDB client takes too long to connect?

When the client takes too much time to connect to MongodB server than the preset timeout value, it can result in error. And the fix involves in raising the timeout limits on the MongoDB client. For this, we first check with the customer on the settings that they use on their client.

What is the default value of a connection timeout?

The default value of a connection timeout depends on the version and language of the driver. Mongo Java & Ruby latest driver versions have a 10s default timeout for connection establishments while the NodeJs driver has no timeout. If the timeout is too high, you risk stalling your application.

How does the mongoclient select the server?

This option was introduced in the newer version of next-generation Mongo drivers (version 3.2.x+ in Java). For each type of operation and user preference, the MongoClient selects the server using a selection algorithm to execute the operation.


1 Answers

Yeah, took me a while to figure this out.

The connection timeout and socket timeout are now both in SocketSettings (with the latter renamed as readTimeout).

So it would look something like the following (where you can replace the 1's with your inputs):

  @Bean
  public MongoClientSettings mongoSetting() {

    return MongoClientSettings.builder()
        .applyToSocketSettings(builder -> {
          builder.connectTimeout(1, MILLISECONDS);
          builder.readTimeout(1, MILLISECONDS);
        })
        .applyToClusterSettings( builder -> builder.serverSelectionTimeout(1, MILLISECONDS))
        .applyConnectionString(new ConnectionString("<your-connection-string>"))

        .build();
  }

I found the need to set the connection string here (in addition to having it set via spring.data.mongodb.uri). Go figure.

like image 133
David Siegal Avatar answered Oct 24 '22 10:10

David Siegal