Daniel
Daniel

Reputation: 221

Spring boot detect mongodb connection error located in another maven module (MongoSocketOpenException)

I have a project in java called: "lydsam".
The first module is called: "bd", it is my connection with my database.

@Configuration
public class ConnectionBD {

    @Bean
    public MongoDatabaseFactory mongoDatabaseFactory(){
        return new SimpleMongoClientDatabaseFactory("mongodb://localhost:27017/lydsam");
    }

    @Bean
    public MongoTemplate mongoTemplate() {
        try{
            return new MongoTemplate(mongoDatabaseFactory());
        }catch(Exception e){
            System.out.println("offline with mongodb");
            System.out.println(e.getMessage());
            return null;
        }
    }
}

and I have another module called: "sales"

@SpringBootApplication(scanBasePackages = { "org.lydsam.sales",
            "org.lydsam.bd" })
public class Sales_aplicacion {

    public static void main(String[] args) {
        try {
            SpringApplication.run(Sales_aplicacion.class, args);
        } catch (Exception e) {
            System.out.println("Error executing in application 'Sales': " + e.getMessage());
        }
    }
}

Everything works fine when I run the application, I would like to catch the error when I disconnect my database and then show it to the console with a message.
I tried using "try catch" but it didn't work for me and I get the following error.

com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.0.3.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:127) ~[mongodb-driver-core-4.0.3.jar:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-4.0.3.jar:na]
at java.base/java.lang.Thread.run(Thread.java:830) ~[na:na]
Caused by: java.net.ConnectException: Connection refused: no further information
at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:579) ~[na:na]
at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:549) ~[na:na]
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597) ~[na:na]
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:339) ~[na:na]
at java.base/java.net.Socket.connect(Socket.java:603) ~[na:na]
at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:63) ~[mongodb-driver-core-4.0.3.jar:na]
at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-4.0.3.jar:na]
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-4.0.3.jar:na]
... 3 common frames omitted  

Please I need help.

Upvotes: 1

Views: 1004

Answers (1)

s7vr
s7vr

Reputation: 75984

Wrap your code with try catch for MongoTimeoutException. You can find more details in my other answer

Upvotes: 2

Related Questions