spring-bootspring-batchbatch-processingdata-migration

Spring batch using two data Sources


I'm working on a Spring Batch project where I need to use two different data sources. I'm planning to use RepositoryItemReader and RepositoryItemWriter for reading and writing to these databases. However, I couldn't find any documentation on how to configure Spring Batch for this specific scenario.

Additionally, when I added JPA configuration, Spring Batch seems to default to using MySQL for its internal operations. How can I configure Spring Batch to work with two data sources and prevent this default behavior when using JPA?


Solution

  • If you use @EnableBatchProcessing, you can configure the datasource you want for the JobRepository by setting the dataSourceRef attribute. For example:

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

    The equivalent to that with the programmatic approach is by overriding DefaultBatchConfiguration#getDataSource().

    The same datasource can then be set on the item reader and writer.