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
This commit is contained in:
@@ -66,6 +66,7 @@ import org.springframework.util.StringUtils;
|
|||||||
* @author Mahmoud Ben Hassine
|
* @author Mahmoud Ben Hassine
|
||||||
* @author Lars Uffmann
|
* @author Lars Uffmann
|
||||||
* @author Lasse Wulff
|
* @author Lasse Wulff
|
||||||
|
* @author Yanming Zhou
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
@AutoConfiguration(after = { HibernateJpaAutoConfiguration.class, TransactionAutoConfiguration.class })
|
@AutoConfiguration(after = { HibernateJpaAutoConfiguration.class, TransactionAutoConfiguration.class })
|
||||||
@@ -140,6 +141,11 @@ public class BatchAutoConfiguration {
|
|||||||
return (tablePrefix != null) ? tablePrefix : super.getTablePrefix();
|
return (tablePrefix != null) ? tablePrefix : super.getTablePrefix();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean getValidateTransactionState() {
|
||||||
|
return this.properties.getJdbc().isValidateTransactionState();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Isolation getIsolationLevelForCreate() {
|
protected Isolation getIsolationLevelForCreate() {
|
||||||
Isolation isolation = this.properties.getJdbc().getIsolationLevelForCreate();
|
Isolation isolation = this.properties.getJdbc().getIsolationLevelForCreate();
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ import org.springframework.transaction.annotation.Isolation;
|
|||||||
* @author Eddú Meléndez
|
* @author Eddú Meléndez
|
||||||
* @author Vedran Pavic
|
* @author Vedran Pavic
|
||||||
* @author Mukul Kumar Chaundhyan
|
* @author Mukul Kumar Chaundhyan
|
||||||
|
* @author Yanming Zhou
|
||||||
* @since 1.2.0
|
* @since 1.2.0
|
||||||
*/
|
*/
|
||||||
@ConfigurationProperties("spring.batch")
|
@ConfigurationProperties("spring.batch")
|
||||||
@@ -67,6 +68,11 @@ public class BatchProperties {
|
|||||||
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"
|
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"
|
||||||
+ "batch/core/schema-@@platform@@.sql";
|
+ "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.
|
* 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;
|
private DatabaseInitializationMode initializeSchema = DatabaseInitializationMode.EMBEDDED;
|
||||||
|
|
||||||
|
public boolean isValidateTransactionState() {
|
||||||
|
return this.validateTransactionState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValidateTransactionState(Boolean validateTransactionState) {
|
||||||
|
this.validateTransactionState = validateTransactionState;
|
||||||
|
}
|
||||||
|
|
||||||
public Isolation getIsolationLevelForCreate() {
|
public Isolation getIsolationLevelForCreate() {
|
||||||
return this.isolationLevelForCreate;
|
return this.isolationLevelForCreate;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -90,6 +90,7 @@ import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
|||||||
import org.springframework.jdbc.datasource.init.DatabasePopulator;
|
import org.springframework.jdbc.datasource.init.DatabasePopulator;
|
||||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||||
import org.springframework.transaction.PlatformTransactionManager;
|
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.assertThat;
|
||||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||||
@@ -107,6 +108,7 @@ import static org.mockito.Mockito.mock;
|
|||||||
* @author Mahmoud Ben Hassine
|
* @author Mahmoud Ben Hassine
|
||||||
* @author Lars Uffmann
|
* @author Lars Uffmann
|
||||||
* @author Lasse Wulff
|
* @author Lasse Wulff
|
||||||
|
* @author Yanming Zhou
|
||||||
*/
|
*/
|
||||||
@ExtendWith(OutputCaptureExtension.class)
|
@ExtendWith(OutputCaptureExtension.class)
|
||||||
class BatchAutoConfigurationTests {
|
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) {
|
private JobLauncherApplicationRunner createInstance(String... registeredJobNames) {
|
||||||
JobLauncherApplicationRunner runner = new JobLauncherApplicationRunner(mock(JobLauncher.class),
|
JobLauncherApplicationRunner runner = new JobLauncherApplicationRunner(mock(JobLauncher.class),
|
||||||
mock(JobExplorer.class), mock(JobRepository.class));
|
mock(JobExplorer.class), mock(JobRepository.class));
|
||||||
|
|||||||
@@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user