From 147968cf837da0398fdb186e6f80d201c668ffcb Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 8 Jan 2014 09:58:23 +0000 Subject: [PATCH] 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 gh-197 --- .../boot/autoconfigure/batch/BasicBatchConfigurer.java | 8 ++++++++ .../autoconfigure/batch/BatchAutoConfigurationTests.java | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java index 218a673484..ac47c16caa 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java @@ -20,6 +20,8 @@ import javax.annotation.PostConstruct; import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.batch.core.configuration.annotation.BatchConfigurer; import org.springframework.batch.core.launch.JobLauncher; import org.springframework.batch.core.launch.support.SimpleJobLauncher; @@ -36,6 +38,8 @@ import org.springframework.transaction.PlatformTransactionManager; @Component public class BasicBatchConfigurer implements BatchConfigurer { + private static Log logger = LogFactory.getLog(BasicBatchConfigurer.class); + private DataSource dataSource; private EntityManagerFactory entityManagerFactory; private PlatformTransactionManager transactionManager; @@ -84,6 +88,10 @@ public class BasicBatchConfigurer implements BatchConfigurer { protected JobRepository createJobRepository() throws Exception { JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean(); factory.setDataSource(this.dataSource); + if (this.entityManagerFactory != null) { + logger.warn("JPA does not support custom isolation levels, so locks may not be taken when launching Jobs"); + factory.setIsolationLevelForCreate("ISOLATION_DEFAULT"); + } factory.setTransactionManager(getTransactionManager()); factory.afterPropertiesSet(); return (JobRepository) factory.getObject(); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java index c8560b77ac..be327187b1 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java @@ -54,6 +54,7 @@ import org.springframework.transaction.PlatformTransactionManager; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; /** @@ -143,6 +144,9 @@ public class BatchAutoConfigurationTests { // It's a lazy proxy, but it does render its target if you ask for toString(): assertTrue(transactionManager.toString().contains("JpaTransactionManager")); assertNotNull(this.context.getBean(EntityManagerFactory.class)); + // Ensure the JobRepository can be used (no problem with isolation level) + assertNull(this.context.getBean(JobRepository.class).getLastJobExecution("job", + new JobParameters())); } @EnableBatchProcessing