• <i id='XWX6s'><tr id='XWX6s'><dt id='XWX6s'><q id='XWX6s'><span id='XWX6s'><b id='XWX6s'><form id='XWX6s'><ins id='XWX6s'></ins><ul id='XWX6s'></ul><sub id='XWX6s'></sub></form><legend id='XWX6s'></legend><bdo id='XWX6s'><pre id='XWX6s'><center id='XWX6s'></center></pre></bdo></b><th id='XWX6s'></th></span></q></dt></tr></i><div id='XWX6s'><tfoot id='XWX6s'></tfoot><dl id='XWX6s'><fieldset id='XWX6s'></fieldset></dl></div>

    <tfoot id='XWX6s'></tfoot>

        <bdo id='XWX6s'></bdo><ul id='XWX6s'></ul>
    1. <small id='XWX6s'></small><noframes id='XWX6s'>

    2. <legend id='XWX6s'><style id='XWX6s'><dir id='XWX6s'><q id='XWX6s'></q></dir></style></legend>

      1. Spring mongo 查询设置自定义超时

        时间:2024-08-23

            <legend id='0kiaw'><style id='0kiaw'><dir id='0kiaw'><q id='0kiaw'></q></dir></style></legend>
          • <small id='0kiaw'></small><noframes id='0kiaw'>

              • <bdo id='0kiaw'></bdo><ul id='0kiaw'></ul>

                <i id='0kiaw'><tr id='0kiaw'><dt id='0kiaw'><q id='0kiaw'><span id='0kiaw'><b id='0kiaw'><form id='0kiaw'><ins id='0kiaw'></ins><ul id='0kiaw'></ul><sub id='0kiaw'></sub></form><legend id='0kiaw'></legend><bdo id='0kiaw'><pre id='0kiaw'><center id='0kiaw'></center></pre></bdo></b><th id='0kiaw'></th></span></q></dt></tr></i><div id='0kiaw'><tfoot id='0kiaw'></tfoot><dl id='0kiaw'><fieldset id='0kiaw'></fieldset></dl></div>
                    <tbody id='0kiaw'></tbody>
                  <tfoot id='0kiaw'></tfoot>
                  本文介绍了Spring mongo 查询设置自定义超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想降低 spring-mongo java 应用程序中的超时设置(如果无法访问数据库,查询应该在 300 毫秒后失败).

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

                  我试过这个配置:

                  @Configuration
                  public class MongoConfiguration {
                  
                  private String mongoUri = "mongodb://127.0.0.1:27017/myDb?connectTimeoutMS=300&socketTimeoutMS=300&waitQueueTimeoutMS=300&wtimeoutMS=300";
                  
                  @Bean
                  public MongoDbFactory mongoDbFactory() throws Exception {
                      Builder options = new MongoClientOptions.Builder().socketTimeout(300).connectTimeout(300).maxWaitTime(300);
                      return new SimpleMongoDbFactory(new MongoClientURI(mongoUri, options));
                  }
                  
                  @Bean
                  public MongoTemplate mongoTemplate() throws Exception {
                      MongoDbFactory mongoDbFactory = mongoDbFactory();
                      MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory);
                      return mongoTemplate;
                  }
                  
                  }
                  

                  但是 mongoUri 选项和构建器都会更改超时:查询仅在 30 000 毫秒后失败.

                  But the mongoUri options nor the builder change the timeout: the query fails only after 30 000ms.

                  我不确定应该覆盖哪个参数,也不确定如何正确执行.

                  I am not sure which parameter I should override nor the way to do it properly.

                  感谢您的帮助

                      <dependency>
                          <groupId>org.mongodb</groupId>
                          <artifactId>mongo-java-driver</artifactId>
                          <version>3.4.0</version>
                      </dependency>
                      <dependency>
                          <groupId>org.springframework.data</groupId>
                          <artifactId>spring-data-mongodb</artifactId>
                          <version>1.9.5.RELEASE</version>
                      </dependency>
                  

                  推荐答案

                  我在这里找到了答案: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));
                  }
                  

                  这篇关于Spring mongo 查询设置自定义超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:通过 jdbc 模板使用两个数据源 下一篇:Spring Data MongoDB 日期之间

                  相关文章

                  1. <i id='7Kxrr'><tr id='7Kxrr'><dt id='7Kxrr'><q id='7Kxrr'><span id='7Kxrr'><b id='7Kxrr'><form id='7Kxrr'><ins id='7Kxrr'></ins><ul id='7Kxrr'></ul><sub id='7Kxrr'></sub></form><legend id='7Kxrr'></legend><bdo id='7Kxrr'><pre id='7Kxrr'><center id='7Kxrr'></center></pre></bdo></b><th id='7Kxrr'></th></span></q></dt></tr></i><div id='7Kxrr'><tfoot id='7Kxrr'></tfoot><dl id='7Kxrr'><fieldset id='7Kxrr'></fieldset></dl></div>
                  2. <tfoot id='7Kxrr'></tfoot>

                      <bdo id='7Kxrr'></bdo><ul id='7Kxrr'></ul>

                      <small id='7Kxrr'></small><noframes id='7Kxrr'>

                      <legend id='7Kxrr'><style id='7Kxrr'><dir id='7Kxrr'><q id='7Kxrr'></q></dir></style></legend>