我有一个包含多个步骤的春季批处理作业。
第 1 步:从数据库中加载 10 条记录。(Tasklet 完成这项工作)
第 2 步:在此处使用 ItemReader、ItemProcessor、ItemWriter 实现和 commit -interval =1 配置面向块的处理
现在据我了解,每张唱片都会发生这种情况
开始事务(读取 - 处理 - 写入)提交 Tx
我的问题是想象它处理了六条记录,现在第 7 条记录在 ItemProcessor 实现中出现异常,它尝试回滚但由于事务处于未知状态而无法回滚
即使它无法回滚第 7 条记录的 tx,它也不会处理第 8、9、10 条记录,并且作业已停止。
注意:ItemProcessor 实现正在调用服务(@Service 注释),这些服务使用 @Transactional(readOnly=false) 注释标记为事务性。
请提出解决方案。
ItemProcessor 代码如下
public Long process(LoanApplication loanApplication)throws Exception {
Long createLoan = null;
LoanStatus loanStatus = null;
StaffUser user = staffUserService.getStaffUserByName(Constants.AUTO_USER);
String notes = null;
try{
try{
loanValidationService.validate(loanApplication);
loanStatus=LoanStatus.U;
}catch(LoanValidationException e){
loanStatus=LoanStatus.UC;
notes=e.getMessage();
}
dataLoadLoanManagementService.setText(notes);
createLoan = dataLoadLoanManagementService.createLoan(loanApplication, user,loanStatus);
}catch(Exception le){
logger.error("Error creating the loan application ; Parent Application Ref : "
+ loanApplication
+ " with status as " +(loanStatus!=null ?loanStatus.getStatus():loanStatus)
+"\n"
+" School Ref :"
+ loanApplication.getSchoolRef()
+"\n"
+" Client Details :"
+loanApplication.getClientDetails()
+ "Error Details: " + ExceptionUtils.getStackTrace(le));
}
return createLoan;
}
即使配置了可跳过的异常类,这也不起作用。
为了解释更多,我在项目处理器中得到持久性异常并且我抓住了它,因此 Spring 批处理执行编写器但在编写器执行后我得到下面的异常
INFO 06-21 11:38:00 Commit failed while step execution data was already updated. Reverting to old version. (TaskletStep.java:342)
ERROR 06-21 11:38:00 Rolling back with transaction in unknown state (TaskletStep.java:351)
ERROR 06-21 11:38:00 Encountered an error executing the step (AbstractStep.java:212)
org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly