I am currently working on a Spring Batch 5 application and I would like to remove the H2 database dependency because the project does not involve or require persisting the metadata of the jobs. My project setup includes the following:
Spring Boot: 3.4.2
Spring Batch: 5.2.0
By default, Spring Batch includes H2 as an in-memory database for job repository configuration. However, I want to configure Spring Batch to use ResourcelessJobRepository and ResourcelessTransactionManager.
Despite this configuration, the H2 dependency is still being included and used by default. I would like to know how to completely remove the H2 dependency and ensure that Spring Batch does not persist any metadata in the database.
Could someone provide guidance on how to achieve this? Any help or examples would be greatly appreciated.
The below dependencies are used in my pom.xml
<dependency>
<groupId>.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
I am currently working on a Spring Batch 5 application and I would like to remove the H2 database dependency because the project does not involve or require persisting the metadata of the jobs. My project setup includes the following:
Spring Boot: 3.4.2
Spring Batch: 5.2.0
By default, Spring Batch includes H2 as an in-memory database for job repository configuration. However, I want to configure Spring Batch to use ResourcelessJobRepository and ResourcelessTransactionManager.
Despite this configuration, the H2 dependency is still being included and used by default. I would like to know how to completely remove the H2 dependency and ensure that Spring Batch does not persist any metadata in the database.
Could someone provide guidance on how to achieve this? Any help or examples would be greatly appreciated.
The below dependencies are used in my pom.xml
<dependency>
<groupId>.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
Share
edited Mar 4 at 10:06
Alagukannan Shekar
asked Mar 4 at 5:16
Alagukannan ShekarAlagukannan Shekar
111 bronze badge
3
- 3 Please add a build system into the question. Gradle/Mvn? Add a build config file also – Komdosh Commented Mar 4 at 5:22
- maven.apache./guides/introduction/… for maven dependency exclusion – Joop Eggen Commented Mar 4 at 10:19
- You are including H2 yourself... – M. Deinum Commented Mar 4 at 11:57
1 Answer
Reset to default 1By default, Spring Batch includes H2 as an in-memory database for job repository configuration.
Spring Batch does not have a dependency to H2, you are adding it manually (probably because Spring Boot is looking for an embedded database to configure Spring Batch).
But you are right, the default batch configuration in Spring Boot requires a datasource (There is an open issue that we are planning to fix in the next major versions). In the meantime, you can override that by excluding the datasource configuration and configuring Spring Batch yourself. Here is a quick example:
import .springframework.batch.core.Job;
import .springframework.batch.core.JobParameters;
import .springframework.batch.core.Step;
import .springframework.batch.core.job.builder.JobBuilder;
import .springframework.batch.core.launch.JobLauncher;
import .springframework.batch.core.launch.support.TaskExecutorJobLauncher;
import .springframework.batch.core.repository.JobRepository;
import .springframework.batch.core.repository.support.ResourcelessJobRepository;
import .springframework.batch.core.step.builder.StepBuilder;
import .springframework.batch.repeat.RepeatStatus;
import .springframework.batch.support.transaction.ResourcelessTransactionManager;
import .springframework.boot.ApplicationRunner;
import .springframework.boot.SpringApplication;
import .springframework.boot.autoconfigure.SpringBootApplication;
import .springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import .springframework.context.annotation.Bean;
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class SpringBatchResourcelesJobRepositoryApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBatchResourcelesJobRepositoryApplication.class, args);
}
@Bean
public Job job(JobRepository jobRepository) {
Step myStep = new StepBuilder("myStep", jobRepository)
.tasklet((contribution, chunkContext)
-> { System.out.println("Hello world!");
return RepeatStatus.FINISHED;
}, new ResourcelessTransactionManager())
.build();
return new JobBuilder("myJob", jobRepository)
.start(myStep)
.build();
}
@Bean
public JobRepository jobRepository() {
return new ResourcelessJobRepository();
}
@Bean
public JobLauncher jobLauncher(JobRepository jobRepository) {
TaskExecutorJobLauncher jobLauncher = new TaskExecutorJobLauncher();
jobLauncher.setJobRepository(jobRepository);
return jobLauncher;
}
@Bean
public ApplicationRunner batchRunner(Job job, JobLauncher jobLauncher) {
return args -> jobLauncher.run(job, new JobParameters());
}
}
The pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache./POM/4.0.0" xmlns:xsi="http://www.w3./2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache./POM/4.0.0 https://maven.apache./xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-batch-resourceles-job-repository</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-batch-resourceles-job-repository</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
prints:
:: Spring Boot :: (v3.4.3)
2025-03-07T08:02:27.834-05:00 INFO 20900 --- [ main] BatchResourcelesJobRepositoryApplication : Starting SpringBatchResourcelesJobRepositoryApplication using Java 21.0.2 with PID 20900
2025-03-07T08:02:27.835-05:00 INFO 20900 --- [ main] BatchResourcelesJobRepositoryApplication : No active profile set, falling back to 1 default profile: "default"
2025-03-07T08:02:28.091-05:00 INFO 20900 --- [ main] o.s.b.c.l.s.TaskExecutorJobLauncher : No TaskExecutor has been set, defaulting to synchronous executor.
2025-03-07T08:02:28.166-05:00 INFO 20900 --- [ main] BatchResourcelesJobRepositoryApplication : Started SpringBatchResourcelesJobRepositoryApplication in 0.515 seconds (process running for 0.693)
2025-03-07T08:02:28.169-05:00 INFO 20900 --- [ main] o.s.b.c.l.s.TaskExecutorJobLauncher : Job: [SimpleJob: [name=myJob]] launched with the following parameters: [{}]
2025-03-07T08:02:28.172-05:00 INFO 20900 --- [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [myStep]
Hello world!
2025-03-07T08:02:28.175-05:00 INFO 20900 --- [ main] o.s.batch.core.step.AbstractStep : Step: [myStep] executed in 3ms
2025-03-07T08:02:28.177-05:00 INFO 20900 --- [ main] o.s.b.c.l.s.TaskExecutorJobLauncher : Job: [SimpleJob: [name=myJob]] completed with the following parameters: [{}] and the following status: [COMPLETED] in 4ms