Skip to content

Commit 76a6550

Browse files
committed
Make transaction manager optional in Tasklet and Chunk-Oriented steps
Before this commit, it was required to provide a transaction manager in tasklet and chunk-oriented steps even if transactions are not needed. This commit makes that optional by using a no-op transaction manager by default. Resolves #4974
1 parent 724874b commit 76a6550

File tree

5 files changed

+40
-10
lines changed

5 files changed

+40
-10
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/StepBuilder.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ public TaskletStepBuilder tasklet(Tasklet tasklet, PlatformTransactionManager tr
6565
return new TaskletStepBuilder(this).tasklet(tasklet, transactionManager);
6666
}
6767

68+
/**
69+
* Build a step with a custom tasklet, not necessarily item processing.
70+
* @param tasklet a tasklet
71+
* @return a {@link TaskletStepBuilder}
72+
* @since 6.0
73+
*/
74+
public TaskletStepBuilder tasklet(Tasklet tasklet) {
75+
return new TaskletStepBuilder(this).tasklet(tasklet);
76+
}
77+
6878
/**
6979
* Build a step that processes items in chunks with the size provided. To extend the
7080
* step to being fault tolerant, call the {@link SimpleStepBuilder#faultTolerant()}

spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/TaskletStepBuilder.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ public TaskletStepBuilder tasklet(Tasklet tasklet, PlatformTransactionManager tr
4949
return this;
5050
}
5151

52+
/**
53+
* @param tasklet the tasklet to use
54+
* @return this for fluent chaining
55+
* @since 6.0
56+
*/
57+
public TaskletStepBuilder tasklet(Tasklet tasklet) {
58+
this.tasklet = tasklet;
59+
return this;
60+
}
61+
5262
@Override
5363
protected TaskletStepBuilder self() {
5464
return this;

spring-batch-core/src/main/java/org/springframework/batch/core/step/item/ChunkOrientedStep.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,11 @@ public class ChunkOrientedStep<I, O> extends AbstractStep {
111111
/*
112112
* Transaction related parameters
113113
*/
114-
private PlatformTransactionManager transactionManager = new ResourcelessTransactionManager();
114+
private PlatformTransactionManager transactionManager;
115115

116116
private TransactionTemplate transactionTemplate;
117117

118-
private TransactionAttribute transactionAttribute = new DefaultTransactionAttribute();
118+
private TransactionAttribute transactionAttribute;
119119

120120
/*
121121
* Chunk related parameters
@@ -309,7 +309,14 @@ public void registerSkipListener(SkipListener<I, O> skipListener) {
309309
@Override
310310
public void afterPropertiesSet() throws Exception {
311311
super.afterPropertiesSet();
312-
Assert.notNull(this.transactionManager, "Transaction manager must not be null");
312+
if (this.transactionManager == null) {
313+
logger.info("No transaction manager has been set. Defaulting to ResourcelessTransactionManager.");
314+
this.transactionManager = new ResourcelessTransactionManager();
315+
}
316+
if (this.transactionAttribute == null) {
317+
logger.info("No transaction attribute has been set. Defaulting to DefaultTransactionAttribute.");
318+
this.transactionAttribute = new DefaultTransactionAttribute();
319+
}
313320
Assert.isTrue(this.chunkSize > 0, "Chunk size must be greater than 0");
314321
Assert.notNull(this.itemReader, "Item reader must not be null");
315322
Assert.notNull(this.itemWriter, "Item writer must not be null");

spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/TaskletStep.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.springframework.batch.repeat.RepeatOperations;
4242
import org.springframework.batch.repeat.RepeatStatus;
4343
import org.springframework.batch.repeat.support.RepeatTemplate;
44+
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
4445
import org.springframework.transaction.PlatformTransactionManager;
4546
import org.springframework.transaction.TransactionStatus;
4647
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
@@ -49,7 +50,6 @@
4950
import org.springframework.transaction.support.TransactionSynchronization;
5051
import org.springframework.transaction.support.TransactionSynchronizationManager;
5152
import org.springframework.transaction.support.TransactionTemplate;
52-
import org.springframework.util.Assert;
5353

5454
import java.util.concurrent.Semaphore;
5555

@@ -118,7 +118,10 @@ public TaskletStep(String name) {
118118
@Override
119119
public void afterPropertiesSet() throws Exception {
120120
super.afterPropertiesSet();
121-
Assert.state(transactionManager != null, "A transaction manager must be provided");
121+
if (this.transactionManager == null) {
122+
logger.info("No transaction manager has been set. Defaulting to ResourcelessTransactionManager.");
123+
this.transactionManager = new ResourcelessTransactionManager();
124+
}
122125
}
123126

124127
/**

spring-batch-samples/src/main/java/org/springframework/batch/samples/helloworld/HelloWorldJobConfiguration.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 the original author or authors.
2+
* Copyright 2023-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,9 +15,10 @@
1515
*/
1616
package org.springframework.batch.samples.helloworld;
1717

18+
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
19+
import org.springframework.batch.core.configuration.annotation.EnableJdbcJobRepository;
1820
import org.springframework.batch.core.job.Job;
1921
import org.springframework.batch.core.step.Step;
20-
import org.springframework.batch.core.configuration.annotation.*;
2122
import org.springframework.batch.core.job.builder.JobBuilder;
2223
import org.springframework.batch.core.repository.JobRepository;
2324
import org.springframework.batch.core.step.builder.StepBuilder;
@@ -26,7 +27,6 @@
2627
import org.springframework.context.annotation.Bean;
2728
import org.springframework.context.annotation.Configuration;
2829
import org.springframework.context.annotation.Import;
29-
import org.springframework.jdbc.support.JdbcTransactionManager;
3030

3131
@Configuration
3232
@EnableBatchProcessing
@@ -35,11 +35,11 @@
3535
public class HelloWorldJobConfiguration {
3636

3737
@Bean
38-
public Step step(JobRepository jobRepository, JdbcTransactionManager transactionManager) {
38+
public Step step(JobRepository jobRepository) {
3939
return new StepBuilder("step", jobRepository).tasklet((contribution, chunkContext) -> {
4040
System.out.println("Hello world!");
4141
return RepeatStatus.FINISHED;
42-
}, transactionManager).build();
42+
}).build();
4343
}
4444

4545
@Bean

0 commit comments

Comments
 (0)