spring-boot - 使用 spring-boot 连接到 spring-batch 和应用程序数据库

标签 spring-boot spring-batch

Spring Batch 有它自己的数据库模式。
我的应用程序有它自己的数据库架构。

我想将它们分开到不同的数据库中,以便 spring-batch 表不在我的应用程序数据库中。

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

我正在使用最新的 spring-boot 1.2.2。

最佳答案

嗯,这就是我做到的。

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

关于spring-boot - 使用 spring-boot 连接到 spring-batch 和应用程序数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29289777/

相关文章:

java - Spring Boot 1.5.x 未写入logging.file处的文件

spring-boot - 为什么 thymeleaf 消息显示不正确?

spring - 使用 Spring 集成设计电子邮件每日摘要功能

java - Spring Batch StepScope Bean

multithreading - Spring Batch 线程安全的 Map 作业存储库

java - 如何在 Spring Boot 中发送并确认消息已传递到 RabbitMQ?

spring-boot-maven-plugin spring-boot.run.main-class 被忽略(Groovy)

java - Docker 容器化单体应用程序

mysql - 将 Spring Batch MyISAM 序列表迁移到 InnoDB

java - @Value注解区分类和bean