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, 而是會拋出異常,參考繼承性事務。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM