From f4a9236e016987401f4117845df3107751514048 Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Thu, 20 Mar 2025 15:52:34 +0800 Subject: [PATCH 1/2] Allow to configure validateTransactionState for Spring Batch See gh-44803 Signed-off-by: Yanming Zhou --- .../batch/BatchAutoConfiguration.java | 7 +++++++ .../boot/autoconfigure/batch/BatchProperties.java | 14 ++++++++++++++ .../batch/BatchAutoConfigurationTests.java | 14 ++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java index 4700d2008f..be2161ad14 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java @@ -66,6 +66,7 @@ import org.springframework.util.StringUtils; * @author Mahmoud Ben Hassine * @author Lars Uffmann * @author Lasse Wulff + * @author Yanming Zhou * @since 1.0.0 */ @AutoConfiguration(after = { HibernateJpaAutoConfiguration.class, TransactionAutoConfiguration.class }) @@ -140,6 +141,12 @@ public class BatchAutoConfiguration { return (tablePrefix != null) ? tablePrefix : super.getTablePrefix(); } + @Override + protected boolean getValidateTransactionState() { + Boolean validateTransactionState = this.properties.getJdbc().getValidateTransactionState(); + return (validateTransactionState != null) ? validateTransactionState : super.getValidateTransactionState(); + } + @Override protected Isolation getIsolationLevelForCreate() { Isolation isolation = this.properties.getJdbc().getIsolationLevelForCreate(); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java index 4f9161ac7b..8df4cf2055 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java @@ -27,6 +27,7 @@ import org.springframework.transaction.annotation.Isolation; * @author EddĂș MelĂ©ndez * @author Vedran Pavic * @author Mukul Kumar Chaundhyan + * @author Yanming Zhou * @since 1.2.0 */ @ConfigurationProperties("spring.batch") @@ -67,6 +68,11 @@ public class BatchProperties { private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/" + "batch/core/schema-@@platform@@.sql"; + /** + * Whether to validate the transaction state. + */ + private Boolean validateTransactionState; + /** * Transaction isolation level to use when creating job meta-data for new jobs. */ @@ -93,6 +99,14 @@ public class BatchProperties { */ private DatabaseInitializationMode initializeSchema = DatabaseInitializationMode.EMBEDDED; + public Boolean getValidateTransactionState() { + return this.validateTransactionState; + } + + public void setValidateTransactionState(Boolean validateTransactionState) { + this.validateTransactionState = validateTransactionState; + } + public Isolation getIsolationLevelForCreate() { return this.isolationLevelForCreate; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java index 1bf6c66d2d..9500379ed2 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java @@ -90,6 +90,7 @@ import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.jdbc.datasource.init.DatabasePopulator; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.annotation.Isolation; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -107,6 +108,7 @@ import static org.mockito.Mockito.mock; * @author Mahmoud Ben Hassine * @author Lars Uffmann * @author Lasse Wulff + * @author Yanming Zhou */ @ExtendWith(OutputCaptureExtension.class) class BatchAutoConfigurationTests { @@ -520,6 +522,18 @@ class BatchAutoConfigurationTests { }); } + @Test + void customJdbcPropertiesIsUsed() { + this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class) + .withPropertyValues("spring.batch.jdbc.validate-transaction-state:false", + "spring.batch.jdbc.isolation-level-for-create:READ_COMMITTED") + .run((context) -> { + SpringBootBatchConfiguration configuration = context.getBean(SpringBootBatchConfiguration.class); + assertThat(configuration.getValidateTransactionState()).isEqualTo(false); + assertThat(configuration.getIsolationLevelForCreate()).isEqualTo(Isolation.READ_COMMITTED); + }); + } + private JobLauncherApplicationRunner createInstance(String... registeredJobNames) { JobLauncherApplicationRunner runner = new JobLauncherApplicationRunner(mock(JobLauncher.class), mock(JobExplorer.class), mock(JobRepository.class)); From adae2e19467f98f51cbbb8144a62ec0b240c078f Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 20 Mar 2025 12:12:46 +0000 Subject: [PATCH 2/2] Polish "Allow to configure validateTransactionState for Spring Batch" See gh-44803 --- .../batch/BatchAutoConfiguration.java | 3 +- .../autoconfigure/batch/BatchProperties.java | 4 +- .../batch/BatchPropertiesTests.java | 47 +++++++++++++++++++ 3 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchPropertiesTests.java diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java index be2161ad14..9bdf51e17b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java @@ -143,8 +143,7 @@ public class BatchAutoConfiguration { @Override protected boolean getValidateTransactionState() { - Boolean validateTransactionState = this.properties.getJdbc().getValidateTransactionState(); - return (validateTransactionState != null) ? validateTransactionState : super.getValidateTransactionState(); + return this.properties.getJdbc().isValidateTransactionState(); } @Override diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java index 8df4cf2055..3bdb237c52 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java @@ -71,7 +71,7 @@ public class BatchProperties { /** * Whether to validate the transaction state. */ - private Boolean validateTransactionState; + private boolean validateTransactionState = true; /** * Transaction isolation level to use when creating job meta-data for new jobs. @@ -99,7 +99,7 @@ public class BatchProperties { */ private DatabaseInitializationMode initializeSchema = DatabaseInitializationMode.EMBEDDED; - public Boolean getValidateTransactionState() { + public boolean isValidateTransactionState() { return this.validateTransactionState; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchPropertiesTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchPropertiesTests.java new file mode 100644 index 0000000000..a89fef9895 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchPropertiesTests.java @@ -0,0 +1,47 @@ +/* + * Copyright 2012-2025 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 org.junit.jupiter.api.Test; + +import org.springframework.batch.core.configuration.support.DefaultBatchConfiguration; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link BatchProperties}. + * + * @author Andy Wilkinson + */ +class BatchPropertiesTests { + + @Test + void validateTransactionStateDefaultMatchesSpringBatchDefault() { + assertThat(new BatchProperties().getJdbc().isValidateTransactionState()) + .isEqualTo(new TestBatchConfiguration().getValidateTransactionState()); + } + + static class TestBatchConfiguration extends DefaultBatchConfiguration { + + @Override + public boolean getValidateTransactionState() { + return super.getValidateTransactionState(); + } + + } + +}