Problems connecting to Atlas with Spring Data: connection reset

    MongoCredential credential = MongoCredential.createCredential("username", "database1", "password".toCharArray());
    MongoClientSettings settings = MongoClientSettings.builder()
            .credential(credential)
            .retryWrites(true)
            .applyToConnectionPoolSettings(builder ->
                    builder.maxConnectionIdleTime(5000, TimeUnit.MILLISECONDS))
            .applyToClusterSettings(builder -> {
                builder.hosts(Arrays.asList(
                   new ServerAddress("mongastreamlistener-shard-00-00-ja2vb.mongodb.net", 27017),
                    new ServerAddress("mongastreamlistener-shard-00-01-ja2vb.mongodb.net", 27017),
                    new ServerAddress("mongastreamlistener-shard-00-02-ja2vb.mongodb.net", 27017)
                ));
            })

            .build();

I have tried passing the setting the connection spring in the application.properties → spring.data.mongodb.uri=

I am getting the below errors:

at com.mongodb.internal.connection.InternalStreamConnection.translateReadException(InternalStreamConnection.java:568) ~[mongodb-driver-core-4.0.4.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:447) ~[mongodb-driver-core-4.0.4.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:298) ~[mongodb-driver-core-4.0.4.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:258) ~[mongodb-driver-core-4.0.4.jar:na]
at com.mongodb.internal.connection.CommandHelper.sendAndReceive(CommandHelper.java:83) ~[mongodb-driver-core-4.0.4.jar:na]
at com.mongodb.internal.connection.CommandHelper.executeCommand(CommandHelper.java:33) ~[mongodb-driver-core-4.0.4.jar:na]
at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:103) ~[mongodb-driver-core-4.0.4.jar:na]
at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:60) ~[mongodb-driver-core-4.0.4.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:128) ~[mongodb-driver-core-4.0.4.jar:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-4.0.4.jar:na]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_73]

Caused by: java.net.SocketException: Connection reset

Hello Tom,

Are you able to connect to your cluster from mongo shell?

Haven’t tried. I am able to connect to the cluster with python. I can perform CRUD operations fine with python without any issues. The issue I am having is connecting to the Cluster with Spring, seems like a SSL issue, but I am not forsure?

Which version of the driver are you using?

If using maven, please provide Mongo related elements from your pom.xml.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.patriotech</groupId>
	<artifactId>mongabackend</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>mongabackend</name>
	<description>Monga Social</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-mongodb</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

The driver version is 4.0.4

I added this to the POM.xml

org.mongodb
mongodb-driver-sync
3.8.1

Know I am getting this error:
Servlet.service() for servlet [dispatcherServlet] in context with path threw exception [Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: com/mongodb/connection/DefaultClusterFactory] with root cause java.lang.ClassNotFoundException: com.mongodb.connection.DefaultClusterFactory

Please post the code you have used to connect from PyMongo.

EDIT ADD: The POM.xml is good.

@Tom_Marler

I can connect to database on a replica-set on localhost and query using MongoTemplate API. It is a Spring Boot application (2.3.1) with similar Spring configuration and Java Driver (4.0.4).

The MongoClient is created using the following:

 MongoClient mongoClient = MongoClients.create(
    MongoClientSettings.builder()
        .applyToClusterSettings(builder ->
            builder.hosts(Arrays.asList(
                new ServerAddress("localhost", 30001),
                new ServerAddress("localhost", 30002),
                new ServerAddress("localhost", 30003))))
    .build());

Could you try with com.mongodb.MongoClientURI and the SRV string?

MongoClientURI uri = new MongoClientURI(  "mongodb+srv://UserName:Password@mongastreamlistener-ja2vb.mongodb.net/database1?retryWrites=true&connectTimeoutMS=5000" ) ;
MongoClient client = new MongoClient( uri ) ;