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
pull/208/head
Dave Syer 11 years ago
parent 2066c04a3e
commit 147968cf83

@ -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();

@ -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

Loading…
Cancel
Save