Skip to content

Commit 147968c

Browse files
author
Dave Syer
committed
Set isolation to DEFAULT for JPA transaction manager
Also logs a warning about the fact that locks may not be taken when starting a Job. JPA and Batch don't really work that well together in general so it's probably not worth a lot of effort to work aoround this. If anyone needs to they should create a custom JpaDialect (and a BatchConfigurer). Fixes spring-projectsgh-197
1 parent 2066c04 commit 147968c

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import javax.persistence.EntityManagerFactory;
2121
import javax.sql.DataSource;
2222

23+
import org.apache.commons.logging.Log;
24+
import org.apache.commons.logging.LogFactory;
2325
import org.springframework.batch.core.configuration.annotation.BatchConfigurer;
2426
import org.springframework.batch.core.launch.JobLauncher;
2527
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
@@ -36,6 +38,8 @@
3638
@Component
3739
public class BasicBatchConfigurer implements BatchConfigurer {
3840

41+
private static Log logger = LogFactory.getLog(BasicBatchConfigurer.class);
42+
3943
private DataSource dataSource;
4044
private EntityManagerFactory entityManagerFactory;
4145
private PlatformTransactionManager transactionManager;
@@ -84,6 +88,10 @@ private JobLauncher createJobLauncher() throws Exception {
8488
protected JobRepository createJobRepository() throws Exception {
8589
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
8690
factory.setDataSource(this.dataSource);
91+
if (this.entityManagerFactory != null) {
92+
logger.warn("JPA does not support custom isolation levels, so locks may not be taken when launching Jobs");
93+
factory.setIsolationLevelForCreate("ISOLATION_DEFAULT");
94+
}
8795
factory.setTransactionManager(getTransactionManager());
8896
factory.afterPropertiesSet();
8997
return (JobRepository) factory.getObject();

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454

5555
import static org.junit.Assert.assertEquals;
5656
import static org.junit.Assert.assertNotNull;
57+
import static org.junit.Assert.assertNull;
5758
import static org.junit.Assert.assertTrue;
5859

5960
/**
@@ -143,6 +144,9 @@ public void testUsingJpa() throws Exception {
143144
// It's a lazy proxy, but it does render its target if you ask for toString():
144145
assertTrue(transactionManager.toString().contains("JpaTransactionManager"));
145146
assertNotNull(this.context.getBean(EntityManagerFactory.class));
147+
// Ensure the JobRepository can be used (no problem with isolation level)
148+
assertNull(this.context.getBean(JobRepository.class).getLastJobExecution("job",
149+
new JobParameters()));
146150
}
147151

148152
@EnableBatchProcessing

0 commit comments

Comments
 (0)