MongoSocketReadException :Prematurely reached end of stream ,while connecting MongoDB Atlas by Spring Boot
Asked Answered
C

4

1

I am using MongoDB atlas (cluster) to connect to my spring boot application. 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. I tried to make some changes in the mongodb cluster URI such as:

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

spring.data.mongodb.uri=mongodb+srv://emuser:[email protected]/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. Below is the snippet of the code where I have used 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>
Cosh answered 18/4, 2020 at 13:31 Comment(0)
M
2

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. 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.

Meilen answered 14/1, 2021 at 13:37 Comment(0)
C
1

Along with maxIdleTimeMS, try setting a keepAlive value manually too.

Also, refer to solutions in this related question.

Covarrubias answered 18/4, 2020 at 13:39 Comment(1)
I tried adding this as well but the issue remains the sameCosh
P
0

I got this error when i tried to connect to mongodb on atlas cluster from my springboot app. Adding allow access from anywhere (0.0.0.0/0) to network permission list fixed my issue.

Pinstripe answered 17/7, 2023 at 15:15 Comment(0)
T
0

Add your ip to the acl, it worked for me

enter image description here

Testify answered 9/8, 2023 at 14:22 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.