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 doAfterReturning(Throwable throwable) {
//手动回滚
DTXUserControls.rollbackCurrentGroup();
}
}
但是方法二我还没有生效,不知道问题出在哪里,有大牛指出非常感谢