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 4700d2008f4..9bdf51e17b7 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,11 @@ public class BatchAutoConfiguration { return (tablePrefix != null) ? tablePrefix : super.getTablePrefix(); } + @Override + protected boolean getValidateTransactionState() { + return this.properties.getJdbc().isValidateTransactionState(); + } + @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 4f9161ac7bc..3bdb237c52e 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 = true; + /** * 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 isValidateTransactionState() { + 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 1bf6c66d2d2..9500379ed2a 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)); 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 00000000000..a89fef9895a --- /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(); + } + + } + +}