【问题标题】:Connecting to both spring-batch and application database using spring-boot使用 spring-boot 连接到 spring-batch 和应用程序数据库
【发布时间】:2015-05-31 03:17:51
【问题描述】:

Spring 批处理拥有自己的数据库架构。
我的应用程序有它自己的数据库模式。

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

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

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

【问题讨论】:

    标签: spring-boot spring-batch


    【解决方案1】:

    这就是我的做法。

    在 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

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

    【讨论】: