Skip to content
Advertisement

How to make Spring server to start even if database is down?

I’m using a Spring Boot(1.4.7) & MyBatis.

spring.main1.datasource.url=jdbc:mariadb://192.168.0.11:3306/testdb?useUnicode=true&characterEncoding=utf8&autoReconnect=true&socketTimeout=5000&connectTimeout=3000
spring.main1.datasource.username=username
spring.main1.datasource.password=password
spring.main1.datasource.driverClassName=org.mariadb.jdbc.Driver
spring.main1.datasource.tomcat.test-on-borrow=true
spring.main1.datasource.tomcat.test-while-idle=true
spring.main1.datasource.tomcat.validation-query=SELECT 1
spring.main1.datasource.tomcat.validation-query-timeout=5000
spring.main1.datasource.tomcat.validation-interval=5000
spring.main1.datasource.tomcat.max-wait=5000
spring.main1.datasource.continue-on-error=true

I cannot start program with errors when database is disconnected on Eclipse or Linux server. (Database is not located on localhost.)

When I try to start program with disconnected database, print this.

java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=192.168.0.11)(port=3306)(type=master) : connect timed out
Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=192.168.0.11)(port=3306)(type=master) : connect timed out
Stopping service [Tomcat]
Application startup failed

Is there any way?

Thanks

Advertisement

Answer

You can set:

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

in your application.properties.

According to the Spring Boot 2.5.5 user guide:

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.

P.S.: Before Spring Boot 2.5, the property was named spring.datasource.continue-on-error.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement