MongoSocketReadException:通过Spring Boot连接MongoDB Atlas时,过早地到达流的末端。

问题描述 投票:0回答:1

我正在使用MongoDB atlas(集群)连接到我的spring boot应用程序。我之前能够成功插入& 从集群中获取数据,但在几分钟的闲置后,我开始得到。

com.mongodb.MongoSocketReadException: Prematurely reached the end of stream.我试着在mongodb集群的URI中做了一些修改,比如:

spring.data.mongodb.uri=mongodb+srv://emuser:[email protected]/emp-mate-db?retryWrites=true&retryReads=true&w=majority 并且还尝试了

spring.data.mongodb.uri=mongodb+srv://emuser:[email protected]/emp-mate-db?ssl=true&retryWrites=true&retryReads=true&w=majority&maxIdleTimeMS=80

我也检查了JRE中的SSL设置,它很好&我也没有在MongoDB集群的Alert部分看到任何错误日志。下面是我使用的代码片段。 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","[email protected]", 24, "Male", "1111111111", "Delhi"));
             usersRepository.save(new Users("2","Adam Clark", "[email protected]",24,"Male", "2222222222", "Shelton CT"));

             }
        }

POM.xml

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
java mongodb spring-boot spring-data-mongodb
1个回答
1
投票

伴随着 maxIdleTimeMS,尝试设置一个 keepAlive 值也是手动的。

另外,请参考 此相关问题.

© www.soinside.com 2019 - 2024. All rights reserved.