Skip to content

Commit d11b5b2

Browse files
committed
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 #4191
1 parent 6e443cb commit d11b5b2

File tree

5 files changed

+12
-21
lines changed

5 files changed

+12
-21
lines changed

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,6 @@ public JobRegistry jobRegistry() throws Exception {
9292
return this.jobRegistry;
9393
}
9494

95-
/**
96-
* Establish the {@link PlatformTransactionManager} for the batch execution.
97-
* @return The instance of the {@link PlatformTransactionManager}.
98-
* @throws Exception The {@link Exception} thrown if an error occurs.
99-
*/
100-
public abstract PlatformTransactionManager transactionManager() throws Exception;
101-
10295
/**
10396
* If a {@link BatchConfigurer} exists, return it. Otherwise, create a
10497
* {@link DefaultBatchConfigurer}. If more than one configurer is present, an

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* a Batch system.
2626
*
2727
* @author Dave Syer
28+
* @author Mahmoud Ben Hassine
2829
*
2930
*/
3031
public interface BatchConfigurer {
@@ -35,12 +36,6 @@ public interface BatchConfigurer {
3536
*/
3637
JobRepository getJobRepository() throws Exception;
3738

38-
/**
39-
* @return The {@link PlatformTransactionManager}.
40-
* @throws Exception The {@link Exception} thrown if an error occurs.
41-
*/
42-
PlatformTransactionManager getTransactionManager() throws Exception;
43-
4439
/**
4540
* @return The {@link JobLauncher}.
4641
* @throws Exception The {@link Exception} thrown if an error occurs.

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,19 @@ public JobExplorer getJobExplorer() {
101101
return this.jobExplorer;
102102
}
103103

104-
@Override
105104
public PlatformTransactionManager getTransactionManager() {
106105
return this.transactionManager;
107106
}
108107

108+
/**
109+
* Set the transaction manager.
110+
* @param transactionManager the transaction manager to use. Must not be {@code null}.
111+
*/
112+
public void setTransactionManager(PlatformTransactionManager transactionManager) {
113+
Assert.notNull(transactionManager, "TransactionManager must not be null");
114+
this.transactionManager = transactionManager;
115+
}
116+
109117
@Override
110118
public void afterPropertiesSet() throws Exception {
111119
initialize();

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,4 @@ public JobExplorer jobExplorer() throws Exception {
5353
return getOrCreateConfigurer().getJobExplorer();
5454
}
5555

56-
@Override
57-
public PlatformTransactionManager transactionManager() throws Exception {
58-
return getOrCreateConfigurer().getTransactionManager();
59-
}
60-
6156
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class TransactionManagerConfigurationWithBatchConfigurerTests extends Transactio
4242
void testConfigurationWithDataSourceAndNoTransactionManager() throws Exception {
4343
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(
4444
BatchConfigurationWithDataSourceAndNoTransactionManager.class);
45-
BatchConfigurer batchConfigurer = applicationContext.getBean(BatchConfigurer.class);
45+
DefaultBatchConfigurer batchConfigurer = applicationContext.getBean(DefaultBatchConfigurer.class);
4646

4747
PlatformTransactionManager platformTransactionManager = batchConfigurer.getTransactionManager();
4848
assertTrue(platformTransactionManager instanceof JdbcTransactionManager);
@@ -56,7 +56,7 @@ void testConfigurationWithDataSourceAndNoTransactionManager() throws Exception {
5656
void testConfigurationWithDataSourceAndTransactionManager() throws Exception {
5757
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(
5858
BatchConfigurationWithDataSourceAndTransactionManager.class);
59-
BatchConfigurer batchConfigurer = applicationContext.getBean(BatchConfigurer.class);
59+
DefaultBatchConfigurer batchConfigurer = applicationContext.getBean(DefaultBatchConfigurer.class);
6060

6161
PlatformTransactionManager platformTransactionManager = batchConfigurer.getTransactionManager();
6262
assertSame(transactionManager, platformTransactionManager);

0 commit comments

Comments
 (0)