首页 > 解决方案 > 尽管数据库关闭,但启动 SpringBoot 服务器

问题描述

我有这个连接到 PostgreSQL 数据库的 SpringBoot 应用程序。即使数据库已关闭,我也想启动服务器,但continue-on-error=true不起作用。当我模拟数据库关闭(密码错误)时,我得到了Error creating bean with name 'countryRepository'错误,即使存储库是延迟加载的。

我认为这是因为默认的@EnableJpaRepositories。有谁知道如何懒惰地初始化它?

通过添加 EntityManagerFactory、EntityManager、TransactionManager、TransactionTemplate 的自定义、延迟初始化的实现来解决

应用程序属性

# Database Properties
spring.datasource.url=jdbc:postgresql://localhost:5432/world-db
spring.datasource.username=world
spring.datasource.password=world123F
spring.datasource.hikari.initializationFailTimeout=0

# Not working... 
spring.datasource.continue-on-error=true
spring.datasource.initialization-mode=never
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL92Dialect
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.hikari.minimum-idle=0
spring.datasource.hikari.initialization-fail-timeout=-1

# Hibernate Properties
spring.jpa.open-in-view=false
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL92Dialect
spring.jpa.hibernate.ddl-auto=validate
spring.h2.console.enabled=true

AppConfig.java

@Lazy
@Configuration
@ComponentScan(basePackages = "com.flairstech.workshop")
public class AppConfig {

    @Bean
    public CountryLanguage getCountryLanguage(){
        return new CountryLanguage();
    }

    @Bean
    public Country getCountry(){
        return new Country();
    }
}

标签: javaspringspring-boothibernatejpa

解决方案


推荐阅读