【问题标题】:Spring and MongoDB Atlas interrupt connection after 5min of inactivitySpring 和 MongoDB Atlas 在 5 分钟不活动后中断连接
【发布时间】:2017-11-11 23:47:22
【问题描述】:

我有一个小问题,我在 MongoDB Atlas 上创建了一个帐户(云解决方案,我为“测试”使用了一个免费帐户),我的 Spring Boot 应用程序使用 Atlas 中的数据库。( MongoDB Database structure )

一切都很好,我启动了应用程序并且它工作了,但是如果我在 5 分钟内停止从我的 spring 应用程序发送请求,当我发送一个请求时我得到一个异常:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception 
[Request processing failed; nested exception is org.springframework.data.mongodb.UncategorizedMongoDbException: 
Prematurely reached end of stream; nested exception is com.mongodb.MongoSocketReadException: Prematurely reached end of stream] with root cause 
com.mongodb.MongoSocketReadException: Prematurely reached end of stream

我也得到了这个例外:

com.mongodb.MongoSocketException: rscuisine-shard-00-02-xgxgc.mongodb.net
com.mongodb.MongoSocketException: rscuisine-shard-00-01-xgxgc.mongodb.net
com.mongodb.MongoSocketException: rscuisine-shard-00-00-xgxgc.mongodb.net

在这个异常之后,如果我再次发送请求,它就会起作用。这就像一个超时问题...

我用这个类(和一个 application.properties 文件)连接数据库:

 @Configuration
 @PropertySource("classpath:application.properties")
 public class MongoConfig extends AbstractMongoConfiguration {  

@Value("${spring.data.mongodb.database}")
 private String DB;

@Value("${spring.data.mongodb.uri}")
private String URI;

protected String getURI() {
    return URI;
}

@Override
public Mongo mongo() throws Exception {
    return new MongoClient(new MongoClientURI(URI));
}

@Override
protected String getMappingBasePackage() {
    return "xx.xxxxxxxx.xxxxxxxxx.xxxxxxxxxxx";
}

@Override
protected String getDatabaseName() {
    return DB;
    }
}

application.properties 文件中的 URI 是这样的:

mongodb://XXXX:XXXX@rscuisine-shard-00-00-xgxgc.mongodb.net:27017,rscuisine-shard-00-01-xgxgc.mongodb.net:27017,rscuisine-shard-00-02-xgxgc.mongodb.net:27017/XXXX?ssl=true&replicaSet=RSCuisine-shard-0&authSource=admin

我检查了 MongoClientURI 类,并且有用于超时的参数,但没有任何作用......你有什么想法吗?

非常感谢。

【问题讨论】:

    标签: java spring mongodb mongodb-query database


    【解决方案1】:

    我在 Atlas 上注意到了同样的事情,但对我来说,在连接字符串上设置超时选项 maxIdleTimeMS确实似乎已经解决了这个问题。

    mongodb+srv://USER:PASS@xxx.yyy.mongodb.net/?retryWrites=true&w=majority&maxIdleTimeMS=300000
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-30
      • 2020-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      • 2015-08-13
      相关资源
      最近更新 更多