繁体   English   中英

如何修复 com.mongo.MongoSocketReadException 并在尝试插入文档时出现消息“过早到达 Stream 的末尾”?

[英]How do I fix a com.mongo.MongoSocketReadException, with the message "Prematurely reached the end of Stream" on attempting to insert a document?

我正在为我的项目使用 Spring Boot,并且我计划将我的应用程序必须的用户数据迁移到 MongoDB。 插入工作了一段时间,但现在突然间,我收到了这个堆栈跟踪:

com.mongodb.MongoSocketReadException: Prematurely reached end of stream
    at com.mongodb.connection.SocketStream.read(SocketStream.java:87)
    at com.mongodb.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:547)
    at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:418)
    at com.mongodb.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:290)
    at com.mongodb.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255)
    at com.mongodb.connection.CommandHelper.sendAndReceive(CommandHelper.java:84)
    at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:34)
    at com.mongodb.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:91)
    at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:51)
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:127)
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114)
    at java.base/java.lang.Thread.run(Thread.java:832)

然后 Mongo 尝试更新集群描述,然后关闭连接,并以此堆栈跟踪结束:

Exception in thread "main" com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches com.mongodb.Mongo$4@75d4a5c2. Client view of cluster state is {type=REPLICA_SET, servers=[{address=sorinorpgcluster-shard-00-00.huchg.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Prematurely reached end of stream}}, {address=sorinorpgcluster-shard-00-01.huchg.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Prematurely reached end of stream}}, {address=sorinorpgcluster-shard-00-02.huchg.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Prematurely reached end of stream}}]
    at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:389)
    at com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:108)
    at com.mongodb.Mongo.getConnectedClusterDescription(Mongo.java:887)
    at com.mongodb.Mongo.createClientSession(Mongo.java:877)
    at com.mongodb.Mongo$3.getClientSession(Mongo.java:866)
    at com.mongodb.Mongo$3.execute(Mongo.java:834)
    at com.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:1025)
    at com.mongodb.MongoCollectionImpl.executeInsertOne(MongoCollectionImpl.java:513)
    at com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:493)
    at com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:487)
    at ProfileMigration.main(ProfileMigration.java:18)

这是代码:

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

public class ProfileMigration {
    public static void main(String[] args){

       MongoClientURI uri =
               new MongoClientURI("mongodb+srv://user:pass@sorinorpgcluster.huchg.mongodb.net/user?retryWrites=true&w=majority");

       MongoClient mongoClient = new MongoClient(uri);
       MongoDatabase db = mongoClient.getDatabase("user");
       MongoCollection<Document> collection =  db.getCollection("account");


       collection.insertOne(new Document("test", "test"));
    }
}

我对如何解决这个问题感到非常困惑,我尝试过使用 MongoClientOptions 但这似乎不起作用。 我正在使用 MongoDB 版本 3.6.4。

设置 maxIdleTimeMS < 30000ms

确保您的 IP 地址在 Mongo 云上列入白名单

您可以在左侧面板中找到它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM