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

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.


You need to add

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

in order to make it works


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