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
This commit is contained in:
Dave Syer 2014-01-08 09:58:23 +00:00
parent 2066c04a3e
commit 147968cf83
2 changed files with 12 additions and 0 deletions

View File

@ -20,6 +20,8 @@ import javax.annotation.PostConstruct;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource; 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.configuration.annotation.BatchConfigurer;
import org.springframework.batch.core.launch.JobLauncher; import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.SimpleJobLauncher; import org.springframework.batch.core.launch.support.SimpleJobLauncher;
@ -36,6 +38,8 @@ import org.springframework.transaction.PlatformTransactionManager;
@Component @Component
public class BasicBatchConfigurer implements BatchConfigurer { public class BasicBatchConfigurer implements BatchConfigurer {
private static Log logger = LogFactory.getLog(BasicBatchConfigurer.class);
private DataSource dataSource; private DataSource dataSource;
private EntityManagerFactory entityManagerFactory; private EntityManagerFactory entityManagerFactory;
private PlatformTransactionManager transactionManager; private PlatformTransactionManager transactionManager;
@ -84,6 +88,10 @@ public class BasicBatchConfigurer implements BatchConfigurer {
protected JobRepository createJobRepository() throws Exception { protected JobRepository createJobRepository() throws Exception {
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean(); JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
factory.setDataSource(this.dataSource); 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.setTransactionManager(getTransactionManager());
factory.afterPropertiesSet(); factory.afterPropertiesSet();
return (JobRepository) factory.getObject(); return (JobRepository) factory.getObject();

View File

@ -54,6 +54,7 @@ import org.springframework.transaction.PlatformTransactionManager;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; 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(): // It's a lazy proxy, but it does render its target if you ask for toString():
assertTrue(transactionManager.toString().contains("JpaTransactionManager")); assertTrue(transactionManager.toString().contains("JpaTransactionManager"));
assertNotNull(this.context.getBean(EntityManagerFactory.class)); 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 @EnableBatchProcessing