我正在尝试在春季批处理作业中提交jms和数据库事务。我当时以为春季批处理交易是XA交易。但是在我的项目编写器中,即使jms事务出错,数据库事务也正在提交。如果我缺少任何东西,谁能帮助我。我需要在春季批处理中使用第三方库进行XA吗?
我实际上是故意抛出异常以测试事务回滚。现在,即使没有任何jms事务,即使项目写入器抛出异常,数据库事务也仍在提交。下面是writer中保存到数据库的方法。 compEvent对象是注入到此类中的jpa存储库
private void writeCEs(Map<TrueEvent, List<Event>> agentMap)
throws FailedCompensationException, Exception {
for (Entry<TrueEvent, List<Event>> 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.... ");
}
下面是我的批处理配置
@ EnableBatchProcessing@EnableTransactionManagement@组态@ComponentScan({“ com.pm. *”})]
公共类TrueBatchConfig扩展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").<List<TrueEvent>, Map<TrueUpEvent, List<Event>>>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();
}
}