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

后端 未结 4 979
余生分开走
余生分开走 2021-02-07 00:49

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&         


        
相关标签:
4条回答
  • 2021-02-07 01:06

    You need to add

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

    in order to make it works

    0 讨论(0)
  • 2021-02-07 01:17

    You can set:

    spring.datasource.continue-on-error=true
    

    in your application.properties.

    According to the Spring Boot 2.2.4 user guide:

    By default, Spring Boot enables the fail-fast feature of the Spring JDBC initializer. This means that, if the scripts cause exceptions, the application fails to start. You can tune that behavior by setting spring.datasource.continue-on-error.

    0 讨论(0)
  • 2021-02-07 01:18

    Check this blog post for a very good solution: Delay startup of your Spring Boot application until your DB is up.

    0 讨论(0)
  • 2021-02-07 01:25

    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
    
    0 讨论(0)
提交回复
热议问题