zoukankan      html  css  js  c++  java
  • mybatis与 Exception

    mybatis将所有的异常全部包成了运行时异常,减少在高层代码中频繁的try-catch导致的代码臃肿问题。Persistence是它们共有的父类,继承自RuntimeException非检查型异常。IbatisException已经被弃用,每个异常类包装了它们对应的组件的异常信息,根据异常的类型和message我们很容易的可以定位到异常,并获取有用的帮助信息。

    另外一部分则是这个异常包装器

    Mybatis暴露的API接口(SessionBuilder,Session操作)都用这个包装器将所有的异常包装成一个PersistenceException

    public class ExceptionFactory {
    
      private ExceptionFactory() {
        // Prevent Instantiation
      }
    
      public static RuntimeException wrapException(String message, Exception e) {
        return new PersistenceException(ErrorContext.instance().message(message).cause(e).toString(), e);
      }
    
    }

    异常的message来自 ErrorContext类,它是异常信息的上线文,提供了线程内的异常信息的通信和组装,系统中的其他组件可以随时随地的将当时可能的异常上下文加入到这个ErrorContext中

    ErrorContext最重要的设计是LOCAL成员,他是个ThreadLocal对象,隔离线程间的数据,所以这个异常上下文对象是线程私有的

    ErrorContext还提供了异常信息的格式化打印工作

      @Override
      public String toString() {
        StringBuilder description = new StringBuilder();
    
        // message
        if (this.message != null) {
          description.append(LINE_SEPARATOR);
          description.append("### ");
          description.append(this.message);
        }
    
        // resource
        if (resource != null) {
          description.append(LINE_SEPARATOR);
          description.append("### The error may exist in ");
          description.append(resource);
        }
    
        // object
        if (object != null) {
          description.append(LINE_SEPARATOR);
          description.append("### The error may involve ");
          description.append(object);
        }
    
        // activity
        if (activity != null) {
          description.append(LINE_SEPARATOR);
          description.append("### The error occurred while ");
          description.append(activity);
        }
    
        // activity
        if (sql != null) {
          description.append(LINE_SEPARATOR);
          description.append("### SQL: ");
          description.append(sql.replace('
    ', ' ').replace('
    ', ' ').replace('	', ' ').trim());
        }
    
        // cause
        if (cause != null) {
          description.append(LINE_SEPARATOR);
          description.append("### Cause: ");
          description.append(cause.toString());
        }
    
        return description.toString();
      }
  • 相关阅读:
    Kooboo CMS
    Kooboo CMS
    Kooboo CMS
    Kooboo CMS
    Kooboo CMS
    Ajax.BeginForm VS Html.BeginForm
    ASP.NET或者 js方式实现文件夹多图片浏览的方式
    C++入门知识总结(1)
    几个步骤轻松搞定ASP.NET 依赖注入。
    使用GIT@OSChina 实现协同工作的方法。
  • 原文地址:https://www.cnblogs.com/cookiezhi/p/6246666.html
Copyright © 2011-2022 走看看