简体   繁体   中英

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

I am using Spring Boot for my project and I am planning to migrate the user data my application has to MongoDB. Inserting was working for a while, but now all of a sudden, I am receiving this Stack Trace:

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)

This is then followed by Mongo attempting to Update the Cluster description, then closing the connection, and ended with this stack trace:

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)

Here is the code:

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"));
    }
}

I am very confused as to how to fix this, I have tried using MongoClientOptions and that does not seem to work. I am using MongoDB version 3.6.4.

Set maxIdleTimeMS < 30000ms

Be sure your IP address is whitelisted on Mongo cloud

You can find it on the left panel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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