Propagation.REQUIRES_NEW无法正常运作

时间:2019-05-15 11:26:59

标签: spring-boot spring-data-jpa spring-transactions

我有以下情况。      我有一个交易方法调用另一个具有REQUIRED_NEW传播的交易方法。如果第一个方法发生异常,则第二个方法(REQUIRED_NEW Propagation)也会回滚。

我正在使用JPA,Spring-boot和chainedKakfkaTransactionManager

我尝试过将chainedKakfkaTransactionManager更改为默认值,但仍然没有运气

这是我的代码:

@Service
@Transactional(readOnly = false)
public class ComponentServiceImpl implements ComponentService {
@Autowired
private UserRepository userRepository ; 
@Override
@Transactional
public boolean validateName(String name) {
try{
  retrun userRepository.validate(name);  
  }catch(Exception e){
   handleError(name);
   throw new Exception("user not valid");
  }
}
@Override
@Transactional(propagation=Propagation.REQUIRES_NEW)
public boolean handleError(String name) {
          userRepository.update(name);
}
}

handleError方法中也会发生回滚。有代码错误吗?

1 个答案:

答案 0 :(得分:0)

感谢@DarrenForsythe,

通过为相同的类(自引用)创建一个自动装配对象,对我有用

    @Service
    @Transactional(readOnly = false)
    public class ComponentServiceImpl implements ComponentService {
    @Autowired
    private UserRepository userRepository ; 

// CREATE SELF REFRENCE
    @Autowired
    private ComponentService componentService;

    @Override
    @Transactional
    public boolean validateName(String name) {
    try{
      retrun userRepository.validate(name);  
      }catch(Exception e){
       componentService.handleError(name);
       throw new Exception("user not valid");
      }
    }
    @Override
    @Transactional(propagation=Propagation.REQUIRES_NEW)
    public boolean handleError(String name) {
              userRepository.update(name);
    }
    }