Apr 8, 2025
Apr 11, 2025
N/A Views
MD

Warning

This article was automatically translated by OpenAI (gemini-2.5-pro-exp-03-25).It may be edited eventually, but please be aware that it may contain incorrect information at this time.

The method for separating the database used within a Job and the database for managing the Job in Spring Batch is described in the Spring Boot documentation, but since there isn't an implementation example, I'm making a note of it here.

Verified with Spring Boot 3.4, Spring Batch 5.2.

You just need to prepare a Config class like the following.

package com.example.batch;

import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.batch.BatchDataSource;
import org.springframework.boot.autoconfigure.batch.BatchTransactionManager;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(name = "spring.batch.datasource.url")
public class DataSourceConfig {

    @Bean
    @ConfigurationProperties("spring.datasource")
    public DataSourceProperties defaultDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @ConfigurationProperties("spring.datasource.hikari")
    public HikariDataSource defaultDataSource(
            @Qualifier("defaultDataSourceProperties") DataSourceProperties properties) {
        HikariDataSource dataSource = properties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
        dataSource.setPoolName("default-pool");
        return dataSource;
    }

    @Bean
    @ConfigurationProperties("spring.batch.datasource")
    public DataSourceProperties batchDataSourceProperties() {
        return new DataSourceProperties();
    }

    @BatchDataSource
    @Bean(defaultCandidate = false)
    @ConfigurationProperties("spring.batch.datasource.hikari")
    public HikariDataSource batchDataSource(@Qualifier("batchDataSourceProperties") DataSourceProperties properties) {
        HikariDataSource dataSource = properties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
        dataSource.setPoolName("batch-pool");
        return dataSource;
    }

    @BatchTransactionManager
    @Bean(defaultCandidate = false)
    public PlatformTransactionManager batchTransactionManager(@BatchDataSource DataSource dataSource) {
        return new JdbcTransactionManager(dataSource);
    }

}

By adding @Bean(defaultCandidate = false), these beans will not be candidates for injection unless explicitly specified with a @Qualifier (including @BatchDataSource and @BatchTransactionManager).
In Spring Boot's Auto Configuration, when creating JobRepository and JobExplorer, if bean definitions annotated with @BatchDataSource or @BatchTransactionManager exist, those will be used.

Here is an example of property settings:

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/mydatabase
spring.datasource.username=myuser
spring.datasource.password=secret
spring.batch.datasource.driver-class-name=org.postgresql.Driver
spring.batch.datasource.url=jdbc:postgresql://localhost:15432/spring_batch
spring.batch.datasource.username=spring_batch
spring.batch.datasource.password=admin

Set the database used within the Job under spring.datasource.*. I've made it use the same properties as the standard database configuration.
Set the database for managing the Job under spring.batch.datasource.*.
Because of @ConditionalOnProperty(name = "spring.batch.datasource.url"), the above Config is only enabled when the database for job management is explicitly configured using spring.batch.datasource.url.
If spring.batch.datasource.url is not specified, the default Auto Configuration remains active.

In this example, PostgreSQL is used for both the database used within the Job and the database for managing the Job, but you can use different databases.
For example, with the following definition, you can use an in-memory H2 database for the job management database.

spring.batch.datasource.url=jdbc:h2:mem:spring_batch
spring.batch.datasource.driverClassName=org.h2.Driver
spring.batch.datasource.username=sa
spring.batch.datasource.password=
Found a mistake? Update the entry.
Share this article: