Spring mongo queries set custom timeout

后端 未结 1 1482
情话喂你
情话喂你 2021-01-20 03:19

I would like to lower the timeout setting in my spring-mongo java application (the query should fail after 300 ms if the database is not accessible).

I tried this c

1条回答
  •  礼貌的吻别
    2021-01-20 03:34

    I found the answer here: https://scalegrid.io/blog/understanding-mongodb-client-timeout-options/

    @Bean
    public MongoDbFactory mongoDbFactory() throws Exception {
        MongoClientOptions.Builder optionsBuilder = MongoClientOptions.builder();
        optionsBuilder.connectTimeout(300);
        optionsBuilder.socketTimeout(300);
        optionsBuilder.serverSelectionTimeout(300);
        return new SimpleMongoDbFactory(new MongoClientURI(mongoUri, optionsBuilder));
    }
    

    0 讨论(0)
提交回复
热议问题