我在 Spring Boot 和 Spring Data 中使用两个数据源。
有一个选项可以通过属性文件 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;
}
最佳答案
我相信你想要spring.datasource.primary.continueOnError=true
关于java - 带有 spring data 的多个数据源 - 需要设置属性 continueOnError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45736084/