Spring Batch 有它自己的数据库模式。
我的应用程序有它自己的数据库架构。
我想将它们分开到不同的数据库中,以便 spring-batch 表不在我的应用程序数据库中。
默认情况下,spring-boot 仅支持连接到单个数据库。如何配置它以便所有与 spring-batch 相关的操作都进入 spring-batch 数据库,而我自己的所有代码都进入我的应用程序数据库?
我正在使用最新的 spring-boot 1.2.2。
最佳答案
嗯,这就是我做到的。
在 application.properties 中
### Database Details
datasource.app.driverClassName=oracle.jdbc.driver.OracleDriver
datasource.app.url=jdbc:oracle:thin:@//localhost:1521/xe
datasource.app.username=YOUR_APP_DB_USERNAME
datasource.app.password=YOUR_PASSWORD
datasource.batch.driverClassName=oracle.jdbc.driver.OracleDriver
datasource.batch.url=jdbc:oracle:thin:@//localhost:1521/xe
datasource.batch.username=YOUR_BATCH_DB_USERNAME
datasource.batch.password=YOUR_PASSWORD
而在您的
@Configuration
类添加以下bean@Primary
@Bean
@ConfigurationProperties(prefix = "datasource.app")
public DataSource appDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix = "datasource.batch")
public DataSource batchDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public JobLauncher jobLauncher() throws Exception {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository());
return jobLauncher;
}
@Bean
public JobRepository jobRepository() throws Exception {
DataSourceTransactionManager batchTransactionManager = new DataSourceTransactionManager();
batchTransactionManager.setDataSource(batchDataSource());
JobRepositoryFactoryBean jobRepositoryFactoryBean = new JobRepositoryFactoryBean();
jobRepositoryFactoryBean.setTransactionManager(batchTransactionManager);
jobRepositoryFactoryBean.setDatabaseType("ORACLE");
jobRepositoryFactoryBean.setIsolationLevelForCreate("ISOLATION_DEFAULT");
jobRepositoryFactoryBean.setDataSource(batchDataSource());
jobRepositoryFactoryBean.afterPropertiesSet();
return jobRepositoryFactoryBean.getObject();
}
关于spring-boot - 使用 spring-boot 连接到 spring-batch 和应用程序数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29289777/