简体   繁体   English

MongoSocketReadException:在 Spring 引导连接 MongoDB Atlas 时,过早到达 stream 的末尾

[英]MongoSocketReadException :Prematurely reached end of stream ,while connecting MongoDB Atlas by Spring Boot

I am using MongoDB atlas (cluster) to connect to my spring boot application.我正在使用 MongoDB 地图集(集群)连接到我的 spring 引导应用程序。 I was earlier able to successfully insert & get the data from the cluster but after few minutes of inactivity I started getting,我之前能够成功地插入并从集群中获取数据,但是在几分钟不活动后,我开始得到,

com.mongodb.MongoSocketReadException: Prematurely reached the end of stream . com.mongodb.MongoSocketReadException: Prematurely reached the end of stream I tried to make some changes in the mongodb cluster URI such as:我尝试在 mongodb 集群 URI 中进行一些更改,例如:

spring.data.mongodb.uri=mongodb+srv://emuser:empassword@emp-mate-bzmeh.gcp.mongodb.net/emp-mate-db?retryWrites=true&retryReads=true&w=majority and also tried spring.data.mongodb.uri=mongodb+srv://emuser:empassword@emp-mate-bzmeh.gcp.mongodb.net/emp-mate-db?retryWrites=true&retryReads=true&w=majority

spring.data.mongodb.uri=mongodb+srv://emuser:empassword@emp-mate-bzmeh.gcp.mongodb.net/emp-mate-db?ssl=true&retryWrites=true&retryReads=true&w=majority&maxIdleTimeMS=80

I have also checked the SSL settings in JRE and its fine & I did not also see any error log in the Alert section on MongoDB cluster.我还检查了 JRE 中的 SSL 设置及其正常,我在 MongoDB 集群的警报部分也没有看到任何错误日志。 Below is the snippet of the code where I have used MongoTemplate .下面是我使用MongoTemplate的代码片段。

import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import com.mongodb.client.result.UpdateResult;
import employeemate.repository.UsersRepository;
import employeemate.resources.Users;

    @Service
    public class UserService {
        @Autowired
        MongoTemplate mongoTemplate;
        @Autowired
        UsersRepository usersRepository;

        public void addSampleData() {
             System.out.println("Adding sample data");
             usersRepository.save(new Users("1","Ashu","test@gmail.com", 24, "Male", "1111111111", "Delhi"));
             usersRepository.save(new Users("2","Adam Clark", "adam@gmail.com",24,"Male", "2222222222", "Shelton CT"));

             }
        }

POM.xml POM.xml

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

Along with maxIdleTimeMS , try setting a keepAlive value manually too.maxIdleTimeMS ,尝试手动设置keepAlive值。

Also, refer to solutions in this related question .另外,请参阅此相关问题中的解决方案。

One possibility is that in MongoDB Atlas you have (sensibly) restricted access to specific IP addresses, and the IP address of the machine you are connecting from has changed because it is set dynamically.一种可能性是,在 MongoDB Atlas 中,您(明智地)限制了对特定 IP 地址的访问,并且您连接的机器的 IP 地址已更改,因为它是动态设置的。 If that's the case, you may have to add your IP address to your MongoDB Atlas account each time it changes, or set yourself up with a static IP address. If that's the case, you may have to add your IP address to your MongoDB Atlas account each time it changes, or set yourself up with a static IP address.

暂无
暂无

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

相关问题 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 MongoClientURI 连接字符串抛出错误“com.mongodb.MongoSocketReadException:过早到达流的末尾” - MongoClientURI connection string throwing error “com.mongodb.MongoSocketReadException: Prematurely reached end of stream” MongoSocketReadException:过早到达流末尾(使用 ssl 从 Java 到 Mongo) - MongoSocketReadException: Prematurely reached end of stream (Java to Mongo using ssl) MongoSocketReadException:过早到达 stream 的末尾(一段时间不活动后) - MongoSocketReadException: Prematurely reached end of stream (after a period of inactivity) 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? 读取inputstream时过早到达DB2流的末尾 - DB2 end of stream prematurely reached while reading inputstream
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM