Revert to thread safe but less robust in the face of transaction exceptions MapStepExecutionDao
This commit is contained in:
@@ -34,7 +34,7 @@ public class Entity implements Serializable {
|
||||
|
||||
private Long id;
|
||||
|
||||
private Integer version;
|
||||
private volatile Integer version;
|
||||
|
||||
public Entity() {
|
||||
super();
|
||||
|
||||
@@ -21,13 +21,13 @@ import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import org.springframework.batch.core.Entity;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.support.SerializationUtils;
|
||||
import org.springframework.batch.support.transaction.TransactionAwareProxyFactory;
|
||||
import org.springframework.dao.OptimisticLockingFailureException;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
@@ -37,9 +37,9 @@ import org.springframework.util.ReflectionUtils;
|
||||
*/
|
||||
public class MapStepExecutionDao implements StepExecutionDao {
|
||||
|
||||
private Map<Long, Map<Long, StepExecution>> executionsByJobExecutionId = TransactionAwareProxyFactory.createAppendOnlyTransactionalMap();
|
||||
private Map<Long, Map<Long, StepExecution>> executionsByJobExecutionId = new ConcurrentHashMap<Long, Map<Long,StepExecution>>();
|
||||
|
||||
private Map<Long, StepExecution> executionsByStepExecutionId = TransactionAwareProxyFactory.createAppendOnlyTransactionalMap();
|
||||
private Map<Long, StepExecution> executionsByStepExecutionId = new ConcurrentHashMap<Long, StepExecution>();
|
||||
|
||||
private AtomicLong currentId = new AtomicLong();
|
||||
|
||||
@@ -71,7 +71,7 @@ public class MapStepExecutionDao implements StepExecutionDao {
|
||||
|
||||
Map<Long, StepExecution> executions = executionsByJobExecutionId.get(stepExecution.getJobExecutionId());
|
||||
if (executions == null) {
|
||||
executions = TransactionAwareProxyFactory.createAppendOnlyTransactionalMap();
|
||||
executions = new ConcurrentHashMap<Long, StepExecution>();
|
||||
executionsByJobExecutionId.put(stepExecution.getJobExecutionId(), executions);
|
||||
}
|
||||
|
||||
|
||||
@@ -23,13 +23,10 @@ import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
|
||||
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
|
||||
import org.springframework.core.task.SimpleAsyncTaskExecutor;
|
||||
import org.springframework.transaction.TransactionException;
|
||||
import org.springframework.transaction.UnexpectedRollbackException;
|
||||
import org.springframework.transaction.interceptor.RollbackRuleAttribute;
|
||||
import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
|
||||
import org.springframework.transaction.interceptor.TransactionAttribute;
|
||||
import org.springframework.transaction.interceptor.TransactionAttributeEditor;
|
||||
import org.springframework.transaction.support.DefaultTransactionStatus;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -473,40 +470,6 @@ public class FaultTolerantStepFactoryBeanRollbackTests {
|
||||
assertEquals("[1, 2, 3, 4, 5]", processor.getProcessed().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransactionException() throws Exception {
|
||||
ResourcelessTransactionManager transactionManager = new ResourcelessTransactionManager() {
|
||||
private boolean failed = false;
|
||||
protected void doCommit(DefaultTransactionStatus status) throws TransactionException {
|
||||
if (writer.getWritten().isEmpty() || failed || !isExistingTransaction(status.getTransaction())) {
|
||||
super.doCommit(status);
|
||||
return;
|
||||
}
|
||||
failed = true;
|
||||
status.setRollbackOnly();
|
||||
super.doRollback(status);
|
||||
throw new UnexpectedRollbackException("Planned");
|
||||
}
|
||||
};
|
||||
MapJobRepositoryFactoryBean repositoryFactory = new MapJobRepositoryFactoryBean();
|
||||
repositoryFactory.setTransactionManager(transactionManager);
|
||||
repositoryFactory.afterPropertiesSet();
|
||||
repository = (JobRepository) repositoryFactory.getObject();
|
||||
factory.setJobRepository(repository);
|
||||
factory.setTransactionManager(transactionManager);
|
||||
|
||||
jobExecution = repository.createJobExecution("skipJob", new JobParameters());
|
||||
stepExecution = jobExecution.createStepExecution(factory.getName());
|
||||
repository.add(stepExecution);
|
||||
|
||||
Step step = (Step) factory.getObject();
|
||||
|
||||
step.execute(stepExecution);
|
||||
assertEquals(BatchStatus.FAILED, stepExecution.getStatus());
|
||||
|
||||
assertEquals("[]", writer.getCommitted().toString());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Collection<Class<? extends Throwable>> getExceptionList(Class<? extends Throwable> arg) {
|
||||
return Arrays.<Class<? extends Throwable>> asList(arg);
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
package org.springframework.batch.core.step.item;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.core.BatchStatus;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.Step;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.support.ListItemReader;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.TransactionException;
|
||||
import org.springframework.transaction.UnexpectedRollbackException;
|
||||
import org.springframework.transaction.support.DefaultTransactionStatus;
|
||||
|
||||
/**
|
||||
* Tests for {@link FaultTolerantStepFactoryBean} with unexpected rollback.
|
||||
*/
|
||||
@ContextConfiguration(locations="classpath:/org/springframework/batch/core/repository/dao/data-source-context.xml")
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class FaultTolerantStepFactoryBeanUnexpectedRollbackTests {
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
|
||||
@Test
|
||||
public void testTransactionException() throws Exception {
|
||||
|
||||
final SkipWriterStub<String> writer = new SkipWriterStub<String>();
|
||||
FaultTolerantStepFactoryBean<String, String> factory = new FaultTolerantStepFactoryBean<String, String>();
|
||||
factory.setItemWriter(writer);
|
||||
|
||||
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(dataSource) {
|
||||
private boolean failed = false;
|
||||
protected void doCommit(DefaultTransactionStatus status) throws TransactionException {
|
||||
if (writer.getWritten().isEmpty() || failed || !isExistingTransaction(status.getTransaction())) {
|
||||
super.doCommit(status);
|
||||
return;
|
||||
}
|
||||
failed = true;
|
||||
status.setRollbackOnly();
|
||||
super.doRollback(status);
|
||||
throw new UnexpectedRollbackException("Planned");
|
||||
}
|
||||
};
|
||||
|
||||
factory.setBeanName("stepName");
|
||||
factory.setTransactionManager(transactionManager);
|
||||
factory.setCommitInterval(2);
|
||||
|
||||
ItemReader<String> reader = new ListItemReader<String>(Arrays.asList("1", "2"));
|
||||
factory.setItemReader(reader);
|
||||
|
||||
JobRepositoryFactoryBean repositoryFactory = new JobRepositoryFactoryBean();
|
||||
repositoryFactory.setDataSource(dataSource);
|
||||
repositoryFactory.setTransactionManager(transactionManager);
|
||||
repositoryFactory.afterPropertiesSet();
|
||||
JobRepository repository = (JobRepository) repositoryFactory.getObject();
|
||||
factory.setJobRepository(repository);
|
||||
|
||||
JobExecution jobExecution = repository.createJobExecution("job", new JobParameters());
|
||||
StepExecution stepExecution = jobExecution.createStepExecution(factory.getName());
|
||||
repository.add(stepExecution);
|
||||
|
||||
Step step = (Step) factory.getObject();
|
||||
|
||||
step.execute(stepExecution);
|
||||
assertEquals(BatchStatus.FAILED, stepExecution.getStatus());
|
||||
|
||||
assertEquals("[]", writer.getCommitted().toString());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user