From d11b5b2c3a39aa30d93b0b952f9c1244a9f97565 Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Tue, 13 Sep 2022 20:02:32 +0200 Subject: [PATCH] Remove BatchConfigurer#getTransactionManager Before this commit, the transaction manager was configurable by implementing `BatchConfigurer#getTransactionManager`. The transaction manager being an implementation detail of the `JobRepository`, it should not be configurable at the same level as the `JobRepository` (ie in the same interface). This commit removes the method `getTransactionManager` from the `BatchConfigurer` interface. If needed, a custom transaction manager could be supplied by implementing `getJobRepository`, or via the constructor of `DefaultBatchConfigurer`. Resolves https://github.com/spring-projects/spring-batch/issues/4191 --- .../annotation/AbstractBatchConfiguration.java | 7 ------- .../core/configuration/annotation/BatchConfigurer.java | 7 +------ .../annotation/DefaultBatchConfigurer.java | 10 +++++++++- .../annotation/SimpleBatchConfiguration.java | 5 ----- ...onManagerConfigurationWithBatchConfigurerTests.java | 4 ++-- 5 files changed, 12 insertions(+), 21 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/AbstractBatchConfiguration.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/AbstractBatchConfiguration.java index 6bcf4c108..406806bd5 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/AbstractBatchConfiguration.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/AbstractBatchConfiguration.java @@ -92,13 +92,6 @@ public abstract class AbstractBatchConfiguration { return this.jobRegistry; } - /** - * Establish the {@link PlatformTransactionManager} for the batch execution. - * @return The instance of the {@link PlatformTransactionManager}. - * @throws Exception The {@link Exception} thrown if an error occurs. - */ - public abstract PlatformTransactionManager transactionManager() throws Exception; - /** * If a {@link BatchConfigurer} exists, return it. Otherwise, create a * {@link DefaultBatchConfigurer}. If more than one configurer is present, an diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/BatchConfigurer.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/BatchConfigurer.java index 3dac18a89..083a482db 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/BatchConfigurer.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/BatchConfigurer.java @@ -25,6 +25,7 @@ import org.springframework.transaction.PlatformTransactionManager; * a Batch system. * * @author Dave Syer + * @author Mahmoud Ben Hassine * */ public interface BatchConfigurer { @@ -35,12 +36,6 @@ public interface BatchConfigurer { */ JobRepository getJobRepository() throws Exception; - /** - * @return The {@link PlatformTransactionManager}. - * @throws Exception The {@link Exception} thrown if an error occurs. - */ - PlatformTransactionManager getTransactionManager() throws Exception; - /** * @return The {@link JobLauncher}. * @throws Exception The {@link Exception} thrown if an error occurs. diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/DefaultBatchConfigurer.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/DefaultBatchConfigurer.java index 861785f72..f98eb5fa2 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/DefaultBatchConfigurer.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/DefaultBatchConfigurer.java @@ -101,11 +101,19 @@ public class DefaultBatchConfigurer implements BatchConfigurer, InitializingBean return this.jobExplorer; } - @Override public PlatformTransactionManager getTransactionManager() { return this.transactionManager; } + /** + * Set the transaction manager. + * @param transactionManager the transaction manager to use. Must not be {@code null}. + */ + public void setTransactionManager(PlatformTransactionManager transactionManager) { + Assert.notNull(transactionManager, "TransactionManager must not be null"); + this.transactionManager = transactionManager; + } + @Override public void afterPropertiesSet() throws Exception { initialize(); diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.java index 26ee63273..5041477e2 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.java @@ -53,9 +53,4 @@ public class SimpleBatchConfiguration extends AbstractBatchConfiguration { return getOrCreateConfigurer().getJobExplorer(); } - @Override - public PlatformTransactionManager transactionManager() throws Exception { - return getOrCreateConfigurer().getTransactionManager(); - } - } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/TransactionManagerConfigurationWithBatchConfigurerTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/TransactionManagerConfigurationWithBatchConfigurerTests.java index 97b459b0b..fcd97dacc 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/TransactionManagerConfigurationWithBatchConfigurerTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/TransactionManagerConfigurationWithBatchConfigurerTests.java @@ -42,7 +42,7 @@ class TransactionManagerConfigurationWithBatchConfigurerTests extends Transactio void testConfigurationWithDataSourceAndNoTransactionManager() throws Exception { ApplicationContext applicationContext = new AnnotationConfigApplicationContext( BatchConfigurationWithDataSourceAndNoTransactionManager.class); - BatchConfigurer batchConfigurer = applicationContext.getBean(BatchConfigurer.class); + DefaultBatchConfigurer batchConfigurer = applicationContext.getBean(DefaultBatchConfigurer.class); PlatformTransactionManager platformTransactionManager = batchConfigurer.getTransactionManager(); assertTrue(platformTransactionManager instanceof JdbcTransactionManager); @@ -56,7 +56,7 @@ class TransactionManagerConfigurationWithBatchConfigurerTests extends Transactio void testConfigurationWithDataSourceAndTransactionManager() throws Exception { ApplicationContext applicationContext = new AnnotationConfigApplicationContext( BatchConfigurationWithDataSourceAndTransactionManager.class); - BatchConfigurer batchConfigurer = applicationContext.getBean(BatchConfigurer.class); + DefaultBatchConfigurer batchConfigurer = applicationContext.getBean(DefaultBatchConfigurer.class); PlatformTransactionManager platformTransactionManager = batchConfigurer.getTransactionManager(); assertSame(transactionManager, platformTransactionManager);