Skip to content

Commit

Permalink
Add setter for isolation level with a strongly typed parameter
Browse files Browse the repository at this point in the history
Resolves #4032
  • Loading branch information
fmbenhassine committed Dec 8, 2021
1 parent e8e3f5d commit f3a1184
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
Expand Up @@ -32,6 +32,7 @@
import org.springframework.beans.factory.InitializingBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
import org.springframework.transaction.interceptor.TransactionInterceptor;
import org.springframework.transaction.support.TransactionSynchronizationManager;
Expand Down Expand Up @@ -59,10 +60,12 @@ public abstract class AbstractJobRepositoryFactoryBean implements FactoryBean<Jo

private boolean validateTransactionState = true;

private static final String ISOLATION_LEVEL_PREFIX = "ISOLATION_";

/**
* Default value for isolation level in create* method.
*/
private static final String DEFAULT_ISOLATION_LEVEL = "ISOLATION_SERIALIZABLE";
private static final String DEFAULT_ISOLATION_LEVEL = ISOLATION_LEVEL_PREFIX + "SERIALIZABLE";

/**
* @return fully configured {@link JobInstanceDao} implementation.
Expand Down Expand Up @@ -135,6 +138,21 @@ public void setIsolationLevelForCreate(String isolationLevelForCreate) {
this.isolationLevelForCreate = isolationLevelForCreate;
}

/**
* public setter for the isolation level to be used for the transaction when
* job execution entities are initially created. The default is
* ISOLATION_SERIALIZABLE, which prevents accidental concurrent execution of
* the same job (ISOLATION_REPEATABLE_READ would work as well).
*
* @param isolationLevelForCreate the isolation level to set
*
* @see SimpleJobRepository#createJobExecution(String,
* org.springframework.batch.core.JobParameters)
*/
public void setIsolationLevelForCreate(Isolation isolationLevelForCreate) {
this.setIsolationLevelForCreate(ISOLATION_LEVEL_PREFIX + isolationLevelForCreate.name());
}

/**
* Public setter for the {@link PlatformTransactionManager}.
* @param transactionManager the transactionManager to set
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2014 the original author or authors.
* Copyright 2006-2021 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.
Expand Down Expand Up @@ -40,6 +40,7 @@
import org.springframework.jdbc.support.lob.LobHandler;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import static org.junit.Assert.assertEquals;
Expand All @@ -52,6 +53,7 @@
/**
* @author Lucas Ward
* @author Will Schipp
* @author Mahmoud Ben Hassine
*
*/
public class JobRepositoryFactoryBeanTests {
Expand Down Expand Up @@ -331,7 +333,7 @@ public void testTransactionAttributesForCreateMethod() throws Exception {
@Test
public void testSetTransactionAttributesForCreateMethod() throws Exception {

factory.setIsolationLevelForCreate("ISOLATION_READ_UNCOMMITTED");
factory.setIsolationLevelForCreate(Isolation.READ_UNCOMMITTED);
testCreateRepository();
JobRepository repository = factory.getObject();
DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition(
Expand Down
Expand Up @@ -58,6 +58,7 @@
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.util.ClassUtils;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -170,7 +171,7 @@ public Job concurrentJob() {
protected JobRepository createJobRepository() throws Exception {
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
factory.setDataSource(getDataSource());
factory.setIsolationLevelForCreate("ISOLATION_READ_COMMITTED");
factory.setIsolationLevelForCreate(Isolation.READ_COMMITTED);
factory.setTransactionManager(getTransactionManager());
factory.afterPropertiesSet();
return factory.getObject();
Expand Down

0 comments on commit f3a1184

Please sign in to comment.