SpringBoot统一异常处理后TX-LCN分布式事务无法捕获异常进行回滚

通常我们使用SpringBoot都会进行统一异常处理,例如写一个BaseController,在BaseController里进行统一异常处理,然后其他的Controller都继承BaseController。

当使用tx-lcn做分布式事务时,如果某个服务抛出了异常,然后该异常又被统一异常处理处理掉了,那么分布式事务将不会回滚。
方法一:

将服务中会被远程RPC调用的接口不进行统一异常处理,那么tx-lcn将会捕获到异常从而进行回滚。

方法二:

在AOP中拦截异常,如果出现异常了则进行手动回滚。

import com.codingapi.txlcn.tc.support.DTXUserControls;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
* @author liuziw
* @date 2019/10/30 16:24
*/
@Component
@Aspect
@Slf4j
public class TxAspect {

@Pointcut("@annotation(com.codingapi.txlcn.tc.annotation.LcnTransaction)") //只需拦截使用了LcnTransaction注解的方法
    public void txAnnotation(){

}

@AfterThrowing(throwing = "throwable", pointcut = "txAnnotation()")
public void doAfterReturning(Throwable throwable) {
//手动回滚
DTXUserControls.rollbackCurrentGroup();
}

}
但是方法二我还没有生效,不知道问题出在哪里,有大牛指出非常感谢

posted on   风再起时9302  阅读(2104)  评论(0编辑  收藏  举报

编辑推荐:
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
阅读排行:
· 2025成都.NET开发者Connect圆满结束
· 后端思维之高并发处理方案
· 千万级大表的优化技巧
· 在 VS Code 中,一键安装 MCP Server!
· 10年+ .NET Coder 心语 ── 继承的思维:从思维模式到架构设计的深度解析
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示