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

问题描述:

Spring 批处理有自己的数据库架构.
我的应用程序有它自己的数据库架构.

Spring batch had it's own database schema.
My application has it's own database schema.

我想将它们分开到不同的数据库中,这样 spring-batch 表就不在我的应用程序数据库中.

I want to keep these separated into different databases so the spring-batch tables are not inside my applications database.

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

By default spring-boot only supports connection to a single database. How do I configure it so that all spring-batch related operations go into the spring-batch database and all my own code goes into my applications database?

我使用的是最新的 spring-boot 1.2.2.

I am using the latest spring-boot 1.2.2.

嗯,我就是这样做的.

在 application.properties 中

In 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

And in your @Configuration class add the following beans

@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();
}