Skip to content

Commit f3a1184

Browse files
committed
Add setter for isolation level with a strongly typed parameter
Resolves #4032
1 parent e8e3f5d commit f3a1184

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/AbstractJobRepositoryFactoryBean.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.beans.factory.InitializingBean;
3333
import org.springframework.transaction.PlatformTransactionManager;
3434
import org.springframework.transaction.TransactionManager;
35+
import org.springframework.transaction.annotation.Isolation;
3536
import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
3637
import org.springframework.transaction.interceptor.TransactionInterceptor;
3738
import org.springframework.transaction.support.TransactionSynchronizationManager;
@@ -59,10 +60,12 @@ public abstract class AbstractJobRepositoryFactoryBean implements FactoryBean<Jo
5960

6061
private boolean validateTransactionState = true;
6162

63+
private static final String ISOLATION_LEVEL_PREFIX = "ISOLATION_";
64+
6265
/**
6366
* Default value for isolation level in create* method.
6467
*/
65-
private static final String DEFAULT_ISOLATION_LEVEL = "ISOLATION_SERIALIZABLE";
68+
private static final String DEFAULT_ISOLATION_LEVEL = ISOLATION_LEVEL_PREFIX + "SERIALIZABLE";
6669

6770
/**
6871
* @return fully configured {@link JobInstanceDao} implementation.
@@ -135,6 +138,21 @@ public void setIsolationLevelForCreate(String isolationLevelForCreate) {
135138
this.isolationLevelForCreate = isolationLevelForCreate;
136139
}
137140

141+
/**
142+
* public setter for the isolation level to be used for the transaction when
143+
* job execution entities are initially created. The default is
144+
* ISOLATION_SERIALIZABLE, which prevents accidental concurrent execution of
145+
* the same job (ISOLATION_REPEATABLE_READ would work as well).
146+
*
147+
* @param isolationLevelForCreate the isolation level to set
148+
*
149+
* @see SimpleJobRepository#createJobExecution(String,
150+
* org.springframework.batch.core.JobParameters)
151+
*/
152+
public void setIsolationLevelForCreate(Isolation isolationLevelForCreate) {
153+
this.setIsolationLevelForCreate(ISOLATION_LEVEL_PREFIX + isolationLevelForCreate.name());
154+
}
155+
138156
/**
139157
* Public setter for the {@link PlatformTransactionManager}.
140158
* @param transactionManager the transactionManager to set

spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/JobRepositoryFactoryBeanTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2014 the original author or authors.
2+
* Copyright 2006-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,6 +40,7 @@
4040
import org.springframework.jdbc.support.lob.LobHandler;
4141
import org.springframework.test.util.ReflectionTestUtils;
4242
import org.springframework.transaction.PlatformTransactionManager;
43+
import org.springframework.transaction.annotation.Isolation;
4344
import org.springframework.transaction.support.DefaultTransactionDefinition;
4445

4546
import static org.junit.Assert.assertEquals;
@@ -52,6 +53,7 @@
5253
/**
5354
* @author Lucas Ward
5455
* @author Will Schipp
56+
* @author Mahmoud Ben Hassine
5557
*
5658
*/
5759
public class JobRepositoryFactoryBeanTests {
@@ -331,7 +333,7 @@ public void testTransactionAttributesForCreateMethod() throws Exception {
331333
@Test
332334
public void testSetTransactionAttributesForCreateMethod() throws Exception {
333335

334-
factory.setIsolationLevelForCreate("ISOLATION_READ_UNCOMMITTED");
336+
factory.setIsolationLevelForCreate(Isolation.READ_UNCOMMITTED);
335337
testCreateRepository();
336338
JobRepository repository = factory.getObject();
337339
DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition(

spring-batch-core/src/test/java/org/springframework/batch/core/test/concurrent/ConcurrentTransactionTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import org.springframework.test.annotation.DirtiesContext;
5959
import org.springframework.test.context.ContextConfiguration;
6060
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
61+
import org.springframework.transaction.annotation.Isolation;
6162
import org.springframework.util.ClassUtils;
6263

6364
import static org.junit.Assert.assertEquals;
@@ -170,7 +171,7 @@ public Job concurrentJob() {
170171
protected JobRepository createJobRepository() throws Exception {
171172
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
172173
factory.setDataSource(getDataSource());
173-
factory.setIsolationLevelForCreate("ISOLATION_READ_COMMITTED");
174+
factory.setIsolationLevelForCreate(Isolation.READ_COMMITTED);
174175
factory.setTransactionManager(getTransactionManager());
175176
factory.afterPropertiesSet();
176177
return factory.getObject();

0 commit comments

Comments
 (0)