Skip to content

Provide a configuration property for Spring Batch's validateTransactionState #44803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
* @author Mahmoud Ben Hassine
* @author Lars Uffmann
* @author Lasse Wulff
* @author Yanming Zhou
* @since 1.0.0
*/
@AutoConfiguration(after = { HibernateJpaAutoConfiguration.class, TransactionAutoConfiguration.class })
Expand Down Expand Up @@ -140,6 +141,12 @@ protected String getTablePrefix() {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* @author Eddú Meléndez
* @author Vedran Pavic
* @author Mukul Kumar Chaundhyan
* @author Yanming Zhou
* @since 1.2.0
*/
@ConfigurationProperties("spring.batch")
Expand Down Expand Up @@ -67,6 +68,11 @@ public static class Jdbc {
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"
+ "batch/core/schema-@@platform@@.sql";

/**
* Whether to validate the transaction state.
*/
private Boolean validateTransactionState;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a boolean with a default value of true.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should align with tablePrefix since it's not default to BATCH_, Spring Boot doesn't need to change if Spring Batch change its defaults, but many other AutoConfigurations doesn't work in such way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our approach has changed over time. These days, we prefer to provide a default if we can as it's better for users when looking at a property and its documentation, particularly in an IDE that supports that through the metadata. It's very unlikely that Batch will change its default. If it does, there's now a test in place that will fail.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we prefer to provide a default if we can as it's better for users when looking at a property and its documentation, particularly in an IDE that supports that through the metadata.

Then tablePrefix and isolationLevelForCreate should have default value.


/**
* Transaction isolation level to use when creating job meta-data for new jobs.
*/
Expand All @@ -93,6 +99,14 @@ public static class Jdbc {
*/
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
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;
Expand All @@ -107,6 +108,7 @@
* @author Mahmoud Ben Hassine
* @author Lars Uffmann
* @author Lasse Wulff
* @author Yanming Zhou
*/
@ExtendWith(OutputCaptureExtension.class)
class BatchAutoConfigurationTests {
Expand Down Expand Up @@ -520,6 +522,18 @@ void defaultExecutionContextSerializerIsUsed() {
});
}

@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));
Expand Down