使用 spring-boot 连接到 spring-batch 和应用程序数据库

jax*_*jax 5 spring-batch spring-boot

Spring Batch 有它自己的数据库模式。
我的应用程序有它自己的数据库架构。

我想将它们分开到不同的数据库中,以便 spring-batch 表不在我的应用程序数据库中。

默认情况下,spring-boot 仅支持连接到单个数据库。如何配置它以便所有与 spring-batch 相关的操作都进入 spring-batch 数据库,而我自己的所有代码都进入我的应用程序数据库?

我正在使用最新的 spring-boot 1.2.2。

jax*_*jax 4

嗯,我就是这样做的。

在应用程序属性中

### 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 
Run Code Online (Sandbox Code Playgroud)

并在你的@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();
}
Run Code Online (Sandbox Code Playgroud)