Skip to content

Commit b852dab

Browse files
committed
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). Issue spring-projects#816
1 parent 8400b16 commit b852dab

File tree

9 files changed

+32
-13
lines changed

9 files changed

+32
-13
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/AbstractBatchConfiguration.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ public JobRegistry jobRegistry() throws Exception {
8686
return this.jobRegistry;
8787
}
8888

89-
@Bean
9089
public abstract PlatformTransactionManager transactionManager() throws Exception;
9190

9291
@Override

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/EnableBatchProcessing.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@
102102
* <li>a {@link JobLauncher} (bean name "jobLauncher")</li>
103103
* <li>a {@link JobRegistry} (bean name "jobRegistry")</li>
104104
* <li>a {@link org.springframework.batch.core.explore.JobExplorer} (bean name "jobExplorer")</li>
105-
* <li>a {@link PlatformTransactionManager} (bean name "transactionManager")</li>
106105
* <li>a {@link JobBuilderFactory} (bean name "jobBuilders") as a convenience to prevent you from having to inject the
107106
* job repository into every job, as in the examples above</li>
108107
* <li>a {@link StepBuilderFactory} (bean name "stepBuilders") as a convenience to prevent you from having to inject the

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/ModularBatchConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2013 the original author or authors.
2+
* Copyright 2012-2021 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.
@@ -34,6 +34,7 @@
3434
* available by implementing the {@link BatchConfigurer} interface.
3535
*
3636
* @author Dave Syer
37+
* @author Mahmoud Ben Hassine
3738
* @since 2.2
3839
* @see EnableBatchProcessing
3940
*/
@@ -61,7 +62,6 @@ public JobLauncher jobLauncher() throws Exception {
6162
}
6263

6364
@Override
64-
@Bean
6565
public PlatformTransactionManager transactionManager() throws Exception {
6666
return getConfigurer(configurers).getTransactionManager();
6767
}

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2013 the original author or authors.
2+
* Copyright 2012-2021 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.
@@ -41,6 +41,7 @@
4141
* {@link BatchConfigurer}.
4242
*
4343
* @author Dave Syer
44+
* @author Mahmoud Ben Hassine
4445
* @since 2.2
4546
* @see EnableBatchProcessing
4647
*/
@@ -87,7 +88,6 @@ public JobExplorer jobExplorer() {
8788
}
8889

8990
@Override
90-
@Bean
9191
public PlatformTransactionManager transactionManager() throws Exception {
9292
return createLazyProxy(transactionManager, PlatformTransactionManager.class);
9393
}

spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/DataSourceConfiguration.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.springframework.context.annotation.Bean;
2121
import org.springframework.context.annotation.Configuration;
2222
import org.springframework.core.io.ResourceLoader;
23+
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
2324
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
2425
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
2526
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
@@ -49,4 +50,9 @@ public DataSource dataSource() {
4950
.build();
5051
}
5152

53+
@Bean
54+
public DataSourceTransactionManager transactionManager(DataSource dataSource) {
55+
return new DataSourceTransactionManager(dataSource);
56+
}
57+
5258
}

spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/TransactionManagerConfigurationWithoutBatchConfigurerTests.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,10 @@ public void testConfigurationWithNoDataSourceAndTransactionManager() {
5050
@Test
5151
public void testConfigurationWithDataSourceAndNoTransactionManager() throws Exception {
5252
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(BatchConfigurationWithDataSourceAndNoTransactionManager.class);
53-
Assert.assertTrue(applicationContext.containsBean("transactionManager"));
54-
PlatformTransactionManager platformTransactionManager = applicationContext.getBean(PlatformTransactionManager.class);
55-
Object targetObject = AopTestUtils.getTargetObject(platformTransactionManager);
56-
Assert.assertTrue(targetObject instanceof DataSourceTransactionManager);
57-
DataSourceTransactionManager dataSourceTransactionManager = (DataSourceTransactionManager) targetObject;
53+
PlatformTransactionManager platformTransactionManager = getTransactionManagerSetOnJobRepository(applicationContext.getBean(JobRepository.class));
54+
Assert.assertTrue(platformTransactionManager instanceof DataSourceTransactionManager);
55+
DataSourceTransactionManager dataSourceTransactionManager = (DataSourceTransactionManager) platformTransactionManager;
5856
Assert.assertEquals(applicationContext.getBean(DataSource.class), dataSourceTransactionManager.getDataSource());
59-
Assert.assertSame(getTransactionManagerSetOnJobRepository(applicationContext.getBean(JobRepository.class)), dataSourceTransactionManager);
6057
}
6158

6259
@Test

spring-batch-integration/src/test/java/org/springframework/batch/integration/chunk/RemoteChunkingManagerStepBuilderTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.springframework.integration.channel.DirectChannel;
5353
import org.springframework.integration.channel.QueueChannel;
5454
import org.springframework.integration.core.MessagingTemplate;
55+
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
5556
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
5657
import org.springframework.lang.Nullable;
5758
import org.springframework.messaging.PollableChannel;
@@ -350,5 +351,10 @@ public DataSource dataSource() {
350351
.build();
351352
}
352353

354+
@Bean
355+
public DataSourceTransactionManager transactionManager(DataSource dataSource) {
356+
return new DataSourceTransactionManager(dataSource);
357+
}
358+
353359
}
354360
}

spring-batch-samples/src/main/java/org/springframework/batch/sample/remotechunking/DataSourceConfiguration.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.context.annotation.Bean;
2525
import org.springframework.context.annotation.Configuration;
2626
import org.springframework.context.annotation.PropertySource;
27+
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
2728
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
2829

2930
/**
@@ -41,4 +42,9 @@ public DataSource dataSource() {
4142
.build();
4243
}
4344

45+
@Bean
46+
public DataSourceTransactionManager transactionManager(DataSource dataSource) {
47+
return new DataSourceTransactionManager(dataSource);
48+
}
49+
4450
}

spring-batch-samples/src/main/java/org/springframework/batch/sample/remotepartitioning/DataSourceConfiguration.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018 the original author or authors.
2+
* Copyright 2018-2021 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.
@@ -24,6 +24,7 @@
2424
import org.springframework.context.annotation.Bean;
2525
import org.springframework.context.annotation.Configuration;
2626
import org.springframework.context.annotation.PropertySource;
27+
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
2728

2829
/**
2930
* @author Mahmoud Ben Hassine
@@ -50,4 +51,9 @@ public DataSource dataSource() {
5051
return dataSource;
5152
}
5253

54+
@Bean
55+
public DataSourceTransactionManager transactionManager(DataSource dataSource) {
56+
return new DataSourceTransactionManager(dataSource);
57+
}
58+
5359
}

0 commit comments

Comments
 (0)