Java MongoSocketReadException:过早到达流末尾(一段时间不活动后)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39079876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MongoSocketReadException: Prematurely reached end of stream (after a period of inactivity)
提问by Rhangaun
I get this error on a find
call (default Java Driver) after a period of inactivity. I tried to add a manual heartbeat (writing to a capped collection), but it didn't help. I only get the issue while being connected to an instance on compose (i.e. not in a local context).
find
在一段时间不活动后,我在调用(默认 Java 驱动程序)时收到此错误。我尝试添加手动心跳(写入上限集合),但没有帮助。我只在连接到 compose 实例时遇到问题(即不在本地上下文中)。
MongoDB version is 3.2.8, latest driver (3.3), using Java 8.
MongoDB 版本为 3.2.8,最新驱动程序 (3.3),使用 Java 8。
Any idea ?
任何的想法 ?
采纳答案by Daqian
I agree with Rhangaun's answer here is my soluction in code of JAVA:
我同意 Rhangaun 的回答,这是我在 JAVA 代码中的解决方案:
public static DB getMongoDB() {
MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
//build the connection options
builder.maxConnectionIdleTime(60000);//set the max wait time in (ms)
MongoClientOptions opts = builder.build();
char[] password2 = "mypassword".toCharArray();
MongoCredential credential2 = MongoCredential.createCredential("username", "databasename",password2);
//add your option to the connection
MongoClient mongoClient = new MongoClient(new ServerAddress("server ip",27017), Arrays.asList(credential2),opts);
//use your database
cachedDb = mongoClient.getDB("databasename");
return cachedDb;
}
Here is my research link:http://3t.io/blog/how-to-prevent-your-connection-from-dropping-with-hosted-mongodb-instances/
这是我的研究链接:http: //3t.io/blog/how-to-prevent-your-connection-from-dropping-with-hosted-mongodb-instances/
Hope it helps you.
希望对你有帮助。
回答by Sachin
I found it in some documentation:
我在一些文档中找到了它:
For long running applications, it is often prudent to enable "keepAlive" with a number of milliseconds. Without it, after some period of time you may start to see "connection closed" errors for what seems like no reason.
对于长时间运行的应用程序,以毫秒为单位启用“keepAlive”通常是谨慎的做法。没有它,一段时间后您可能会开始看到“连接关闭”错误,这似乎没有任何原因。
Check if this helps. When you connect to mongoDB you can pass socket options to it. I am from node background we use following options to keep it alive.
检查这是否有帮助。当您连接到 mongoDB 时,您可以将套接字选项传递给它。我来自节点背景,我们使用以下选项使其保持活动状态。
server: {
socketOptions: {
keepAlive: 100,
connectTimeoutMS: 30000
}
}
Hope this helps!!
希望这可以帮助!!
回答by iengchen
I solve this problem by set sslEnabled to true,code sample:
我通过将 sslEnabled 设置为 true 来解决这个问题,代码示例:
@Bean
public MongoClient mongoClient() {
List<ServerAddress> saList = new ArrayList<>();
saList.add(new ServerAddress("cluster0-shard-00-00-75shm.gcp.mongodb.net", 27017));
saList.add(new ServerAddress("cluster0-shard-00-01-75shm.gcp.mongodb.net", 27017));
saList.add(new ServerAddress("cluster0-shard-00-02-75shm.gcp.mongodb.net", 27017));
char[] pwd = "password".toCharArray();
MongoCredential credential = MongoCredential.createCredential("username", "admin", pwd);
//set sslEnabled to true here
MongoClientOptions options = MongoClientOptions.builder()
.readPreference(ReadPreference.primaryPreferred())
.retryWrites(true)
.requiredReplicaSetName("Cluster0-shard-0")
.maxConnectionIdleTime(6000)
.sslEnabled(true)
.build();
MongoClient mongoClient = new MongoClient(saList, credential, options);
return mongoClient;
}
Addition: my client jar is org.mongodb.mongodb-driver 3.6.4,server is mongodb atlas M0 3.6.6 on GCP
另外:我的客户端 jar 是 org.mongodb.mongodb-driver 3.6.4,服务器是 GCP 上的 mongodb atlas M0 3.6.6
回答by jmojico
This worked for me in spring boot and cloud based mongo (Atlas clustered instances).
这在 Spring Boot 和基于云的 mongo(Atlas 集群实例)中对我有用。
Edit application.properties like this:
像这样编辑 application.properties:
spring.data.mongodb.uri = mongodb+srv://username:[email protected]/dbname
For regular mongo instances(non-clustered) use this:
对于常规 mongo 实例(非集群),请使用:
spring.data.mongodb.uri = mongodb://username:[email protected]:27017,hostname2:27017/dbname?ssl=true
- If you want to set other connection options, you can chain multiple parameters using '&' ; documentation here: https://mongodb.github.io/mongo-java-driver/3.4/javadoc/com/mongodb/MongoClientURI.html
- If you were using other spring.data.mongodb parameters, you should remove all of themotherwise spring won't read spring.data.mongodb.uri
- 如果要设置其他连接选项,可以使用“&”链接多个参数;这里的文档:https: //mongodb.github.io/mongo-java-driver/3.4/javadoc/com/mongodb/MongoClientURI.html
- 如果您使用其他spring.data.mongodb 参数,则应删除所有参数, 否则 spring 不会读取 spring.data.mongodb.uri
回答by Kokkonda Abhilash
My property files has the URL like this
我的属性文件有这样的 URL
spring.data.mongodb.uri = mongodb://username:password@<server_name>/<db_name>?ssl=false&sslInvalidHostNameAllowed=true
I faced the same MongoSocketReadException: Prematurely reached end of stream. I changed the ssl=false
to true
and it worked fine.
我遇到了同样的 MongoSocketReadException:过早到达流的结尾。我改变了ssl=false
,以true
它工作得很好。