Move the transaction manager configuration to AbstractTaskletStepBuilder

Before this commit, the transaction manager was configurable
at the StepBuilder level, which is inconsistent with the XML
config style in addition to be not needed for most step types.

This commit moves the configuration of the transaction manager
from the StepBuilder down to the AbstractTaskletStepBuilder,
which is the level where the transaction manager is needed.

Resolves #4130
This commit is contained in:
Mahmoud Ben Hassine
2022-09-02 10:35:02 +02:00
parent 55af86df59
commit 7c8fb172a7
46 changed files with 409 additions and 195 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@ import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.jdbc.support.JdbcTransactionManager;
/**
* @author Dave Syer
@@ -62,4 +63,9 @@ public class DataSourceConfiguration {
return dataSource;
}
@Bean
public JdbcTransactionManager transactionManager(DataSource dataSource) {
return new JdbcTransactionManager(dataSource);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@ import org.springframework.batch.sample.support.RetrySampleItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
/**
* @author Dave Syer
@@ -44,6 +45,9 @@ public class RetrySampleConfiguration {
@Autowired
private StepBuilderFactory steps;
@Autowired
private PlatformTransactionManager transactionManager;
@Bean
public Job retrySample() {
return jobs.get("retrySample").start(step()).build();
@@ -51,8 +55,8 @@ public class RetrySampleConfiguration {
@Bean
protected Step step() {
return steps.get("step").<Trade, Object>chunk(1).reader(reader()).writer(writer()).faultTolerant()
.retry(Exception.class).retryLimit(3).build();
return steps.get("step").<Trade, Object>chunk(1).transactionManager(this.transactionManager).reader(reader())
.writer(writer()).faultTolerant().retry(Exception.class).retryLimit(3).build();
}
@Bean

View File

@@ -33,6 +33,7 @@ import org.springframework.context.annotation.Import;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.jms.dsl.Jms;
import org.springframework.transaction.PlatformTransactionManager;
/**
* This configuration class is for the worker side of the remote partitioning sample. Each
@@ -84,9 +85,9 @@ public class WorkerConfiguration {
* Configure the worker step
*/
@Bean
public Step workerStep() {
public Step workerStep(PlatformTransactionManager transactionManager) {
return this.workerStepBuilderFactory.get("workerStep").inputChannel(requests()).outputChannel(replies())
.tasklet(tasklet(null)).build();
.tasklet(tasklet(null)).transactionManager(transactionManager).build();
}
@Bean

View File

@@ -33,6 +33,7 @@ import org.springframework.context.annotation.Import;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.jms.dsl.Jms;
import org.springframework.transaction.PlatformTransactionManager;
/**
* This configuration class is for the worker side of the remote partitioning sample. Each
@@ -70,8 +71,9 @@ public class WorkerConfiguration {
* Configure the worker step
*/
@Bean
public Step workerStep() {
return this.workerStepBuilderFactory.get("workerStep").inputChannel(requests()).tasklet(tasklet(null)).build();
public Step workerStep(PlatformTransactionManager transactionManager) {
return this.workerStepBuilderFactory.get("workerStep").inputChannel(requests()).tasklet(tasklet(null))
.transactionManager(transactionManager).build();
}
@Bean

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2021 the original author or authors.
* Copyright 2021-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.support.JdbcTransactionManager;
@Configuration
public class DataSourceConfiguration {
@@ -30,4 +31,9 @@ public class DataSourceConfiguration {
.addScript("/org/springframework/batch/core/schema-hsqldb.sql").generateUniqueName(true).build();
}
@Bean
public JdbcTransactionManager transactionManager(DataSource dataSource) {
return new JdbcTransactionManager(dataSource);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 the original author or authors.
* Copyright 2019-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@ import org.springframework.batch.item.support.ListItemReader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.transaction.PlatformTransactionManager;
/**
* @author Mahmoud Ben Hassine
@@ -43,10 +44,13 @@ public class SkippableExceptionDuringProcessSample {
private final StepBuilderFactory stepBuilderFactory;
private final PlatformTransactionManager transactionManager;
public SkippableExceptionDuringProcessSample(JobBuilderFactory jobBuilderFactory,
StepBuilderFactory stepBuilderFactory) {
StepBuilderFactory stepBuilderFactory, PlatformTransactionManager transactionManager) {
this.jobBuilderFactory = jobBuilderFactory;
this.stepBuilderFactory = stepBuilderFactory;
this.transactionManager = transactionManager;
}
@Bean
@@ -85,9 +89,9 @@ public class SkippableExceptionDuringProcessSample {
@Bean
public Step step() {
return this.stepBuilderFactory.get("step").<Integer, Integer>chunk(3).reader(itemReader())
.processor(itemProcessor()).writer(itemWriter()).faultTolerant().skip(IllegalArgumentException.class)
.skipLimit(3).build();
return this.stepBuilderFactory.get("step").<Integer, Integer>chunk(3)
.transactionManager(this.transactionManager).reader(itemReader()).processor(itemProcessor())
.writer(itemWriter()).faultTolerant().skip(IllegalArgumentException.class).skipLimit(3).build();
}
@Bean

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 the original author or authors.
* Copyright 2019-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@ import org.springframework.batch.item.support.ListItemReader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.transaction.PlatformTransactionManager;
/**
* @author Mahmoud Ben Hassine
@@ -43,10 +44,13 @@ public class SkippableExceptionDuringReadSample {
private final StepBuilderFactory stepBuilderFactory;
private final PlatformTransactionManager transactionManager;
public SkippableExceptionDuringReadSample(JobBuilderFactory jobBuilderFactory,
StepBuilderFactory stepBuilderFactory) {
StepBuilderFactory stepBuilderFactory, PlatformTransactionManager transactionManager) {
this.jobBuilderFactory = jobBuilderFactory;
this.stepBuilderFactory = stepBuilderFactory;
this.transactionManager = transactionManager;
}
@Bean
@@ -85,9 +89,9 @@ public class SkippableExceptionDuringReadSample {
@Bean
public Step step() {
return this.stepBuilderFactory.get("step").<Integer, Integer>chunk(3).reader(itemReader())
.processor(itemProcessor()).writer(itemWriter()).faultTolerant().skip(IllegalArgumentException.class)
.skipLimit(3).build();
return this.stepBuilderFactory.get("step").<Integer, Integer>chunk(3)
.transactionManager(this.transactionManager).reader(itemReader()).processor(itemProcessor())
.writer(itemWriter()).faultTolerant().skip(IllegalArgumentException.class).skipLimit(3).build();
}
@Bean

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 the original author or authors.
* Copyright 2019-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@ import org.springframework.batch.item.support.ListItemReader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.transaction.PlatformTransactionManager;
/**
* @author Mahmoud Ben Hassine
@@ -43,10 +44,13 @@ public class SkippableExceptionDuringWriteSample {
private final StepBuilderFactory stepBuilderFactory;
private final PlatformTransactionManager transactionManager;
public SkippableExceptionDuringWriteSample(JobBuilderFactory jobBuilderFactory,
StepBuilderFactory stepBuilderFactory) {
StepBuilderFactory stepBuilderFactory, PlatformTransactionManager transactionManager) {
this.jobBuilderFactory = jobBuilderFactory;
this.stepBuilderFactory = stepBuilderFactory;
this.transactionManager = transactionManager;
}
@Bean
@@ -85,9 +89,9 @@ public class SkippableExceptionDuringWriteSample {
@Bean
public Step step() {
return this.stepBuilderFactory.get("step").<Integer, Integer>chunk(3).reader(itemReader())
.processor(itemProcessor()).writer(itemWriter()).faultTolerant().skip(IllegalArgumentException.class)
.skipLimit(3).build();
return this.stepBuilderFactory.get("step").<Integer, Integer>chunk(3)
.transactionManager(this.transactionManager).reader(itemReader()).processor(itemProcessor())
.writer(itemWriter()).faultTolerant().skip(IllegalArgumentException.class).skipLimit(3).build();
}
@Bean

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018-2021 the original author or authors.
* Copyright 2018-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.support.JdbcTransactionManager;
/**
* @author Mahmoud Ben Hassine
@@ -70,8 +71,8 @@ public class ValidationSampleConfiguration {
@Bean
public Step step() throws Exception {
return this.steps.get("step").<Person, Person>chunk(1).reader(itemReader()).processor(itemValidator())
.writer(itemWriter()).build();
return this.steps.get("step").<Person, Person>chunk(1).transactionManager(transactionManager(dataSource()))
.reader(itemReader()).processor(itemValidator()).writer(itemWriter()).build();
}
@Bean
@@ -85,4 +86,9 @@ public class ValidationSampleConfiguration {
.addScript("/org/springframework/batch/core/schema-hsqldb.sql").generateUniqueName(true).build();
}
@Bean
public JdbcTransactionManager transactionManager(DataSource dataSource) {
return new JdbcTransactionManager(dataSource);
}
}

View File

@@ -49,6 +49,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.util.DigestUtils;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -94,7 +95,8 @@ class JsonSupportIntegrationTests {
@Bean
public Step step() {
return steps.get("step").<Trade, Trade>chunk(2).reader(itemReader()).writer(itemWriter()).build();
return steps.get("step").<Trade, Trade>chunk(2).transactionManager(transactionManager(dataSource()))
.reader(itemReader()).writer(itemWriter()).build();
}
@Bean
@@ -108,6 +110,11 @@ class JsonSupportIntegrationTests {
.addScript("/org/springframework/batch/core/schema-hsqldb.sql").generateUniqueName(true).build();
}
@Bean
public JdbcTransactionManager transactionManager(DataSource dataSource) {
return new JdbcTransactionManager(dataSource);
}
}
@Test