43

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

Simeon Leyzerzon
  • 18,658
  • 9
  • 54
  • 82
Cs lee
  • 443
  • 1
  • 4
  • 4
  • This maybe fixed in a later Spring Boot release. According to https://github.com/spring-projects/spring-boot/issues/7589 try upgrading to at least 1.5.2? – Strelok Jul 31 '17 at 07:11
  • Could exception handling prove somewhat useful? – Adrian M. Dec 08 '18 at 19:44

5 Answers5

34

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.

Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
  • 1
    This is not working with 2.1.1.RELEASE or the one older than this – user666 Dec 17 '18 at 09:12
  • According to the Spring Boot 2.1.1 user guide this should work. – Ortomala Lokni Dec 17 '18 at 09:42
  • 1
    I am using these config:spring.datasource.url= spring.datasource.username= spring.datasource.password= spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver spring.datasource.continue-on-error=true – user666 Dec 17 '18 at 09:56
  • in addition to an MSSQLDbConfig class initializing the entityManagerFactory – user666 Dec 17 '18 at 09:57
  • I added both properties: spring.datasource.continue-on-error=true & spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect. But, it fails with the error UnsatisfiedDependencyException for the repository with nested exception IllegalArgumentException: Not a managed type for my class annotated with @Entity. – Saurabhcdt Oct 01 '19 at 07:09
  • Adding `spring.datasource.continue-on-error=true` to `application.yml` file doesn't seem to work for Spring Boot 2.2.2.RELEASE. We are on Oracle DB. – Simeon Leyzerzon Jan 10 '20 at 19:54
  • According to the documentation this seems to be applicable for Flyway intialization, thus if the script fails, it resumes. – Bruno Barin Jun 30 '20 at 17:08
  • Not working anymore I guess –  Sep 30 '21 at 21:14
  • 1
    @cristi_nmr The name of the property is now `spring.sql.init.continue-on-error`. See my updated answer. – Ortomala Lokni Oct 01 '21 at 06:41
  • @OrtomalaLokni oh, thank you for the update –  Oct 01 '21 at 16:22
  • Will it try to connect again when any query is made? – Somnium Sep 14 '22 at 07:41
18

I was able to solve this. One main difference between what I got working and the code in the question, though, is that I'm using Hikari instead of Tomcat for the connection pool.

These were the key settings I had to make:

spring.datasource.hikari.minimum-idle: 0
spring.datasource.hikari.initialization-fail-timeout: -1
spring.datasource.continue-on-error: true
spring.datasource.driver-class-name: org.postgresql.Driver
spring.jpa.database-platform: org.hibernate.dialect.PostgreSQLDialect

Setting minimum-idle to 0 allows Hikari to be happy without any connections.

The initialization-fail-timeout setting of -1 tells Hikari that I don't want it to get a connection when the pool fires up.

From the HikariCP documentation:

A value less than zero will bypass any initial connection attempt, and the pool will start immediately while trying to obtain connections in the background. Consequently, later efforts to obtain a connection may fail.

The continue-on-error setting true allows the service to continue even when encountering an error.

Both the driver-class-name and database-platform were required. Otherwise, Hikari tries to figure out those values by connecting to the database (during startup).


Just in case I'm missing something, though, here's my full Spring config:

spring:
  application:
    name: <redacted>
  datasource:
    url: <redacted>
    username: <redacted>
    password: <redacted>
    driver-class-name: org.postgresql.Driver
    hikari:
      minimum-idle: 0
      maximum-pool-size: 15
      connection-timeout: 10000 #10s
      idle-timeout: 300000 #5m
      max-lifetime: 600000 #10m
      initialization-fail-timeout: -1
      validation-timeout: 1000 #1s
    continue-on-error: true
  jpa:
    open-in-view: false
    database-platform: org.hibernate.dialect.PostgreSQLDialect

And my project has the following Spring Boot dependencies:

org.springframework.boot:spring-boot
org.springframework.boot:spring-boot-actuator
org.springframework.boot:spring-boot-actuator-autoconfigure
org.springframework.boot:spring-boot-autoconfigure
org.springframework.boot:spring-boot-configuration-processor
org.springframework.boot:spring-boot-devtools
org.springframework.boot:spring-boot-starter
org.springframework.boot:spring-boot-starter-actuator
org.springframework.boot:spring-boot-starter-jdbc
org.springframework.boot:spring-boot-starter-jooq
org.springframework.boot:spring-boot-starter-json
org.springframework.boot:spring-boot-starter-logging
org.springframework.boot:spring-boot-starter-security
org.springframework.boot:spring-boot-starter-test
org.springframework.boot:spring-boot-starter-tomcat
org.springframework.boot:spring-boot-starter-validation
org.springframework.boot:spring-boot-starter-web
Danny Wedul
  • 201
  • 2
  • 6
3

You need to add

spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect

in order to make it works

Luca Carducci
  • 173
  • 1
  • 9
0

If the tips above didn't help and you use jpa, then set

spring.jpa.hibernate.ddl-auto=none

it worked for me.

vszholobov
  • 2,133
  • 1
  • 8
  • 23
-4

When you build your app you can add that

mvn clean install -DskipTests

it will skip the tests of database connection

(-D is used to define a system property)

Yehouda
  • 112
  • 1
  • 6