简体   繁体   English

MongoSocketReadException:过早到达 stream 的末尾(一段时间不活动后)

[英]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。

This worked for me in spring boot and cloud based mongo (Atlas clustered instances).这在 spring 引导和基于云的 mongo(Atlas 集群实例)中对我有用。

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-serverFongo一起使用,最终得到了这个错误。 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.

相关问题 MongoSocketReadException:过早到达流末尾(使用 ssl 从 Java 到 Mongo) - MongoSocketReadException: Prematurely reached end of stream (Java to Mongo using ssl) mongoDB和Spark:“ com.mongodb.MongoSocketReadException:过早到达流的末尾” - mongoDB & Spark: “com.mongodb.MongoSocketReadException: Prematurely reached end of stream” com.mongodb.MongoSocketReadException:过早到达流的结尾 - com.mongodb.MongoSocketReadException: Prematurely reached end of stream 将Java应用程序连接到MongoDB-com.mongodb.MongoSocketReadException:已提前到达流的末尾 - Connect Java- Application to MongoDB - com.mongodb.MongoSocketReadException: Prematurely reached end of stream 获取com.mongodb.MongoSocketReadException:过早地到达流MongoDB的末尾 - Getting com.mongodb.MongoSocketReadException: Prematurely reached end of stream- MongoDB Mongo客户端尝试关闭不存在/创建的连接,并因MongoSocketReadException失败:过早到达流的末尾 - Mongo client trying to close connection that didn't exist/create and fails with MongoSocketReadException: Prematurely reached end of stream 如何修复 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? MongoSocketReadException:在 Spring 引导连接 MongoDB Atlas 时,过早到达 stream 的末尾 - MongoSocketReadException :Prematurely reached end of stream ,while connecting MongoDB Atlas by Spring Boot MongoClientURI 连接字符串抛出错误“com.mongodb.MongoSocketReadException:过早到达流的末尾” - MongoClientURI connection string throwing error “com.mongodb.MongoSocketReadException: Prematurely reached end of stream” 读取inputstream时过早到达DB2流的末尾 - DB2 end of stream prematurely reached while reading inputstream
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM