1

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.

Daniel
  • 181
  • 4
  • 13
  • Your objective is to graceful shutdown your application when database was down. Correct? – Dinesh Dontha Aug 27 '20 at 06:56
  • @DineshDontha Yes, correct, but I want to display a message on the console showing that my database is offline. – Daniel Aug 27 '20 at 15:53
  • I tried to do some analysis. Generally, MongoDB in real time will be provided with high availability with multiple instances with load balancing among them. So, I found nothing on how to catch/handle such SocketExceptions which happen in Runtime. As of now, I think it would not be handled. Someone should help on this. – Dinesh Dontha Aug 27 '20 at 20:11

1 Answers1

2

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

s7vr
  • 73,656
  • 11
  • 106
  • 127