Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Mongo Timeout in Spring Boot

I am using spring boot web application which connects to mongo db which is working out of the box. I just use the following properties:

spring.data.mongodb.host=myHost
spring.data.mongodb.port=27017
spring.data.mongodb.database=myDatabase
spring.data.mongo.repositories.enabled=true
spring.data.mongodb.username=myUser
spring.data.mongodb.password=myPassword

The default timeout to MongoDB is 10 seconds. I would like to configure the timeout. I tried doing spring.data.mongodb.socketTimeout=2 or spring.data.mongodb.connectionTimeout=2

None of the properties work. Is it something that I can specify in the properties and the Spring framework will take care of it or can someone give example of doing it by declaring the Bean.

like image 370
Noor Syed Avatar asked Apr 22 '16 02:04

Noor Syed


3 Answers

To follow your theme, this would be the best to answer your question. Looks like you would like to set a timeout in your application.properties file.

In order to do so:

Instead of

spring.data.mongodb.host=myHost
spring.data.mongodb.port=27017
spring.data.mongodb.database=myDatabase

spring.data.mongodb.username=myUser
spring.data.mongodb.password=myPassword

Try this

spring.data.mongodb.uri=mongodb://myUser:myPassword@myHost:27017,myHost:27017/myDatabase?serverSelectionTimeoutMS=2000&connectTimeoutMS=2000
spring.data.mongodb.database=myDatabase
spring.data.mongo.repositories.enabled=true

Modify time in milliseconds to desired time.

like image 173
Jodi Avatar answered Sep 19 '22 19:09

Jodi


For Spring Data with Reactive MongoDb:

My case was a bit different (I had closing connections - which throws "Caused by: java.lang.IllegalStateException: state should be: open")

But You can use this with your case.

application.yml:

spring.data.mongodb:
  host: localhost
  database: myDb
  port: 27017
  username: admin
  password: test

If Your implementation use MongoReactiveAutoConfiguration for bean creation, then You can configure it with this bean:

@Bean
public MongoClientSettings mongoClientSettings() {
    final MongoClientSettings clientSettings = MongoClientSettings.builder()
            .retryWrites(true)
            .applyToConnectionPoolSettings((ConnectionPoolSettings.Builder builder) -> {
                builder.maxSize(300) //connections count
                        .minSize(100)
                        .maxConnectionLifeTime(0, TimeUnit.MILLISECONDS)
                        .maxConnectionIdleTime(0, TimeUnit.MILLISECONDS)
                        .maxWaitTime(5000, TimeUnit.MILLISECONDS)
                        .maxWaitQueueSize(5000);
            })
            .applyToSocketSettings(builder -> {
                builder.connectTimeout(2000, TimeUnit.MILLISECONDS);
            })
            .applicationName("app")
            .build();

    return clientSettings;
}
like image 31
John Tribe Avatar answered Sep 21 '22 19:09

John Tribe


This will override the Spring Boot autoconfiguration:

@Configuration
public class MongoDbSettings {

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

}
like image 23
Berthier Lemieux Avatar answered Sep 22 '22 19:09

Berthier Lemieux