【本人秃顶程序员】SpringMVC+Mybatis+事务回滚+异常封装返回

←←←←←←←←←←←← 快,点关注!

问题的背景:

  1. 使用dubbo搭建分布式服务架构,service的实现,采用SpringMVC4.1.6+MyBatis3.2.8。
  2. 为了少维护一个维度,拟对service接口进行通用性定义,即让业务的变化,不影响已定义的service接口。

最终Service的方法签名定义如下(示例):

public ServiceResult addProduct(ServiceParam param)

其中ServiceResult定义如下:

public class ServiceResult<T> {
 private T result;
 /**
  * 错误返回code 
  */
 private int retCode;
 public int getRetCode() {
  return retCode;
 }
 public void setRetCode(int retCode) {
  this.retCode = retCode;
 }
 public T getResult() {
  return result;
 }
 public void setResult(T result) {
  this.result = result;
 }
} 

其中ServiceParam定义如下:

public class ServiceParam<T> {
 private T param;

 public T getParam() {
  return param;
 }

 public void setParam(T param) {
  this.param = param;
 }
}  

此方法签名保证远程调用两端的业务变化,不会对此接口造成影响。实现了运维维度减一。

其中,ServiceResult封装了所有的业务和运行异常,将之转化为errorCode返回。如此便减少了异常的远程传输的消耗,同时隔离了调用两端的异常耦合,也避免了调用方在嵌套的异常捕捉块里进行业务处理的尴尬。

但是,在实际的框架搭建中,却发现了这样的一个问题,addProduct方法的事务,如果通过Spring来管理,则必须要抛异常来进行事务的回滚。如果方法抛了异常(对调用者来说,就是远程异常),则不能满足我们把异常转化到errorCode来输出的目的。

因此,我们必须想办法去把异常捕获转化成errorCode,同时又不能影响事务回滚。经过多次尝试,终于圆满解决该问题。特此记录分享于此。

下面是我的思路及解决办法:

既然使用的是Spring来管理事务,则就用AOP来拦截相关Service方法,在事务回滚过后,继续捕捉到异常,对该异常进行转化输出。

第一步,建立AOP拦截类

@Aspect
@Component
@Order(0)
public class ExceptionAspect {
 /**
  * 配置切入点,该方法无方法体,主要为方便同类中其他方法使用此处配置的切入点 
  */
 @Pointcut("execution(* cn.xx.dubbo.security.service..*(..))")
 public void aspect() {
 }

 @Around("aspect()")
 public ServiceResult around(JoinPoint joinPoint) {
  System.out.println("===============START");
  ServiceResult result;
  try {
   //调用拦截的方法主体  
   result = (ServiceResult) ((ProceedingJoinPoint) joinPoint).proceed();
  } catch (Throwable e) {
   System.out.println("出现了异常:" + e.getMessage());
   result = new ServiceResult();
   result.setResult("exception:" + e.getMessage());
   result.setRetCode(-1);
  }

  System.out.println("===============END");
  return result;
 }
}  

在该类中,定义切面执行的顺序Order(0),即最外层。

第二步,使用注解对service服务方法进行事务定义,并在service方法抛出业务异常,以便使得事务回滚。

@Override
@Transactional
public ServiceResult addProduct(ServiceParam<Product> param) 
throws Exception {
 Product product = param.getParam();
 int rlt = biz.addProduct(product);
 if(rlt == 0)
  throw new Exception("操作失败!");
 else {
  ServiceResult<Integer> result = new ServiceResult();
  result.setResult(rlt);
  return result;
 }
}  

第三步,修改Spring配置文件
使用注解式事务声明,加入order属性,使其大于0,则优先于自定义的AOP类执行事务

<tx:annotation-driven transaction-manager="transactionManager"
  proxy-target-class="true" order="10"/>  

最后用Junit测试该service方法,即便该方法抛异常,最外层的ExceptionAspect 也会拦截到该异常,在事务正确回滚之后,把异常转化为errorCode,封装到ServiceResult中成功返回,改造成功!

欢迎大家加入粉丝群:963944895,群内免费分享Spring框架、Mybatis框架SpringBoot框架、SpringMVC框架、SpringCloud微服务、Dubbo框架、Redis缓存、RabbitMq消息、JVM调优、Tomcat容器、MySQL数据库教学视频及架构学习思维导图

猜你喜欢

转载自blog.csdn.net/weixin_44175121/article/details/86084493
今日推荐