How to Configure Multiple DataSources in Spring Batch Without Errors?

Question

How can I resolve the exception related to configuring multiple DataSources in Spring Batch?

@Configuration
@EnableBatchProcessing 
public class BatchJobConfiguration {

    @Primary
    @Bean(name = "baseDatasource")
    public DataSource dataSource() {
         // first datasource definition here
    }
    
    @Bean(name = "secondaryDataSource")
    public DataSource dataSource2() {
         // second datasource definition here
    }
    ...
}

Answer

Configuring multiple DataSources in Spring Batch requires specific steps to avoid conflicts with the default BatchConfigurer. When you declare more than one DataSource, you must customize your batch configuration to avoid runtime exceptions regarding datasource conflicts.

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {

    @Primary
    @Bean(name = "baseDatasource")
    public DataSource dataSource() { /* datasource 1 implementation */ }

    @Bean(name = "secondaryDataSource")
    public DataSource secondaryDataSource() { /* datasource 2 implementation */ }

    @Bean
    public JobRepository jobRepository() throws Exception {
        return new JobRepositoryFactoryBean(dataSource()).getObject();
    }

    @Bean
    public DataSourceTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }
}

Causes

  • Spring Batch's default configuration assumes a single DataSource for job repository and execution context.
  • The exception 'To use the default BatchConfigurer the context must contain no more than one DataSource, found 2' indicates that the application context is detecting more than one DataSource bean.

Solutions

  • Implement a custom BatchConfigurer that can handle multiple DataSources.
  • Ensure that your job repository is explicitly configured to use the correct DataSource.

Common Mistakes

Mistake: Not defining a custom BatchConfigurer when multiple datasources are used.

Solution: Create a class that implements BatchConfigurer and overrides createJobRepository to point to the desired DataSource.

Mistake: Using @Primary on both DataSources.

Solution: Only use @Primary on the DataSource you want to default to, typically the main one used in most batch operations.

Mistake: Assuming XML configuration allows multiple datasources without additional configuration.

Solution: When migrating from XML to Java Config, ensure all necessary configurations are revised accordingly.

Helpers

  • Spring Batch
  • multiple DataSources
  • BatchConfigurer
  • Spring Boot
  • DataSource configuration

Related Questions

⦿How to Efficiently Search a Stream for a String in Java Without Loading Everything into Memory?

Learn how to efficiently search a text stream in Java for a specific string handling buffer boundaries without excessive memory usage.

⦿How to Accurately Identify Lambda Expressions in Java?

Learn effective methods to identify lambda expressions in Java distinguishing them from proxy classes with examples and tips.

⦿Understanding the Interaction of Spring 3 Expression Language with Property Placeholders

Learn how Spring 3 Expression Language SpEL interacts with property placeholders and how to implement default values correctly.

⦿Why Does Indirect Increment Perform Faster Than Direct Increment in Java?

Explore the surprising performance difference between indirect and direct increment operations in Java including benchmark results and insights.

⦿Instance Factory Methods vs Static Factory Methods: Understanding the Differences

Explore the differences between instance factory methods and static factory methods their use cases and see examples to clarify their applications.

⦿How to Manage Sessions and Cookies in Apache JMeter for Scalable Performance Testing

Learn how to effectively manage session and cookie behavior in Apache JMeter to prevent session invalidations when scaling performance tests with multiple threads.

⦿Why Does My Java Version Show as OpenJDK 1.8 Instead of Java 8?

Understand why OpenJDK displays as version 1.8 and not Java 8 and learn more about Java versioning nuances.

⦿How to Mock a Local Variable of a Method in Mockito?

Learn how to use Mockito to mock local variables in methods for effective unit testing of Java classes.

⦿Is Stopwatch Benchmarking a Reliable Method for Java Performance Testing?

Explore the pros and cons of stopwatch benchmarking in Java performance testing. Learn better alternatives for accurate results.

⦿Comparative Analysis of Execution Speed: Java vs. C#

Explore studies comparing execution speed between Java and C and understand factors affecting performance in programming languages.

© Copyright 2025 - CodingTechRoom.com

close