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
There is an option to ignore the initializing of
datasource
when your application fails to start by setting this property: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:
Also if you want to handle when your database is down or up, you can create a
bean
fordatasource
and handle any exception which mean your database is down. In order to do this, create a class@Configuration
which hasdatasource
asbean
like this: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:
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:The test application can be found on github