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.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With