From 0d3282554c153af112fc0ad75f5ad94152a196dd Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Thu, 26 Aug 2021 14:32:50 +0200 Subject: [PATCH] Remove the unconditional exposure of the transaction manager as a bean This commit removes the unconditional exposure of the transaction manager as a bean in the application context. The transaction manager is still taken from the BatchConfigurer and set where needed (ie on JobRepository and StepBuilderFactory) as previously done, but is not exposed anymore as a bean to prevent any clash with a user defined transaction manager. If no transaction manager is provided, a DataSourceTransactionManager will be configured by default as required by batch (without being exposed as a bean). Resolves #816 --- .../annotation/AbstractBatchConfiguration.java | 1 - .../configuration/annotation/EnableBatchProcessing.java | 1 - .../annotation/ModularBatchConfiguration.java | 4 ++-- .../annotation/SimpleBatchConfiguration.java | 4 ++-- .../annotation/DataSourceConfiguration.java | 6 ++++++ ...nManagerConfigurationWithoutBatchConfigurerTests.java | 9 +++------ .../chunk/RemoteChunkingManagerStepBuilderTests.java | 6 ++++++ .../sample/remotechunking/DataSourceConfiguration.java | 6 ++++++ .../remotepartitioning/DataSourceConfiguration.java | 8 +++++++- 9 files changed, 32 insertions(+), 13 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 b1f2aa851..d0a320746 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 @@ -84,7 +84,6 @@ public abstract class AbstractBatchConfiguration implements ImportAware, Initial return this.jobRegistry; } - @Bean public abstract PlatformTransactionManager transactionManager() throws Exception; @Override diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/EnableBatchProcessing.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/EnableBatchProcessing.java index f86b41ac9..8d128e93c 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/EnableBatchProcessing.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/EnableBatchProcessing.java @@ -102,7 +102,6 @@ import org.springframework.transaction.PlatformTransactionManager; *
  • a {@link JobLauncher} (bean name "jobLauncher")
  • *
  • a {@link JobRegistry} (bean name "jobRegistry")
  • *
  • a {@link org.springframework.batch.core.explore.JobExplorer} (bean name "jobExplorer")
  • - *
  • a {@link PlatformTransactionManager} (bean name "transactionManager")
  • *
  • a {@link JobBuilderFactory} (bean name "jobBuilders") as a convenience to prevent you from having to inject the * job repository into every job, as in the examples above
  • *
  • a {@link StepBuilderFactory} (bean name "stepBuilders") as a convenience to prevent you from having to inject the diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/ModularBatchConfiguration.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/ModularBatchConfiguration.java index 8c337916d..f615f1a35 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/ModularBatchConfiguration.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/ModularBatchConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2021 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. @@ -34,6 +34,7 @@ import org.springframework.transaction.PlatformTransactionManager; * available by implementing the {@link BatchConfigurer} interface. * * @author Dave Syer + * @author Mahmoud Ben Hassine * @since 2.2 * @see EnableBatchProcessing */ @@ -61,7 +62,6 @@ public class ModularBatchConfiguration extends AbstractBatchConfiguration { } @Override - @Bean public PlatformTransactionManager transactionManager() throws Exception { return getConfigurer(configurers).getTransactionManager(); } 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 408282292..f6f9a36f8 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2021 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. @@ -41,6 +41,7 @@ import org.springframework.transaction.PlatformTransactionManager; * {@link BatchConfigurer}. * * @author Dave Syer + * @author Mahmoud Ben Hassine * @since 2.2 * @see EnableBatchProcessing */ @@ -87,7 +88,6 @@ public class SimpleBatchConfiguration extends AbstractBatchConfiguration { } @Override - @Bean public PlatformTransactionManager transactionManager() throws Exception { return createLazyProxy(transactionManager, PlatformTransactionManager.class); } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/DataSourceConfiguration.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/DataSourceConfiguration.java index 38d74ecc5..7330e529e 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/DataSourceConfiguration.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/DataSourceConfiguration.java @@ -20,6 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ResourceLoader; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils; import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; @@ -49,4 +50,9 @@ public class DataSourceConfiguration { .build(); } + @Bean + public DataSourceTransactionManager transactionManager(DataSource dataSource) { + return new DataSourceTransactionManager(dataSource); + } + } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/TransactionManagerConfigurationWithoutBatchConfigurerTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/TransactionManagerConfigurationWithoutBatchConfigurerTests.java index f1419a5af..23fc07587 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/TransactionManagerConfigurationWithoutBatchConfigurerTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/TransactionManagerConfigurationWithoutBatchConfigurerTests.java @@ -49,13 +49,10 @@ public class TransactionManagerConfigurationWithoutBatchConfigurerTests extends @Test public void testConfigurationWithDataSourceAndNoTransactionManager() throws Exception { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(BatchConfigurationWithDataSourceAndNoTransactionManager.class); - Assert.assertTrue(applicationContext.containsBean("transactionManager")); - PlatformTransactionManager platformTransactionManager = applicationContext.getBean(PlatformTransactionManager.class); - Object targetObject = AopTestUtils.getTargetObject(platformTransactionManager); - Assert.assertTrue(targetObject instanceof DataSourceTransactionManager); - DataSourceTransactionManager dataSourceTransactionManager = (DataSourceTransactionManager) targetObject; + PlatformTransactionManager platformTransactionManager = getTransactionManagerSetOnJobRepository(applicationContext.getBean(JobRepository.class)); + Assert.assertTrue(platformTransactionManager instanceof DataSourceTransactionManager); + DataSourceTransactionManager dataSourceTransactionManager = (DataSourceTransactionManager) platformTransactionManager; Assert.assertEquals(applicationContext.getBean(DataSource.class), dataSourceTransactionManager.getDataSource()); - Assert.assertSame(getTransactionManagerSetOnJobRepository(applicationContext.getBean(JobRepository.class)), dataSourceTransactionManager); } @Test diff --git a/spring-batch-integration/src/test/java/org/springframework/batch/integration/chunk/RemoteChunkingManagerStepBuilderTests.java b/spring-batch-integration/src/test/java/org/springframework/batch/integration/chunk/RemoteChunkingManagerStepBuilderTests.java index 2ffa33a2d..190eab9dc 100644 --- a/spring-batch-integration/src/test/java/org/springframework/batch/integration/chunk/RemoteChunkingManagerStepBuilderTests.java +++ b/spring-batch-integration/src/test/java/org/springframework/batch/integration/chunk/RemoteChunkingManagerStepBuilderTests.java @@ -52,6 +52,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.integration.channel.DirectChannel; import org.springframework.integration.channel.QueueChannel; import org.springframework.integration.core.MessagingTemplate; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import org.springframework.lang.Nullable; import org.springframework.messaging.PollableChannel; @@ -350,5 +351,10 @@ public class RemoteChunkingManagerStepBuilderTests { .build(); } + @Bean + public DataSourceTransactionManager transactionManager(DataSource dataSource) { + return new DataSourceTransactionManager(dataSource); + } + } } diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/remotechunking/DataSourceConfiguration.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/remotechunking/DataSourceConfiguration.java index 040a94dc4..08fbe58e0 100644 --- a/spring-batch-samples/src/main/java/org/springframework/batch/sample/remotechunking/DataSourceConfiguration.java +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/remotechunking/DataSourceConfiguration.java @@ -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.DataSourceTransactionManager; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; /** @@ -37,4 +38,9 @@ public class DataSourceConfiguration { .build(); } + @Bean + public DataSourceTransactionManager transactionManager(DataSource dataSource) { + return new DataSourceTransactionManager(dataSource); + } + } diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/remotepartitioning/DataSourceConfiguration.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/remotepartitioning/DataSourceConfiguration.java index 028c72c7c..34d664184 100644 --- a/spring-batch-samples/src/main/java/org/springframework/batch/sample/remotepartitioning/DataSourceConfiguration.java +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/remotepartitioning/DataSourceConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2018-2021 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. @@ -24,6 +24,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; /** * @author Mahmoud Ben Hassine @@ -50,4 +51,9 @@ public class DataSourceConfiguration { return dataSource; } + @Bean + public DataSourceTransactionManager transactionManager(DataSource dataSource) { + return new DataSourceTransactionManager(dataSource); + } + }