Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 912
Cs lee Avatar asked Jul 31 '17 06:07

Cs lee


People also ask

How do you handle database in spring boot?

To access the Relational Database by using JdbcTemplate in Spring Boot application, we need to add the Spring Boot Starter JDBC dependency in our build configuration file. Then, if you @Autowired the JdbcTemplate class, Spring Boot automatically connects the Database and sets the Datasource for the JdbcTemplate object.

How do I know if spring boot is connected to database?

The easiest way to test the database connection from Spring boot is to start the application and by checking to debug logs.

Can spring boot disable default web server?

Another way to disable the embedded web server in Spring Boot is by using code. We can use either the SpringApplicationBuilder: new SpringApplicationBuilder(MainApplication. class) .


1 Answers

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.

like image 169
Ortomala Lokni Avatar answered Oct 05 '22 12:10

Ortomala Lokni