springboot @Transactional 在controller中无效的问题,未解决

6 篇文章 0 订阅
2 篇文章 0 订阅

参考文章:
https://blog.csdn.net/fubaojiexing/article/details/79057639
https://blog.csdn.net/Dongguabai/article/details/80788585
https://www.jianshu.com/p/00758c77bf60
https://note.youdao.com/ynoteshare1/index.html?id=4eab4c614ec0825fdcf6f6251f47161e&type=note
https://blog.csdn.net/u010963948/article/details/79208328

主要的失效原因有以下几种:
1.spring会对unchecked异常进行事务回滚;如果是checked异常则不回滚。
java里面将派生于Error或者RuntimeException(比如空指针,1/0)的异常称为unchecked异常,
其他继承自java.lang.Exception得异常统称为Checked Exception,如IOException、TimeoutException等
你的异常类型是不是unchecked异常。
如果check异常也想回滚怎么办,注解上面写明异常类型即可。
@Transactional(rollbackFor=Exception.class)
2. @Transactional 注解标注的方法不是public类型的。
3. 代理类的原因。

我想在controller层调用两个service 事务方法,失败。
由于我用的dubbo 微服务,controller层和service层是两个独立的服务。controller层未配置数据源, 事务不起作用的原因未找到,可能跟数据未配置有关系,工作时间紧,做个笔记以后有机会再查。。。。

service层事务处理正常,代码如下:


```java
 /**
     * 添加快递单 返回记录id
     * rollbackFor = Exception.class 抛出 Exception 时回滚事物,不需要手动回滚
     *
     * @param snWaybillInfoEntity
     * @return
     * @throws PSBCommonException
     */
    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int insterWaybillSubscribeInfo(SNWaybillInfoEntity snWaybillInfoEntity, List<TemInfoitem> temInfoitems) throws PSBCommonException {
        log.info("====== 添加快递单,入参:" + JSON.toJSONString(snWaybillInfoEntity));
        Integer result = 0;
        try {
            result = waybillSubscribeDao.insterWaybillSubscribeInfo(snWaybillInfoEntity);
            if (result == 0) {
                throw new PSBCommonException(RestErrorCodeConstants.STATUS_SYSTEM_ERROR, RestErrorCodeConstants.STATUS_SYSTEM_ERROR_INFO + ",插入快递单失败!");
            }
            if (null != temInfoitems && temInfoitems.size() > 0) {
                //设置快递单ID
                for (TemInfoitem temInfoitem : temInfoitems) {
                    temInfoitem.setWaybillId(snWaybillInfoEntity.getId());
                }
                result = waybillSubscribeDao.insterWaybillTemInfoitems(temInfoitems);
                if (result == 0) {
                    throw new PSBCommonException(RestErrorCodeConstants.STATUS_SYSTEM_ERROR, RestErrorCodeConstants.STATUS_SYSTEM_ERROR_INFO + ",插入商品信息失败!");
                }
                throw new SQLException();
            }
        } catch (SQLException e) {
            // 抛出异常回滚事物,不需要手动回滚
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            log.info("====== 添加快递单失败!");
            log.error(e);
            throw new PSBCommonException(RestErrorCodeConstants.SQL_ERROR_CODE, RestErrorCodeConstants.SQL_ERROR_INFO);
        }
        log.info("====== 添加快递单,出参,添加记录id:" + snWaybillInfoEntity.getId());
        return snWaybillInfoEntity.getId();
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值