Skip to content

Commit 84fbf43

Browse files
committed
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
2 parents 0453bf2 + adae2e1 commit 84fbf43

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java

+6
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
* @author Mahmoud Ben Hassine
6767
* @author Lars Uffmann
6868
* @author Lasse Wulff
69+
* @author Yanming Zhou
6970
* @since 1.0.0
7071
*/
7172
@AutoConfiguration(after = { HibernateJpaAutoConfiguration.class, TransactionAutoConfiguration.class })
@@ -140,6 +141,11 @@ protected String getTablePrefix() {
140141
return (tablePrefix != null) ? tablePrefix : super.getTablePrefix();
141142
}
142143

144+
@Override
145+
protected boolean getValidateTransactionState() {
146+
return this.properties.getJdbc().isValidateTransactionState();
147+
}
148+
143149
@Override
144150
protected Isolation getIsolationLevelForCreate() {
145151
Isolation isolation = this.properties.getJdbc().getIsolationLevelForCreate();

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java

+14
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* @author Eddú Meléndez
2828
* @author Vedran Pavic
2929
* @author Mukul Kumar Chaundhyan
30+
* @author Yanming Zhou
3031
* @since 1.2.0
3132
*/
3233
@ConfigurationProperties("spring.batch")
@@ -67,6 +68,11 @@ public static class Jdbc {
6768
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"
6869
+ "batch/core/schema-@@platform@@.sql";
6970

71+
/**
72+
* Whether to validate the transaction state.
73+
*/
74+
private boolean validateTransactionState = true;
75+
7076
/**
7177
* Transaction isolation level to use when creating job meta-data for new jobs.
7278
*/
@@ -93,6 +99,14 @@ public static class Jdbc {
9399
*/
94100
private DatabaseInitializationMode initializeSchema = DatabaseInitializationMode.EMBEDDED;
95101

102+
public boolean isValidateTransactionState() {
103+
return this.validateTransactionState;
104+
}
105+
106+
public void setValidateTransactionState(Boolean validateTransactionState) {
107+
this.validateTransactionState = validateTransactionState;
108+
}
109+
96110
public Isolation getIsolationLevelForCreate() {
97111
return this.isolationLevelForCreate;
98112
}

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

+14
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
import org.springframework.jdbc.datasource.init.DatabasePopulator;
9191
import org.springframework.orm.jpa.JpaTransactionManager;
9292
import org.springframework.transaction.PlatformTransactionManager;
93+
import org.springframework.transaction.annotation.Isolation;
9394

9495
import static org.assertj.core.api.Assertions.assertThat;
9596
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -107,6 +108,7 @@
107108
* @author Mahmoud Ben Hassine
108109
* @author Lars Uffmann
109110
* @author Lasse Wulff
111+
* @author Yanming Zhou
110112
*/
111113
@ExtendWith(OutputCaptureExtension.class)
112114
class BatchAutoConfigurationTests {
@@ -520,6 +522,18 @@ void defaultExecutionContextSerializerIsUsed() {
520522
});
521523
}
522524

525+
@Test
526+
void customJdbcPropertiesIsUsed() {
527+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
528+
.withPropertyValues("spring.batch.jdbc.validate-transaction-state:false",
529+
"spring.batch.jdbc.isolation-level-for-create:READ_COMMITTED")
530+
.run((context) -> {
531+
SpringBootBatchConfiguration configuration = context.getBean(SpringBootBatchConfiguration.class);
532+
assertThat(configuration.getValidateTransactionState()).isEqualTo(false);
533+
assertThat(configuration.getIsolationLevelForCreate()).isEqualTo(Isolation.READ_COMMITTED);
534+
});
535+
}
536+
523537
private JobLauncherApplicationRunner createInstance(String... registeredJobNames) {
524538
JobLauncherApplicationRunner runner = new JobLauncherApplicationRunner(mock(JobLauncher.class),
525539
mock(JobExplorer.class), mock(JobRepository.class));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.batch;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.batch.core.configuration.support.DefaultBatchConfiguration;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
/**
26+
* Tests for {@link BatchProperties}.
27+
*
28+
* @author Andy Wilkinson
29+
*/
30+
class BatchPropertiesTests {
31+
32+
@Test
33+
void validateTransactionStateDefaultMatchesSpringBatchDefault() {
34+
assertThat(new BatchProperties().getJdbc().isValidateTransactionState())
35+
.isEqualTo(new TestBatchConfiguration().getValidateTransactionState());
36+
}
37+
38+
static class TestBatchConfiguration extends DefaultBatchConfiguration {
39+
40+
@Override
41+
public boolean getValidateTransactionState() {
42+
return super.getValidateTransactionState();
43+
}
44+
45+
}
46+
47+
}

0 commit comments

Comments
 (0)