Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce @BatchTransactionManager to make it easier to configure Spring Batch to use a custom transaction manager #39473

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
* @author Kazuki Shimizu
* @author Mahmoud Ben Hassine
* @author Lars Uffmann
* @author Lasse Wulff
* @since 1.0.0
*/
@AutoConfiguration(after = { HibernateJpaAutoConfiguration.class, TransactionAutoConfiguration.class })
Expand Down Expand Up @@ -108,11 +109,13 @@ static class SpringBootBatchConfiguration extends DefaultBatchConfiguration {
private final ExecutionContextSerializer executionContextSerializer;

SpringBootBatchConfiguration(DataSource dataSource, @BatchDataSource ObjectProvider<DataSource> batchDataSource,
PlatformTransactionManager transactionManager, BatchProperties properties,
PlatformTransactionManager transactionManager,
@BatchTransactionManager ObjectProvider<PlatformTransactionManager> batchTransactionManager,
BatchProperties properties,
ObjectProvider<BatchConversionServiceCustomizer> batchConversionServiceCustomizers,
ObjectProvider<ExecutionContextSerializer> executionContextSerializer) {
this.dataSource = batchDataSource.getIfAvailable(() -> dataSource);
this.transactionManager = transactionManager;
this.transactionManager = batchTransactionManager.getIfAvailable(() -> transactionManager);
this.properties = properties;
this.batchConversionServiceCustomizers = batchConversionServiceCustomizers.orderedStream().toList();
this.executionContextSerializer = executionContextSerializer
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2012-2014 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.autoconfigure.batch;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.transaction.PlatformTransactionManager;

/**
* Qualifier annotation for a {@link PlatformTransactionManager
* PlatformTransactionManager} to be injected into Batch auto-configuration. Can be used
* on a secondary {@link PlatformTransactionManager PlatformTransactionManager}, if there
* is another one marked as {@link Primary @Primary}.
*
* @author Lasse Wulff
* @since 3.3.0
*/
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Qualifier
public @interface BatchTransactionManager {

}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import org.springframework.context.annotation.Primary;
import org.springframework.core.annotation.Order;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.integration.transaction.PseudoTransactionManager;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
Expand All @@ -102,6 +103,7 @@
* @author Kazuki Shimizu
* @author Mahmoud Ben Hassine
* @author Lars Uffmann
* @author Lasse Wulff
*/
@ExtendWith(OutputCaptureExtension.class)
class BatchAutoConfigurationTests {
Expand Down Expand Up @@ -351,6 +353,18 @@ void testBatchDataSource() {
});
}

@Test
void testBatchTransactionManager() {
this.contextRunner.withUserConfiguration(TestConfiguration.class, BatchTransactionManagerConfiguration.class)
.run((context) -> {
assertThat(context).hasSingleBean(SpringBootBatchConfiguration.class);
PlatformTransactionManager batchTransactionManager = context.getBean("batchTransactionManager",
PlatformTransactionManager.class);
assertThat(context.getBean(SpringBootBatchConfiguration.class).getTransactionManager())
.isEqualTo(batchTransactionManager);
});
}

@Test
void jobRepositoryBeansDependOnBatchDataSourceInitializer() {
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
Expand Down Expand Up @@ -519,6 +533,28 @@ public DataSource batchDataSource() {

}

@Configuration(proxyBeanMethods = false)
protected static class BatchTransactionManagerConfiguration {

@Bean
public DataSource dataSource() {
return DataSourceBuilder.create().url("jdbc:hsqldb:mem:database").username("sa").build();
}

@Bean
@Primary
public PlatformTransactionManager normalTransactionManager() {
return new PseudoTransactionManager();
}

@BatchTransactionManager
@Bean
public PlatformTransactionManager batchTransactionManager() {
return new PseudoTransactionManager();
}

}

@Configuration(proxyBeanMethods = false)
static class EmptyConfiguration {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ For more info about Spring Batch, see the {spring-batch}[Spring Batch project pa



[[howto.batch.specifying-a-transaction-manager]]
=== Specifying a Batch Transaction Manager
Similar to <<howto.batch.specifying-a-data-source>> you can also define a `PlatformTransactionManager`
for use in the batch processing by marking it as `@BatchTransactionManager`.
If you do so and want two transaction managers, remember to mark the other one as `@Primary`.



[[howto.batch.running-jobs-on-startup]]
=== Running Spring Batch Jobs on Startup
Spring Batch auto-configuration is enabled by adding `spring-boot-starter-batch` to your application's classpath.
Expand Down