即使数据库已关闭,如何使 Spring 服务器启动?

2022-01-18 00:00:00 java spring-boot spring-data fail-fast

我正在使用 Spring Boot(1.4.7) &MyBatis.

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

当 Eclipse 或 Linux 服务器上的数据库断开连接时,我无法启动程序并出现错误.(数据库不在本地主机上.)

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

有什么办法吗?

谢谢

推荐答案

你可以设置:

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

在您的 application.properties 中.

in your application.properties.

根据 Spring Boot 2.5.5 用户指南:

默认情况下,Spring Boot 启用其基于脚本的数据库初始化程序的快速失败功能.这意味着,如果脚本导致异常,应用程序将无法启动.您可以通过设置 spring.sql.init.continue-on-error 来调整该行为.

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.:在 Spring Boot 2.5 之前,该属性被命名为 spring.datasource.continue-on-error.

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

相关文章