skip to Main Content

I am using SpringBoot v2.7.0

I have following springboot config

spring:
  data:
    mongodb:
      uri: ${MONGO_DB_URL}
      database: ${MONGO_DB_DATABASE}

There is not explicit beans configurations. I have repositories for respective documents which extends MongoRepository<ModelNameClass, IDType>

This is all working fine. But now I want to handle the situation where i want to make sure that app still boots up if MongoDB is down. MongoDB is not our primary database.

How can i do so?

2

Answers


  1. There is an option to ignore the initializing of datasource when your application fails to start by setting this property:

    spring.sql.init.continue-on-error=true
    

    You can read more about this fail-fast-feature on Spring Boot documentation which is Spring Boot 2.7.0 Initialize a Database Using Basic SQL Scripts which says:

    By default, Spring Boot enables the fail-fast feature of its script-based database initializer. This means that, if the scripts cause exceptions, the application fails to start. You can tune that behavior by setting spring.sql.init.continue-on-error.

    Also if you want to handle when your database is down or up, you can create a bean for datasource and handle any exception which mean your database is down. In order to do this, create a class @Configuration which has datasource as bean like this:

    @Configuration
    public class DataSourceConfiguration {
    
        @Bean
        public DataSource dataSource() {
            try {
                // create datasource to your database 
            } catch (Exception) {
                // create a fake/dummy datasource to your database 
            }
        }
    }
    
    Login or Signup to reply.
  2. MongoDB not being available wouldn’t stop the app from starting successfully. Just tested this pointing the app to a local DB that didn’t exist. There is below exception in the logs, but the app starts up and the test controller endpoint I added is accessible and responds correctly:

    2022-08-13 07:08:10.093  INFO 14471 --- [localhost:27017] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server localhost:27017
    
    com.mongodb.MongoSocketOpenException: Exception opening socket
            at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.6.0.jar:na]
            at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:180) ~[mongodb-driver-core-4.6.0.jar:na]
            at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:193) ~[mongodb-driver-core-4.6.0.jar:na]
            at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:157) ~[mongodb-driver-core-4.6.0.jar:na]
            at java.base/java.lang.Thread.run(Thread.java:829) ~[na:na]
    Caused by: java.net.ConnectException: Connection refused (Connection refused)
            at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]
            at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:412) ~[na:na]
            at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:255) ~[na:na]
            at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:237) ~[na:na]
            at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:na]
            at java.base/java.net.Socket.connect(Socket.java:609) ~[na:na]
            at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:107) ~[mongodb-driver-core-4.6.0.jar:na]
            at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-4.6.0.jar:na]
            at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-4.6.0.jar:na]
            ... 4 common frames omitted
    
    2022-08-13 07:08:10.204  INFO 14471 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 1 endpoint(s) beneath base path '/actuator'
    2022-08-13 07:08:10.230  INFO 14471 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
    2022-08-13 07:08:10.250  INFO 14471 --- [           main] io.github.devatherock.Application        : Started Application in 1.799 seconds (JVM running for 2.045)
    2022-08-13 07:08:17.510  INFO 14471 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
    2022-08-13 07:08:17.510  INFO 14471 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
    2022-08-13 07:08:17.512  INFO 14471 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
    

    If what you mean is that the app’s actuator health check is failing when mongodb is down, then the MongoHealthIndicator can be disabled by setting the below config:

    management:
      health:
        mongo:
          enabled: false
    

    The test application can be found on github

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search