>PROBLEM
Starting a project using Maven, JEE/Spring, and MongoDB driver (mongo-java-driver) throws the following exception:
...[cluster-ClusterId{value='62586e27f362ec2cd1336bda', description='null'}-localhost:27027] # INFO # org.mongodb.driver.cluster # method: info # Exception in monitor thread while connecting to server localhost:27027
com.mongodb.MongoSocketOpenException: Exception opening socket
...
Caused by: java.net.ConnectException: Connection refused: no further information at java.base/sun.nio.ch.Net.pollConnect(Native Method)
>SOLUTION
Although connection refusal may have many causes, this post will treat one of them that maybe it is your issue.
The "mongo-java-driver" tries to start MongoDB connection automatically as soon as the application starts if it finds MongoDB's configuration in .properties or .yml files.
It happens when we start a project using this default feature but later we decide to customize it, and there is no need for this feature anymore.
So, just remove or comment on the lines in the configuration file.
For instance, if .yml, do this:
spring:
profiles: prod
jpa:
database: default
# data:
# mongodb:
# host: localhost
# port: 27027
# database: mydatabase
If .properties:
#spring.data.mongodb.host=localhost
#spring.data.mongodb.port=27017
#spring.data.mongodb.database=mydatabase
>ENV
SpriingBoot 2.X
Java 17
MongoDB 5
- pom.xml:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.12.10</version>
</dependency>
No comments:
Post a Comment