Allow to configure validateTransactionState for Spring Batch
See gh-44803 Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
This commit is contained in:
committed by
Andy Wilkinson
parent
1646fa6483
commit
f4a9236e01
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user