[英]MongoSocketReadException: Prematurely reached end of stream (after a period of inactivity)
I get this error on a find
call (default Java Driver) after a period of inactivity.在一段时间不活动后,我在
find
调用(默认 Java 驱动程序)中收到此错误。 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 (ie not in a local context).
我只有在连接到 compose 上的实例时才会遇到问题(即不在本地上下文中)。
MongoDB version is 3.2.8, latest driver (3.3), using Java 8. MongoDB版本为3.2.8,最新驱动(3.3),使用Java 8.
Any idea?任何想法?
I found it in some documentation:我在一些文档中找到了它:
For long running applications, it is often prudent to enable "keepAlive" with a number of milliseconds.对于长时间运行的应用程序,通常谨慎的做法是在几毫秒内启用“keepAlive”。 Without it, after some period of time you may start to see "connection closed" errors for what seems like no reason.
没有它,一段时间后您可能会开始看到“连接关闭”错误,这似乎是没有原因的。
Check if this helps.检查这是否有帮助。 When you connect to mongoDB you can pass socket options to it.
当您连接到 mongoDB 时,您可以将套接字选项传递给它。 I am from node background we use following options to keep it alive.
我来自节点背景,我们使用以下选项来保持它的活力。
server: {
socketOptions: {
keepAlive: 100,
connectTimeoutMS: 30000
}
}
Hope this helps希望这可以帮助
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,服务器是 mongodb on GCP atlas M0 3.6.6。
Edit application.properties like this:像这样编辑 application.properties:
spring.data.mongodb.uri = mongodb+srv://username:password@solarsystem-1tpu0.mongodb.net/dbname
For regular mongo instances (non-clustered) use this:对于常规 mongo 实例(非集群),请使用:
spring.data.mongodb.uri = mongodb://username:password@solarsystem-shard-00-00-1tpu0.mongodb.net:27017,hostname2:27017/dbname?ssl=true
I agree with Rhangaun's answer here is my solution 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.希望它可以帮助你。
To me it was a whole different issue - I was using mongo-java-server with Fongo and ended up getting this error.对我来说,这是一个完全不同的问题——我将mongo-java-server与Fongo一起使用,最终得到了这个错误。 Turns out that old versions of it are not compatible with
FieldType.DECIMAL128
conversions .原来它的旧版本与
FieldType.DECIMAL128
转换不兼容。
Updating it to the latest one (1.36.0 currently) fixed the issue for me.将其更新到最新版本(目前为 1.36.0)为我解决了这个问题。
The problem is that Mongodb end the connection.问题是 Mongodb 结束连接。 You need to increase the timeout of the Mongodb Driver here is an example code.
您需要增加 Mongodb 驱动程序的超时时间 这里是一个示例代码。
MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
//build the connection options
builder.maxConnectionIdleTime(86400000);//set the max wait time in (ms)
MongoClientOptions opts = builder.build();
final Morphia morphia = new Morphia();
morphia.mapPackage("com.java.code");
final String hostURL = "host_url";
MongoCredential credential = MongoCredential.createCredential("username","database","Password".toCharArray());
ServerAddress address = new ServerAddress(hostURL);
List<MongoCredential> credentialList = new ArrayList<>();
credentialList.add(credential);
final MongoClient client = new MongoClient(address,credentialList,opts);
// create the Datastore connecting to the default port on the local host
datastore = morphia.createDatastore(client,"datastore");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.