try catch之后依然发生事务回滚

参考:https://blog.csdn.net/lilizhou2008/article/details/106110401/

 

总结:

查看源码,显示catch中的代码被spring 动态代理,原因是本方法和catch中的方法都被同一事务管理:@Transactional(rollbackFor = Exception.class)

 

示例:

@Transactional
public class test156{
    @Autowired
    UserCenterService userCenterService;

    public Object getUserName (){
        try {
            Map<String, Object> userInfo = userCenterService.getInfoByIpa(String ipa);
            if (userInfo != null) {
                return userInfo.get("name");
            } else {
                return null;
            }
        } catch (Exception ex) {
            log.info("user center has exception..."+ex);
        }
        return null;
    }

}

@Transactional
public class UserCenterServiceImpl implements UserCenterService{
    public String getInfoByIpa(String ipa) {
        // get result by other server
        return doPostMethod(requestType, url, params, headerMap)
    }
}

 

以上情况,在 test156 中的catch并不会生效返回null, 而是会抛出异常,参考继承性事务。

 

posted @ 2021-02-04 10:10  冬凛  阅读(1262)  评论(0)    收藏  举报