Add setter for isolation level with a strongly typed parameter

Resolves #4032
This commit is contained in:
Mahmoud Ben Hassine
2021-12-08 11:31:30 +01:00
parent e8e3f5da08
commit f3a11847e1
3 changed files with 25 additions and 4 deletions

View File

@@ -32,6 +32,7 @@ import org.springframework.beans.factory.FactoryBean;
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;
@@ -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.
@@ -135,6 +138,21 @@ public abstract class AbstractJobRepositoryFactoryBean implements FactoryBean<Jo
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

View File

@@ -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.
@@ -40,6 +40,7 @@ import org.springframework.jdbc.support.lob.DefaultLobHandler;
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;
@@ -52,6 +53,7 @@ import static org.mockito.Mockito.when;
/**
* @author Lucas Ward
* @author Will Schipp
* @author Mahmoud Ben Hassine
*
*/
public class JobRepositoryFactoryBeanTests {
@@ -331,7 +333,7 @@ public class JobRepositoryFactoryBeanTests {
@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(

View File

@@ -58,6 +58,7 @@ import org.springframework.lang.Nullable;
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;
@@ -170,7 +171,7 @@ public class ConcurrentTransactionTests {
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();