具有spring数据的多个数据源 - 需要设置属性continueOnError

时间:2017-08-17 13:12:42

标签: java spring spring-boot spring-data-jpa datasource

我在spring boot中使用两个数据源和spring数据。 可以通过属性文件spring.datasource.continueOnError=true

设置属性

我的要求是只为一个数据源设置此属性,如果另一个数据源已关闭,我需要关闭应用程序。 如何将此属性设置为我需要跳过应用程序的数据源?#
请找到我的数据源配置

@Bean
public LocalContainerEntityManagerFactoryBean webNotifyEntityManager() {
    final LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean =
            new LocalContainerEntityManagerFactoryBean();
    localContainerEntityManagerFactoryBean.setDataSource(webNotifyDataSource());
    localContainerEntityManagerFactoryBean.setPackagesToScan(Constants.WEBNOTIFY_REPOSITORIES);
    final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    localContainerEntityManagerFactoryBean.setJpaVendorAdapter(vendorAdapter);
    final HashMap<String, Object> properties = new HashMap<>();
    properties.put("hibernate.dialect", hibernateDialect);
    properties.put("hibernate.show_sql", true);
    localContainerEntityManagerFactoryBean.setJpaPropertyMap(properties);
    return localContainerEntityManagerFactoryBean;
}

@Primary
@Bean
public DataSource webNotifyDataSource() {
    final DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(Preconditions.checkNotNull(driverClassName));
    dataSource.setUrl(Preconditions.checkNotNull(webNotifyUrl));
    dataSource.setUsername(Preconditions.checkNotNull(webNotifyUserName));
    dataSource.setPassword(Preconditions.checkNotNull(webNotifyPassword));
    return dataSource;
}

@Primary
@Bean
public PlatformTransactionManager webNotifyTransactionManager() {
    final JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(webNotifyEntityManager().getObject());
    return transactionManager;
}

1 个答案:

答案 0 :(得分:1)

我相信你想要spring.datasource.primary.continueOnError=true