Connecting to both spring-batch and application database using spring-boot

南笙酒味 提交于 2019-12-04 20:24:54

Well this is how I did it.

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 

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();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!