[英]MongoClientURI connection string throwing error “com.mongodb.MongoSocketReadException: Prematurely reached end of stream”
public void MongoDBClient(String user, String pwd, String dbName, String collectionName) {
MongoClientURI uri = new MongoClientURI("mongodb+srv://" + user + ":" + pwd + "@cluster0.mff6p.mongodb.net/"
+ dbName + "?retryWrites=true&w=majority&connectTimeoutMS=30000&socketTimeoutMS=30000");
try (MongoClient mongoClient = new MongoClient(uri)) {
MongoDatabase database = mongoClient.getDatabase(dbName);
MongoCollection<Document> collection = database.getCollection(collectionName);
Document query = new Document("_id", new ObjectId("5f05e46281048f54ac98c455"));
Document result = collection.find(query).iterator().next();
System.out.println(result);
System.out.println("Test3: " + result.getString("Cluster"));
}
}
Getting exception in the above code -在上面的代码中出现异常 -
INFO: Exception in monitor thread while connecting to server cluster0-shard-00-01.mff6p.mongodb.net:27017
com.mongodb.MongoSocketReadException: Prematurely reached end of stream
at com.mongodb.internal.connection.SocketStream.read(SocketStream.java:112)
at com.mongodb.internal.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:580)
at com.mongodb.internal.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:445)
at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:299)
at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:259)
at com.mongodb.internal.connection.CommandHelper.sendAndReceive(CommandHelper.java:83)
at com.mongodb.internal.connection.CommandHelper.executeCommand(CommandHelper.java:33)
at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:105)
at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:62)
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:129)
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117)
at java.base/java.lang.Thread.run(Thread.java:834)
Can anyone help me, i think there is an issue with the connection string url, MongoClientURI...谁能帮助我,我认为连接字符串 url,MongoClientURI 存在问题...
Its seems that your connection was terminated by the mongo server.看来您的连接已被 mongo 服务器终止。 It could have multiple reasons and best way to debug is to look at the server logs.它可能有多种原因,最好的调试方法是查看服务器日志。
Usually the following is the issue:通常会出现以下问题:
From 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.没有它,一段时间后您可能会开始看到“连接关闭”错误,这似乎是没有原因的。
Try to enable to keepAlive
property.尝试启用keepAlive
属性。
server: {
socketOptions: {
keepAlive: 100,
connectTimeoutMS: 30000
}
}
Try like this像这样试试
const MongoClient = require('mongodb').MongoClient;
const dburl = "mongodb+srv://" + user + ":" + pwd + "@cluster0.mff6p.mongodb.net/"
+ dbName + "?retryWrites=true&w=majority&connectTimeoutMS=30000&socketTimeoutMS=30000";
MongoClient.connect(dburl,{useNewUrlParser:true,useUnifiedTopology:true},(err,client) => {
if(err){
console.log("Error",err);
}
else{
console.log("Connected");
}
});
THANKS谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.