Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

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?

I am using the latest spring-boot 1.2.2.

like image 471
jax Avatar asked Sep 28 '22 10:09

jax


1 Answers

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();
}
like image 70
jax Avatar answered Oct 05 '22 07:10

jax