java—尝试使用多个数据库启动spring boot,即使其中一个数据库没有连接

e5nqia27  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(214)

我有一个spring-boot应用程序,它使用多个数据库而没有问题。如果我尝试在某个数据库关闭的情况下启动应用程序,它将无法启动。
如果其中一个数据库坏了,我需要启动应用程序。
我正在使用SpringBoot“2.3.2.release”。
如果无法建立连接,我需要忽略整个bean。有人有什么想法或线索吗?
db1关闭时出现的错误是

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'db1EntityManager' 
    defined in class path resource [com/example/persistence/config/DB1Config.class]: 
    Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException:
    Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service
    [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when
    'hibernate.dialect' not set

数据库配置

@Configuration
@ConditionalOnProperty(
        value = "datasource.db1.enabled",
        havingValue = "true"
)
@EnableTransactionManagement
@EnableJpaRepositories(
        basePackages = "com.example.persistence.databases.db1",
        entityManagerFactoryRef = "db1EntityManager",
        transactionManagerRef = "db1TransactionManager"
)
public class DB1Config extends DBConfig {

    private Environment env;

    @Value("${datasource.db1.username}")
    private String username;

    @Value("${datasource.db1.password}")
    private String password;

    @Value("${datasource.db1.url}")
    private String url;

    public DB1Config(Environment env) {
        this.env = env;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean db1EntityManager() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(db1DataSource());
        em.setPackagesToScan("com.example.persistence.databases.db1.model");
        populateEntityManager(em, env);
        return em;
    }

    @Bean
    public DataSource db1DataSource() {
        return createDataSource(url, env.getProperty(JDBC_DRIVER_CLASS_NAME), username, password);
    }

    @Bean
    public PlatformTransactionManager db1TransactionManager() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(db1EntityManager().getObject());
        return transactionManager;
    }
}

数据库配置

@Configuration
@ConditionalOnProperty(
        value = "datasource.db2.enabled",
        havingValue = "true"
)
@EnableTransactionManagement
@EnableJpaRepositories(
        basePackages = "com.example.persistence.databases.db2",
        entityManagerFactoryRef = "db2EntityManager",
        transactionManagerRef = "db2TransactionManager"
)
public class DB2Config extends DBConfig {

    private Environment env;

    @Value("${datasource.db2.username}")
    private String username;

    @Value("${datasource.db2.password}")
    private String password;

    @Value("${datasource.db2.url}")
    private String url;

    public DB2Config(Environment env) {
        this.env = env;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean db2EntityManager() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(db2DataSource());
        em.setPackagesToScan("com.example.persistence.databases.db2.model");
        populateEntityManager(em, env);
        return em;
    }

    @Bean
    public DataSource db2DataSource() {
        return createDataSource(url, env.getProperty(JDBC_DRIVER_CLASS_NAME), username, password);
    }

    @Bean
    public PlatformTransactionManager db2TransactionManager() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(db2EntityManager().getObject());
        return transactionManager;
    }
}

数据库配置

public abstract class DBConfig {
    protected static final String JDBC_DRIVER_CLASS_NAME = "jdbc.driverClassName";

    protected void populateEntityManager(LocalContainerEntityManagerFactoryBean em, Environment env) {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        HashMap<String, Object> properties = new HashMap<>();
        properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
        properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
        em.setJpaPropertyMap(properties);
    }

    protected DataSource createDataSource(String url, String driver, String username, String password) {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}

应用程序属性

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.datasource.continue-on-error=true
spring.jpa.hibernate.ddl-auto=validate
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver

datasource.db1.enabled=true
datasource.db1.url=jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=host1)(PORT=1648))(CONNECT_DATA=(SERVICE_NAME=service1)(SERVER=DEDICATED)))
datasource.db1.username=user1
datasource.db1.password=password1

datasource.db2.enabled=true
datasource.db2.url=jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=host2)(PORT=1648))(CONNECT_DATA=(SERVICE_NAME=service2)(SERVER=DEDICATED)))
datasource.db2.username=user2
datasource.db2.password=password2

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题