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