Skip to content

Commit

Permalink
Remove the unconditional exposure of the transaction manager as a bean
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fmbenhassine committed Aug 26, 2021
1 parent 8400b16 commit b852dab
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ public JobRegistry jobRegistry() throws Exception {
return this.jobRegistry;
}

@Bean
public abstract PlatformTransactionManager transactionManager() throws Exception;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@
* <li>a {@link JobLauncher} (bean name "jobLauncher")</li>
* <li>a {@link JobRegistry} (bean name "jobRegistry")</li>
* <li>a {@link org.springframework.batch.core.explore.JobExplorer} (bean name "jobExplorer")</li>
* <li>a {@link PlatformTransactionManager} (bean name "transactionManager")</li>
* <li>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</li>
* <li>a {@link StepBuilderFactory} (bean name "stepBuilders") as a convenience to prevent you from having to inject the
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -34,6 +34,7 @@
* available by implementing the {@link BatchConfigurer} interface.
*
* @author Dave Syer
* @author Mahmoud Ben Hassine
* @since 2.2
* @see EnableBatchProcessing
*/
Expand Down Expand Up @@ -61,7 +62,6 @@ public JobLauncher jobLauncher() throws Exception {
}

@Override
@Bean
public PlatformTransactionManager transactionManager() throws Exception {
return getConfigurer(configurers).getTransactionManager();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -41,6 +41,7 @@
* {@link BatchConfigurer}.
*
* @author Dave Syer
* @author Mahmoud Ben Hassine
* @since 2.2
* @see EnableBatchProcessing
*/
Expand Down Expand Up @@ -87,7 +88,6 @@ public JobExplorer jobExplorer() {
}

@Override
@Bean
public PlatformTransactionManager transactionManager() throws Exception {
return createLazyProxy(transactionManager, PlatformTransactionManager.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;
Expand Down Expand Up @@ -49,4 +50,9 @@ public DataSource dataSource() {
.build();
}

@Bean
public DataSourceTransactionManager transactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,10 @@ public void testConfigurationWithNoDataSourceAndTransactionManager() {
@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
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;
Expand Down Expand Up @@ -350,5 +351,10 @@ public DataSource dataSource() {
.build();
}

@Bean
public DataSourceTransactionManager transactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;

/**
Expand All @@ -41,4 +42,9 @@ public DataSource dataSource() {
.build();
}

@Bean
public DataSourceTransactionManager transactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}

}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -24,6 +24,7 @@
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
Expand All @@ -50,4 +51,9 @@ public DataSource dataSource() {
return dataSource;
}

@Bean
public DataSourceTransactionManager transactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}

}

0 comments on commit b852dab

Please sign in to comment.