0

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

Clark Ngo
  • 343
  • 4
  • 14
Bhaskar
  • 11
  • 3

1 Answers1

0

According to your configuration, Spring Batch is not configured to use your JpaTransactionManager, you need to override getTransactionManager, see example here. In your case it should be something like:

public class TrueBatchConfig extends DefaultBatchConfigurer {

   // .. 

    @Override
    public PlatformTransactionManager getTransactionManager() {
        return new JpaTransactionManager(); // TODO set entity manager factory
    }

}
Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50
  • I have separate trasaction manager for db and trasaction manager looks like this. @Bean public PlatformTransactionManager trueDSTransactionManager( @Qualifier("trueDSFactory") EntityManagerFactory batchCommEntityManagerFactory) { return new JpaTransactionManager(batchCommEntityManagerFactory); } Actually, this problem got solved by creating a separate service and calling this service from writer. I included all my writes in that service. Now I see trasaction is rolling back. Event XA transaction is also working with separate service. – Bhaskar Jun 04 '20 at 18:22