通常我們使用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();
}
}
但是方法二我還沒有生效,不知道問題出在哪里,有大牛指出非常感謝