gpt4 book ai didi

java - Spring 和 MongoDB Atlas 在 5 分钟不活动后中断连接

转载 作者:行者123 更新时间:2023-12-01 13:47:21 25 4
gpt4 key购买 nike

我有一个小问题,我在 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

我也得到了这个异常(exception):
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 类,并且有超时东西的参数,但没有任何作用......你有什么想法吗?

非常感谢。

最佳答案

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

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

关于java - Spring 和 MongoDB Atlas 在 5 分钟不活动后中断连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44466595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com