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.
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.
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.
“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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With