MyBatis

MyBatis Executor 对比分析

2017-11-13  本文已影响0人  wuyong和大叔

PS:该篇内容基于 mybatis 3.4.4 版本 , 数据库基于 mysql 5.6

Executor 类图

主要分析 SimpleExecutor , ReuseExecutor , BatchExecutor , CachingExecutor 的不同点,以及实际使用中该如何选择。


image.png

BaseExecutor

BaseExecutor 主要是使用了模板设计模式(template), 共性被封装在 BaseExecutor 中 , 容易变化的内容被分离到了子类中 。

SimpleExecutor

MyBatis 的官方文档中对 SimpleExecutor 的说明是 "普通的执行器" , 普通就在于每一次执行都会创建一个新的 Statement 对象 。下面看一下 mybatis 创建 Statement 对象的代码 :

// BaseStatementHandler 中的方法
  @Override
  public Statement prepare(Connection connection, Integer transactionTimeout) throws SQLException {
    ErrorContext.instance().sql(boundSql.getSql());
    Statement statement = null;
    try {
      statement = instantiateStatement(connection);
      setStatementTimeout(statement, transactionTimeout);
      setFetchSize(statement);
      return statement;
    } catch (SQLException e) {
      closeStatement(statement);
      throw e;
    } catch (Exception e) {
      closeStatement(statement);
      throw new ExecutorException("Error preparing statement.  Cause: " + e, e);
    }
  }
  
// PreparedStatementHandler 中的方法
  @Override
  protected Statement instantiateStatement(Connection connection) throws SQLException {
    String sql = boundSql.getSql();
    if (mappedStatement.getKeyGenerator() instanceof Jdbc3KeyGenerator) {
      String[] keyColumnNames = mappedStatement.getKeyColumns();
      if (keyColumnNames == null) {
        return connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
      } else {
        return connection.prepareStatement(sql, keyColumnNames);
      }
    } else if (mappedStatement.getResultSetType() != null) {
      return connection.prepareStatement(sql, mappedStatement.getResultSetType().getValue(), ResultSet.CONCUR_READ_ONLY);
    } else {
      return connection.prepareStatement(sql);
    }
  }

在眼见为实了 mybatis 创建 Statement 对象的代码后我们可以看SimpleExecutor 的代码 , 每次调用时都创建了一个新的 Statement 对象:

 // 实现 BaseExecutor 中的抽象方法
  @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 handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
      stmt = prepareStatement(handler, ms.getStatementLog());
      return handler.<E>query(stmt, resultHandler);
    } finally {
      closeStatement(stmt);
    }
  }

  // 获取一个 Statement , 这里的 handler 默认使用的是 
 // org.apache.ibatis.executor.statement.PreparedStatementHandler
  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;
    }

ReuseExecutor

官方文档中的解释是“执行器会重用预处理语句(prepared statements)” , 这次倒是解释的很详细。也就是说不会每一次调用都去创建一个 Statement 对象 , 而是会重复利用以前创建好的(如果SQL相同的话),这也就是在很多数据连接池库中常见的 PSCache 概念 。但是ReuseExecutor的PSCache 范围只能存在于一次回话中 , 因为每一次回话内部都会使用一个新的 ReuseExecutor 对象 , 所以 mybatis 的 PSCache 作用十分有限。

  // 存储 SQL 语句对应的 Statement 对象
  private final Map<String, Statement> statementMap = new HashMap<String, Statement>();

  // .....

  private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
    Statement stmt;
    BoundSql boundSql = handler.getBoundSql();
    String sql = boundSql.getSql();
    // 检查该 sql 是否有可用的 Statement , 如果有的话直接从缓存中获取 , 没有的话创建新的 Statement 并缓存
    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;
  }

SimpleExecutor & ReuseExecutor 对比总结

SimpleExecutor 比 ReuseExecutor 的性能要差 , 因为 SimpleExecutor 没有做 PSCache。为什么做了 PSCache 性能就会高呢 , 因为当SQL越复杂占位符越多的时候预编译的时间也就越长,创建一个 PreparedStatement 对象的时间也就越长。测试代码 :

public class PrepareStatementTest {

    private static final Logger LOG = LoggerFactory.getLogger(PrepareStatementTest.class);
    private static final Properties PROP = new Properties();
    private static final int COUNT = 2000000;

    private String driverClass;
    private String url;
    private String user;
    private String password;

    private static final StringBuilder TEST_PREPAREED_STATEMENT_SQL = new StringBuilder();
    private long startTime;

    @Before
    public void init() throws Exception {

        PROP.load(PrepareStatementTest.class.getClassLoader().getResourceAsStream("jdbc.properties"));
        this.driverClass = PROP.getProperty("jdbc.driver");
        this.url = PROP.getProperty("jdbc.url");
        this.user = PROP.getProperty("jdbc.user");
        this.password = PROP.getProperty("jdbc.password");
        DriverManager.registerDriver((Driver) Class.forName(this.driverClass).newInstance());

        TEST_PREPAREED_STATEMENT_SQL.append("INSERT INTO `user` (nickname,realname,phone,login_password,pay_password,create_time,update_time) VALUES");
        for (int i = 0 ; i < COUNT ; i++ ) {
            if (i == COUNT - 1) {
                TEST_PREPAREED_STATEMENT_SQL.append(" (?,?,?,?,?,?,?)");
            } else {
                TEST_PREPAREED_STATEMENT_SQL.append(" (?,?,?,?,?,?,?)").append(", ");
            }
        }

        this.startTime = System.currentTimeMillis();
    }

    @After
    public void after() throws Exception {
        LOG.debug("======> cost time {} ms" , (System.currentTimeMillis() - this.startTime));
    }

    @Test
    public void costTime() throws Exception {
        Connection connection = DriverManager.getConnection(url, user, password);
        PreparedStatement ps = connection.prepareStatement(TEST_PREPAREED_STATEMENT_SQL.toString());

    }
}

BatchExecutor

BatchExecutor 的特性其实非常简单,其实就是调用了 Statement 的 addBatch 方法。另外千万不要认为 BatchExecutor 比 ReuseExecutor 功能强大性能高 , 实际上不是的 BatchExecutor 是没有做 PSCache 的。

  @Override
  public int doUpdate(MappedStatement ms, Object parameterObject) throws SQLException {
    final Configuration configuration = ms.getConfiguration();
    final StatementHandler handler = configuration.newStatementHandler(this, ms, parameterObject, RowBounds.DEFAULT, null, null);
    final BoundSql boundSql = handler.getBoundSql();
    final String sql = boundSql.getSql();
    final Statement stmt;
    if (sql.equals(currentSql) && ms.equals(currentStatement)) {
      int last = statementList.size() - 1;
      stmt = statementList.get(last);
      applyTransactionTimeout(stmt);
     handler.parameterize(stmt);//fix Issues 322
      BatchResult batchResult = batchResultList.get(last);
      batchResult.addParameterObject(parameterObject);
    } else {
      Connection connection = getConnection(ms.getStatementLog());
      stmt = handler.prepare(connection, transaction.getTimeout());
      handler.parameterize(stmt);    //fix Issues 322
      currentSql = sql;
      currentStatement = ms;
      statementList.add(stmt);
      batchResultList.add(new BatchResult(ms, sql, parameterObject));
    }
  // handler.parameterize(stmt);
    handler.batch(stmt);
    return BATCH_UPDATE_RETURN_VALUE;
  }

BatchExecutor 与 SimpleExecutor 和 ReuseExecutor 还有一个区别就是 , BatchExecutor 的事务是没法自动提交的。因为 BatchExecutor 只有在调用了 SqlSession 的 commit 方法的时候 , 它才会去执行 executeBatch 方法。

  // BaseExecutor 的 commit 方法
  public void commit(boolean required) throws SQLException {
    if (closed) throw new ExecutorException("Cannot commit, transaction is already closed");
    clearLocalCache();
    flushStatements();
    if (required) {
      transaction.commit();
    }
  }

// BatchExecutor 中的 doFlushStatements 方法
@Override
  public List<BatchResult> doFlushStatements(boolean isRollback) throws SQLException {
    try {
      List<BatchResult> results = new ArrayList<BatchResult>();
      if (isRollback) {
        return Collections.emptyList();
      }
      for (int i = 0, n = statementList.size(); i < n; i++) {
        Statement stmt = statementList.get(i);
        applyTransactionTimeout(stmt);
        BatchResult batchResult = batchResultList.get(i);
        try {
          // 这里才调用了 executeBatch 方法
          batchResult.setUpdateCounts(stmt.executeBatch());
          MappedStatement ms = batchResult.getMappedStatement();
          List<Object> parameterObjects = batchResult.getParameterObjects();
          KeyGenerator keyGenerator = ms.getKeyGenerator();
          if (Jdbc3KeyGenerator.class.equals(keyGenerator.getClass())) {
            Jdbc3KeyGenerator jdbc3KeyGenerator = (Jdbc3KeyGenerator) keyGenerator;
            jdbc3KeyGenerator.processBatch(ms, stmt, parameterObjects);
          } else if (!NoKeyGenerator.class.equals(keyGenerator.getClass())) { //issue #141
            for (Object parameter : parameterObjects) {
              keyGenerator.processAfter(this, ms, stmt, parameter);
            }
          }
        } catch (BatchUpdateException e) {
          StringBuilder message = new StringBuilder();
          message.append(batchResult.getMappedStatement().getId())
              .append(" (batch index #")
              .append(i + 1)
              .append(")")
              .append(" failed.");
          if (i > 0) {
            message.append(" ")
                .append(i)
                .append(" prior sub executor(s) completed successfully, but will be rolled back.");
          }
          throw new BatchExecutorException(message.toString(), e, results, batchResult);
        }
        results.add(batchResult);
      }
      return results;
    } finally {
      for (Statement stmt : statementList) {
        closeStatement(stmt);
      }
      currentSql = null;
      statementList.clear();
      batchResultList.clear();
    }
  }

CachingExecutor

再回头去看一下 Executor 组件的类结构图 , 发现 CachingExecutor 没有 extends BaseExecutor , 为什么?一定有原因这个世界上没有无缘无故的事情。看看 CachingExecutor 的实现,发现 CachingExecutor 其实是一个装饰者对象 , mybatis 这里对 Executor 的设计使用了 Decorator (装饰者) 设计模式。

public class CachingExecutor implements Executor {

  private Executor delegate;
  private TransactionalCacheManager tcm = new TransactionalCacheManager();

  public CachingExecutor(Executor delegate) {
    this.delegate = delegate;
    delegate.setExecutorWrapper(this);
  }

接下来说明 CachingExecutor 这个装饰者对象的作用 ,看名字的话也能猜的差不多了 , 这个装饰对象是用来处理二级缓存的。 当全局设置开启了二级缓存时会初始化一个 CachingExecutor 。

  // org.apache.ibatis.session.Configuration 中的方法
  public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
    executorType = executorType == null ? defaultExecutorType : executorType;
    executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
    Executor executor;
    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);
    }
    
    // 如果开启了二级缓存 , 实例化 CachingExecutor 对象
    if (cacheEnabled) {
      executor = new CachingExecutor(executor);
    }
    executor = (Executor) interceptorChain.pluginAll(executor);
    return executor;
  }

// CachingExecutor
@Override
  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, parameterObject, boundSql);
        @SuppressWarnings("unchecked")
        List<E> list = (List<E>) tcm.getObject(cache, key);
        if (list == null) {
          list = delegate.<E> query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
          tcm.putObject(cache, key, list); // issue #578 and #116
        }
        return list;
      }
    }
    return delegate.<E> query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
  }

总结

实际生产环境中建议使用 ReuseExecutor , 另外在实际应用中涉及到大量数据的更新,插入操作不建议使用 mybatis 而应该使用原生的 JDBC 操作 , 因为数据量很大的时候进行一次 executeBatch 也是很耗时的 , 使用原生 JDBC 操作可以 clearBatch 和 executeBatch 结合使用提高性能 ; 通过对源码的分析,了解到了各个Executor实现的优劣 , 它们之间的组织与协同关系 , 希望阅读源码不仅仅是熟悉了框架的运行流程,实现原理,更能体会作者的思想,分析出框架的优点缺点 , 结合实际为自己所用。站在巨人的肩膀上能看的更远。

上一篇下一篇

猜你喜欢

热点阅读