Transaction is not rolling back in spring batch. I am throwing exception intentionally to test transaction roll back. Database transaction is committing even with exception thrown from the item writer.Below is the method in writer which is saving into DB. compEvent object is jpa repository injected into this class
private void writeCEs(Map> agentMap) throws FailedCompensationException, Exception { for (Entry> agent : agentMap.entrySet()) { agent.getValue().stream().forEach((ce) -> { compEvent.save(ce); }); updateRecordFileStatus(agent.getKey()); //postToAccounting(agent.getKey(), agent.getValue()); } throw new Exception("Testing XA roolback.... "); }
I tried marking transaction for the above method with @Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class) Still it is committing the transaction. I am sure I am missing something. any help would be appreciated.
Below is my batch configuration
@EnableBatchProcessing @EnableTransactionManagement @Configuration @ComponentScan({ "com.pm.*" }) public class TrueBatchConfig extends DefaultBatchConfigurer { @Autowired private JobBuilderFactory jobs; @Autowired private StepBuilderFactory steps; @Autowired EventReader reader; @Autowired private EventProcessor processor; @Autowired private EventWriter writer; @Bean protected Step step1(ThreadPoolTaskExecutor executor) { DefaultTransactionAttribute attribute = new DefaultTransactionAttribute(); attribute.setPropagationBehavior(Propagation.REQUIRED.value()); attribute.setIsolationLevel(Isolation.DEFAULT.value()); attribute.setTimeout(30); return steps.get("step1")., Map>>chunk(10).reader(reader) .processor(processor).writer(writer).transactionAttribute(attribute).build(); } @Bean(name = "firstBatchJob") public Job job(@Qualifier("step1") Step step1) { return jobs.get("firstBatchJob").start(step1).build(); } }
compEvent object is jpa repository
so it looks like you are using JPA. In that case, I'm expecting you are configuring Spring Batch to use aJpaTransactionManager
for your step. Is that the case? – Mahmoud Ben Hassine