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
This commit is contained in:
Mahmoud Ben Hassine
2022-09-13 20:02:32 +02:00
parent 6e443cb1b1
commit d11b5b2c3a
5 changed files with 12 additions and 21 deletions

View File

@@ -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

View File

@@ -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.

View File

@@ -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();

View File

@@ -53,9 +53,4 @@ public class SimpleBatchConfiguration extends AbstractBatchConfiguration {
return getOrCreateConfigurer().getJobExplorer();
}
@Override
public PlatformTransactionManager transactionManager() throws Exception {
return getOrCreateConfigurer().getTransactionManager();
}
}

View File

@@ -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);