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

3
  • Which transaction manager are you using? You said 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 a JpaTransactionManager for your step. Is that the case? May 26, 2020 at 9:37
  • Yes, I am using JpaTransactionManager
    – Bhaskar
    May 28, 2020 at 19:08
  • MySQL with MyISAM tables perhaps?
    – M. Deinum
    Sep 20, 2022 at 12:30

1 Answer 1

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
    }

}
1
  • 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 4, 2020 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.