Mybatis源码之Executor

1,387 阅读11分钟

公众号搜索“码路印记”,点关注不迷路!

上一篇文章《Mybatis源码之SqlSession》聊到了SqlSession其实是一个包工头儿,揽了活自己不干都安排给了执行器;而且在《Mybatis源码之SQL执行过程》中已经了解到,执行器通过对StatementHandler生命周期的调度与管理,最终完成SQL命令执行。

今天我们就来认识一下Mybatis的四大组件之一:Executor-执行器。提前说明一下,由于Executor涉及到Mybatis的一级缓存、二级缓存,这部分内容后面会单独分析,本文先不展开。

Executor简介

Executor位于executor包,Mybatis中所有的SQL命令都由它来调度执行。首先通过UML类图认识一下Executor家族:
image.png

  • 最顶层是Executor接口,定义了查询、更新、事务、缓存操作相关的接口方法,Executor接口对外暴露,由SqlSession依赖,并受其调度与管理。
  • 第二层左侧为BaseExecutor,它是一个抽象类,实现了大部分Executor的接口。它有三个子类,分别是SimpleExecutor、ReuseExecutor、BatchExecutor。BaseExecutor及其子类完成了一级缓存管理和与数据库交互有关的操作。
  • 第二层右侧为CachingExecutor,缓存执行器,Mybatis二级缓存的核心处理类。CachingExecutor持有一个BaseExecutor的实现类(SimpleExecutor、ReuseExecutor或BatchExecutor)实例作为委托执行器。它主要完成Mybatis二级缓存处理逻辑,当缓存查询中不存在或查询不到结果时,会通过委托执行器查询数据库。
  • 第三层就是BaseExecutor的三个子类。简单执行器为默认执行器,具备执行器的所有能力;可重用执行器,是相对简单执行器而言的,它具备MappedStatement的缓存与复用能力,即在一个SqlSession会话内重复执行同一个命令,可以直接复用已缓存的MappedStatement;批量执行器,即一次可以执行多个命令。

Executor的核心功能是调度执行SQL,参与了全过程;为了提高查询性能,Mybatis在Executor中设计了一级缓存和二级缓存,一级缓存由BaseExecutor及其子类实现,二级缓存由CachingExecutor实现。一级缓存是默认开启的,二级缓存需要启用配置。由于CachingExecutor负责缓存管理,真正的数据库查询是由BaseExecutor完成的,所以对外来看,Executor有三种类型SIMPLE、REUSE、BATCH,默认是SIMPLE,我们可以在配置文件或创建SqlSession时指定参数修改默认执行器类型。以下为Mybatis中ExecutorType定义:

public enum ExecutorType {
    // 简单执行器
    SIMPLE, 
    // 可重用的执行器
    REUSE, 
    // 批处理执行器
    BATCH
}

创建过程分析

前面已经说到,SqlSession依赖Executor,Executor接受SqlSession的请求并执行,而且Executor的创建是随着SqlSession的创建而创建的。Executor的创建流程始于DefaultSqlSessionFactory#openSession()及其重载方法,代码如下:

  @Override
  public SqlSession openSession() {
    //这里使用默认的执行器类型:SIMPLE
    return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
  }

  private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
    Transaction tx = null;
    try {
      final Environment environment = configuration.getEnvironment();
      final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
      tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
      //通过configuration.newExecutor方法创建执行器
      final Executor executor = configuration.newExecutor(tx, execType);
      return new DefaultSqlSession(configuration, executor, autoCommit);
    } catch (Exception e) {
      closeTransaction(tx); // may have fetched a connection so lets call close()
      throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
    } finally {
      ErrorContext.instance().reset();
    }
  }

简单分析一下这段代码:这里采用了无参openSession方法,其内部使用Configuration#defaultExecutorType作为执行器类型调用openSessionFromDataSource方法;后者对execType未做处理调用了Configuration#newExecutor()执行Executor创建流程。

Configuration#defaultExecutorType默认值为SIMPLE,它会在Mybatis解析配置文件时被修改。若配置文件未涉及executorType的配置,默认值不会改变。另外,可以调用openSession的重载方法指定执行器类型。两种设置方式如下所示:

  • Mybatis默认(缺省)的执行器类型是SIMPLE,我们可以在配置文件中进行修改设置:
    <settings>
        <setting name="defaultExecutorType" value="SIMPLE"/>
    </settings>
  • 或者通过代码进行设置:
// 指定执行器类型为REUSE
try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.REUSE)) {
    //……    
}

了解了ExecutorType的设置方式,接着看Configuration#newExecutor()方法:

//Executor执行器创建方法
  public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
    //executorType为null:使用默认执行器,否则按照入参类型
    //看Configuration代码,其实有: protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE;
    executorType = executorType == null ? defaultExecutorType : executorType;
    //executorType和defaultExecutorType都为null,默认使用SIMPLE
    executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
    Executor executor;
    //按照executorType创建不同的执行器。
    if (ExecutorType.BATCH == executorType) {
      executor = new BatchExecutor(this, transaction);
    } else if (ExecutorType.REUSE == executorType) {
      executor = new ReuseExecutor(this, transaction);
    } else {
      executor = new SimpleExecutor(this, transaction);
    }
    //这里判断是否开启了二级缓存,默认值是true。
    //所以如果没有修改配置文件的话,这个if语句是会进入执行的
    if (cacheEnabled) {
      //创建缓存执行器,同时把上面创建的executor作为其委托执行器
      executor = new CachingExecutor(executor);
    }
    //这里是加载与Executor有关的插件。
    executor = (Executor) interceptorChain.pluginAll(executor);
    return executor;
  }

总结一下执行过程:

  • 首先:处理执行器类型参数,确保其不为空,最终以SIMPLE兜底;
  • 其次:根据执行器类型调用对应的执行器实现类做初始化,执行器可以为SimpleExecutor、ReuseExecutor、BatchExecutor中的一个;
  • 然后:如果启用了二级缓存(cacheEnabled默认是true,但是如果不设置cache的相关参数,二级缓存是不起作用的),创建缓存执行器,同时把上面创建的executor作为缓存执行器的委托执行器;
  • 最后:加载与Executor有关的插件。


默认情况下,会返回一个CachingExecutor对象,其内部包装了BaseExecutor的某个子类对象。
image.png

query流程

Executor接口中query方法有两个重载,我们从参数少的这个开始入手,它内部也会调用到另外一个。默认情况下会使用CachingExecutor,所以我们从CachingExecutor#query()开始:

  public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {
    // 获取BoundSql
    BoundSql boundSql = ms.getBoundSql(parameterObject);
    // 创建缓存key
    CacheKey key = createCacheKey(ms, parameterObject, rowBounds, boundSql);
    // 调用重载query方法
    return query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
  }

  public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql)
      throws SQLException {
    // 获取缓存对象,启用二级缓存才会有
    Cache cache = ms.getCache();
    // 缓存不空
    if (cache != null) {
      // 刷新缓存
      flushCacheIfRequired(ms);
      if (ms.isUseCache() && resultHandler == null) {
        ensureNoOutParams(ms, boundSql);
        @SuppressWarnings("unchecked")
        // 从缓存中查询
        List<E> list = (List<E>) tcm.getObject(cache, key);
        if (list == null) {
          // 缓存中没有,通过委托查询
          list = delegate.query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
          tcm.putObject(cache, key, list); // issue #578 and #116
        }
        return list;
      }
    }
    //默认情况没有开启二级缓存,会直接走到这里
    //delegate即BaseExecutor三个子类的其中一个
    return delegate.query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
  }

进入CachingExecutor#query,首先通过MappedStatement获取BoundSql,创建缓存key,然后调用了重载的query方法。重载query查询在不考虑缓存的情况下,会直接通过委托执行器的query方法进行查询。

这里的委托执行器为BaseExecutor的子类,而BaseExecutor实现了query方法,所以我们先进入BaseExecutor#query()(同样先忽略一级缓存部分的逻辑):

public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {
    ErrorContext.instance().resource(ms.getResource()).activity("executing a query").object(ms.getId());
    if (closed) {
      throw new ExecutorException("Executor was closed.");
    }
    if (queryStack == 0 && ms.isFlushCacheRequired()) {
      clearLocalCache();
    }
    List<E> list;
    try {
      queryStack++;
      //从一级缓存(本地缓存)中查询
      list = resultHandler == null ? (List<E>) localCache.getObject(key) : null;
      if (list != null) {
        handleLocallyCachedOutputParameters(ms, key, parameter, boundSql);
      } else {
        //缓存中不存在,从数据库中查询
        list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql);
      }
    } finally {
      queryStack--;
    }
    if (queryStack == 0) {
      for (DeferredLoad deferredLoad : deferredLoads) {
        deferredLoad.load();
      }
      // issue #601
      deferredLoads.clear();
      if (configuration.getLocalCacheScope() == LocalCacheScope.STATEMENT) {
        // issue #482
        clearLocalCache();
      }
    }
    return list;
  }

  private <E> List<E> queryFromDatabase(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {
    List<E> list;
    //缓存占位
    localCache.putObject(key, EXECUTION_PLACEHOLDER);
    try {
      //调用抽象方法执行数据库查询,子类实现
      list = doQuery(ms, parameter, rowBounds, resultHandler, boundSql);
    } finally {
      //移除占位
      localCache.removeObject(key);
    }
    //设置缓存
    localCache.putObject(key, list);
    if (ms.getStatementType() == StatementType.CALLABLE) {
      localOutputParameterCache.putObject(key, parameter);
    }
    return list;
  }

两个方法比较长,大部分是缓存处理。忽略缓存的情况下(直接看我注释的部分),从query方法调用了数据库查询方法queryFromDatabase,但是,真正查询的逻辑是在抽象方法doQuery中实现的,doQuery由BaseExecutor子类实现。我们依次看下子类实现逻辑:

SimpleExecutor#doQuery

  @Override
  public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
    Statement stmt = null;
    try {
      //获取配置对象
      Configuration configuration = ms.getConfiguration();
      //创建StatementHandler
      StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
      //准备Statement
      stmt = prepareStatement(handler, ms.getStatementLog());
      //执行查询
      return handler.query(stmt, resultHandler);
    } finally {
      //关闭Statement
      closeStatement(stmt);
    }
  }

  private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
    Statement stmt;
    Connection connection = getConnection(statementLog);
    stmt = handler.prepare(connection, transaction.getTimeout());
    handler.parameterize(stmt);
    return stmt;
  }

SimpleExecutor#doQuery方法代码比较简单,过程清晰明了,简单说明一下:

  • 从MappedStatement对象获取全局Configuration配置对象;
  • 调用Configuration#newStatementHandler创建StatementHandler对象;
  • 创建并初始化Statement对象;
  • 调用StatementHandler#query执行Statement,并使用resultHandler解析返回值;
  • 最后关闭Statement。


从上述流程可知,doQuery方法调度StatementHandler完成了对Statement的初始化、参数设置、执行、结果处理与关闭,是对Statement整个生命周期的管理与控制,与前文所说的Executor参与了SQL语句执行的全过程名副其实。

ReuseExecutor#doQuery

  //Statement缓存
  private final Map<String, Statement> statementMap = new HashMap<>();

  @Override
  public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
    Configuration configuration = ms.getConfiguration();
    StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
    Statement stmt = prepareStatement(handler, ms.getStatementLog());
    return handler.query(stmt, resultHandler);
  }

  private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
    Statement stmt;
    BoundSql boundSql = handler.getBoundSql();
    String sql = boundSql.getSql();
    //检查缓存中是否存在当前sql
    if (hasStatementFor(sql)) {
      //如果有,就直接拿出来用
      stmt = getStatement(sql);
      applyTransactionTimeout(stmt);
    } else {
      //如果没有,就新建一个
      Connection connection = getConnection(statementLog);
      stmt = handler.prepare(connection, transaction.getTimeout());
      //然后,缓存起来。
      putStatement(sql, stmt);
    }
    handler.parameterize(stmt);
    return stmt;
  }

ReuseExecutor#doQuery与SimpleExecutor#doQuery的逻辑基本一致,不同点在于prepareStatement方法的实现逻辑。prepareStatement使用statementMap对执行过的sql进行缓存,只有statementMap中不存在当前sql的时候才会执行创建流程,对性能有一定的提升。需要注意的是,Executor对象是SqlSession的组成部分,所以这个缓存与SqlSession的生命周期一致。

BatchExecutor#doQuery

  @Override
  public <E> List<E> doQuery(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql)
      throws SQLException {
    Statement stmt = null;
    try {
      //批量执行,目前我理解是为了把之前批量更新的语句执行掉
      flushStatements();
      //获取Configuration对象
      Configuration configuration = ms.getConfiguration();
      //创建StatementHandler
      StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameterObject, rowBounds, resultHandler, boundSql);
      //创建Statement
      Connection connection = getConnection(ms.getStatementLog());
      stmt = handler.prepare(connection, transaction.getTimeout());
      //设置Statement参数
      handler.parameterize(stmt);
      //执行并返回结果
      return handler.query(stmt, resultHandler);
    } finally {
      closeStatement(stmt);
    }
  }

BatchExecutor#doQuery方法除了多执行flushStatements方法外,与SimpleExecutor基本一致,不再展开。

update()流程

update()方法的对应了insert、update、delete等不同的命令,其执行流程与query()方法流程类似。同样是经过CachingExecutor->BaseExecutor->SimpleExecutor/ReuseExecutor/BatchExecutor。把整个过程的代码一起贴出来分析(注释):

  //org.apache.ibatis.executor.CachingExecutor#update
  @Override
  public int update(MappedStatement ms, Object parameterObject) throws SQLException {
    // 刷新缓存:开启缓存时,update命令默认情况是需要刷新缓存的
    flushCacheIfRequired(ms);
    //调用委托执行器进行update
    return delegate.update(ms, parameterObject);
  }

  //org.apache.ibatis.executor.BaseExecutor#update
  @Override
  public int update(MappedStatement ms, Object parameter) throws SQLException {
    ErrorContext.instance().resource(ms.getResource()).activity("executing an update").object(ms.getId());
    if (closed) {
      throw new ExecutorException("Executor was closed.");
    }
    // 清空缓存,然后调用子类的doUpdate方法
    clearLocalCache();
    //调用抽象方法执行数据库更新操作
    return doUpdate(ms, parameter);
  }

Mybatis的缓存机制仅对查询有效,所以Executor能做的就是:使缓存失效、请求中转,最终调用doUpdate执行数据库操作,所以:

  • CachingExecutor#update首先使二级缓存失效,然后调用委托执行器执行update操作。
  • BaseExecutor#update也是首先使得一级缓存失效,然后调用抽象方法doUpdate执行数据库的更新操作。

SimpleExecutor#doUpdate

SimpleExecutor#doUpdate与doQuery完全一致,不再说明了。

ReuseExecutor#doUpdate

ReuseExecutor#doUpdate与doQuery完全一致,不再说明了。

BatchExecutor#doUpdate

BatchExecutor的执行与前两个不一样,它用于执行批量的sql命令,所以多了一些批量准备工作。为了减少与数据库的交互次数,BatchExecutor会批量执行sql命令。代码如下:

  @Override
  public int doUpdate(MappedStatement ms, Object parameterObject) throws SQLException {
    //获取Configuration对象
    final Configuration configuration = ms.getConfiguration();
    //创建StatementHandler
    final StatementHandler handler = configuration.newStatementHandler(this, ms, parameterObject, RowBounds.DEFAULT, null, null);
    //获取sql对象
    final BoundSql boundSql = handler.getBoundSql();
    //获取sql与酒
    final String sql = boundSql.getSql();
    final Statement stmt;
    //如果当前命令与上一次执行一样,就不再重复创建Statement,性能提升
    if (sql.equals(currentSql) && ms.equals(currentStatement)) {
      //取出最后一条的索引
      int last = statementList.size() - 1;
      //取出最后一个Statement对象
      stmt = statementList.get(last);
      applyTransactionTimeout(stmt);
      //设置Statement参数
      handler.parameterize(stmt);//fix Issues 322
      //获取BatchResult
      BatchResult batchResult = batchResultList.get(last);
      //设置BatchResult参数对象
      batchResult.addParameterObject(parameterObject);
    } else {
      //如果当前命令与上一次执行不一样,重新创建
      Connection connection = getConnection(ms.getStatementLog());
      //初始化、准备Statement
      stmt = handler.prepare(connection, transaction.getTimeout());
      //设置参数
      handler.parameterize(stmt);    //fix Issues 322
      //设置当前执行的sql命令信息
      currentSql = sql;
      currentStatement = ms;
      //存起来
      statementList.add(stmt);
      //保存结果对象
      batchResultList.add(new BatchResult(ms, sql, parameterObject));
    }
    //批量处理
    handler.batch(stmt);
    return BATCH_UPDATE_RETURN_VALUE;
  }

BatchExecutor#doUpdate方法完成了Statement执行前的准备工作,在准备Statement时与上一次要执行的Statement进行对比,如果一致则不再执行重新创建Statement的流程。所以,使用BatchExecutor时应该尽量执行相同的sql命令。

但是,BatchExecutor#doUpdate并未进行数据库的执行操作,它需要通过SqlSession#flushStatements进行触发,然后调用到BatchExecutor#doFlushStatements执行最终的操作,这里就不再展开了。

Executor总结

本文从源码角度对Executor的创建流程、query流程、update流程进行了详细梳理,了解了Executor执行update/query的执行流程,重点说明了其与数据库交互的过程,即Statement的调度管理,Executor的缓存机制我们并没有展开。Executor是Mybatis四大组件之一,虽然还没有研究其他三个,但是已经对Mybatis的SQL执行过程有了整体的了解,由此可见Executor的作用举足轻重。

本文到这里就结束了,希望对您有用!本人水平有限,如您发现有任何错误或不当之处,欢迎批评指正。

公众号搜索“码路印记”,点关注不迷路!