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
    评论
在Spring Boot,使用@Transactional注解来实现声明式事务管理是非常方便的。然而,有几个可能导致@Transactional注解无效的原因。 首先,根据引用\[1\],如果在捕获异常的同时抛出了异常,事务将不会回滚。这意味着在异常处理过程,需要确保不再抛出异常,以便事务能够正常回滚。 其次,根据引用\[2\],如果@Transactional注解应用在非public修饰的方法上,事务将会失效。这意味着@Transactional注解应该应用在public修饰的方法上,以确保事务能够正确地被管理。 此外,根据引用\[3\],如果@Transactional注解应用在protected或private修饰的方法上,事务虽然无效,但不会报错。这是一个容易犯错的地方,需要注意。 综上所述,如果在Spring Boot使用@Transactional注解无效,可以检查是否在异常处理过程抛出了异常,以及是否将注解应用在public修饰的方法上。 #### 引用[.reference_title] - *1* [SpringBoot @Transactional 事务 注解 失效](https://blog.csdn.net/tianshi527/article/details/111236464)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [springboot使用@Transactional注解事物不生效的原因(数据库事务隔离机制以及传播机制传播行为)](https://blog.csdn.net/zy103118/article/details/122296318)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [springboot 注解@Transactional失效的原因](https://blog.csdn.net/kang1011/article/details/121451324)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值