Set Mongo Timeout in Spring Boot

后端 未结 7 1125
失恋的感觉
失恋的感觉 2020-12-30 10:21

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.h         


        
7条回答
  •  孤城傲影
    2020-12-30 10:57

    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;
    }
    

提交回复
热议问题