@Transactional 注解 事务在service层 for循环内调用方法失败多个表操作不回滚解决方式

举例 :service层方法

for(int i= 0;i<userList.size();i++){
   try{
       //循环体内事务必须另开一个类
       writeOne(userList.get(i),customerList.get(i),customerPersonList.get(i));
       }catch (Exception e) {
            User userFile = new User();
            userFile.setPhoneNo(userList.get(i).getPhoneNo());
            userFile.setIdentityCardNo(userList.get(i).getIdentityCardNo());
            userFileList.add(userFile);
       }
}

@Transactional(propagation=Propagation.REQUIRES_NEW)
public void writeOne(User user, Customer customer, CustomerPerson customerPerson){

         try{
              userService.insert(user);
              customerService.insert(customer);
              customerPersonService.insert(customerPerson);
              }catch (Exception e) {
                   logger.info("user= {},customer= {},customerPerson= {}",user, customer,customerPerson);
                   throw new BusinessException(ResponseStatusEnum.DATABASE_ERROR);
              }

}

 

 

如果在一个类下 失败事务不会回滚    把writeOne 这个方法单独创建一个类  则可以   10条数据 如果第8条失败  则第8条数据回滚  其他不影响执行    

posted @ 2017-06-02 16:54  夜色幽幽  阅读(2602)  评论(0编辑  收藏  举报