使用两个数据源的 Spring 批处理

问题描述 投票:0回答:1

我正在开发一个 Spring Batch 项目,我需要使用两个不同的数据源。我计划使用 RepositoryItemReaderRepositoryItemWriter 来读取和写入这些数据库。但是,我找不到任何有关如何针对此特定场景配置 Spring Batch 的文档。

另外,当我添加JPA配置时,Spring Batch似乎默认使用MySQL进行内部操作。如何配置 Spring Batch 以使用两个数据源并在使用 JPA 时防止这种默认行为?

spring-boot spring-batch batch-processing data-migration
1个回答
0
投票

如果您使用

@EnableBatchProcessing
,您可以通过设置
JobRepository
属性为
dataSourceRef
配置所需的数据源。例如:

@Configuration
@EnableBatchProcessing(dataSourceRef = "batchDataSource", transactionManagerRef = "jpaTransactionManager")
public static class JobConfiguration {

}

与编程方法等效的是覆盖

DefaultBatchConfiguration#getDataSource()

然后可以在项目读取器和写入器上设置相同的数据源。

© www.soinside.com 2019 - 2024. All rights reserved.