Browse Source

Merge pull request #44803 from quaff

* gh-44803:
  Polish "Allow to configure validateTransactionState for Spring Batch"
  Allow to configure validateTransactionState for Spring Batch

Closes gh-44803
pull/44800/head
Andy Wilkinson 11 months ago
parent
commit
84fbf439d4
  1. 6
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java
  2. 14
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java
  3. 14
      spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java
  4. 47
      spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchPropertiesTests.java

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

14
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; @@ -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 { @@ -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 { @@ -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;
}

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

47
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchPropertiesTests.java

@ -0,0 +1,47 @@ @@ -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();
}
}
}
Loading…
Cancel
Save