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

前端 未结 1 2117
终归单人心
终归单人心 2021-02-10 11:01

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-bat

1条回答
  •  一生所求
    2021-02-10 11:34

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

    0 讨论(0)
提交回复
热议问题