Mybatis 执行器,执行一个sql分这么多种类型

1,265 阅读6分钟

Executor 执行器

今天分享一下 Executor。它在框架中是具体sql的执行器,sqlSession(门面模式)封装通用的api,把具体操作委派给 Executor 执行,Executor协同BoundSql,StatementHandler,ParameterHandler 和 ResultSetHandler 完成工作。

它使用装饰器的方式组织 Executor 对象。如 CachingExecutor 装饰了SimpleExecutor 提供二级缓存功能。

可以通过插件机制扩展功能。mybatisplus 就是通过插件机制扩展的功能。

下面是更新流程,Executor 处于流程中间蓝色部分,缓存执行器,基础执行器,简单执行器三个 Executor 通过责任链的方式组织起来,各司其职,一同完成执行工作。,可以感受到它的作用是承上启下。

更新流程图片来自 http://coderead.cn/

执行器介绍

Executor 的继承关系

Mybatis 一共提供了四种执行器的实现和一个模板类:

  • 基础执行器 BaseExecutor:实现Executor接口的抽象类,实现了框架逻辑,具体的逻辑委派给子类实现。一级缓存也是在这里实现的。
  • 缓存执行器 CachingExecutor:实现了二级缓存,是jvm级别的全局缓存。
  • 简单执行器 SimpleExecutor:继承自 BaseExecutor,具体执行逻辑的实现。
  • 重用执行器 ReuseExecutor:相同的 sql 只会预编译一次。
  • 批处理执行器 BatchExecutor:批处理执行器 使用 JDBC 的batch API 执行 SQL 的批量操作,如insert 或者 update。select的逻辑和 SimpleExecutor 的实现一样。

今天介绍 SimpleExecutor,ReuseExecutor 和 BatchExecutor 三个执行器的特定和逻辑, CachingExecutor 的功能是提供二级缓存,暂时不在这里介绍。

SimpleExecutor

简单执行器顾名思义,处理的逻辑比较简单直接,来一个 sql 预编译一个,处理一个。 示例代码如下:

// 创建 SimpleExecutor 
SimpleExecutor simpleExecutor = new SimpleExecutor(sessionFactory.getConfiguration(),
jdbcTransaction);
// 获取 MappedStatement 
final MappedStatement ms = sessionFactory.getConfiguration().getMappedStatement("example.mapper.UserMapper.getUserByID");
final BoundSql boundSql = ms.getBoundSql(1);
// 执行 2 次查询
simpleExecutor.doQuery(ms, 1, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER, boundSql);
simpleExecutor.doQuery(ms, 1, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER, boundSql);

执行结果:


[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==>  Preparing: select * from `user` where id = ? 
[DEBUG][main] m.p.ThresholdInterceptor.intercept ThresholdInterceptor plugin... 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==> Parameters: 1(Integer) 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug <==      Total: 1 

[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==>  Preparing: select * from `user` where id = ? 
[DEBUG][main] m.p.ThresholdInterceptor.intercept ThresholdInterceptor plugin... 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==> Parameters: 1(Integer) 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug <==      Total: 1 

通过日志看到,虽然执行相同的 sql 但是每次都要执行预编译。这是一个需要优化的点。

ReuseExecutor

ReuseExecutor 对相同 SQL 重复编译做了优化,相同的 sql 的 Statement 只创建一个。

示例代码上面一样,只是把 SimpleExecutor 换成 ReuseExecutor 。 从执行我们看到,Preparing 只有一次,执行结果也是正确的:

[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==>  Preparing: select * from `user` where id = ? 
[DEBUG][main] m.p.ThresholdInterceptor.intercept ThresholdInterceptor plugin... 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==> Parameters: 1(Integer) 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug <==      Total: 1 

[DEBUG][main] m.p.ThresholdInterceptor.intercept ThresholdInterceptor plugin... 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==> Parameters: 1(Integer) 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug <==      Total: 1 

他是怎么做到的呢?翻开代码看看实现,其实逻辑也很简单,用 SQL 当作 key 保存对应的 Statement 来实现重用。

  private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
    Statement stmt;
    BoundSql boundSql = handler.getBoundSql();
    String sql = boundSql.getSql();
    // 关键逻辑,通过 sql 判断是否已经创建了 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;
  }
  private final Map<String, Statement> statementMap = new HashMap<>();
  private boolean hasStatementFor(String sql) {
    try {
      Statement statement = statementMap.get(sql);
      return statement != null && !statement.getConnection().isClosed();
    } catch (SQLException e) {
      return false;
    }
  }

BatchExecutor

有些场景下,我们要批量保存或者删除,更新数据,这时候我们一条一条的执行效率就会很低,需要一个批量执行的机制。

JDBC 批量操作

批量操作可以把相关的sql打包成一个 batch,一次发送到服务器,减少和服务器的交互,也就是 RTT 时间。

使用批量操作前要确认服务器是否支持批量操作,可通过 DatabaseMetaData.supportsBatchUpdates() 方法的返回值来判断。

实例代码,通过 JDBC 提供的 API 执行批量操作。

Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);

DatabaseMetaData metaData = conn.getMetaData();
System.out.println("metaData.supportsBatchUpdates() = " + metaData.supportsBatchUpdates());

//执行 sql
System.out.println("Creating statement...");
String sql = "update user set name=? where id = ?";
pstmt = conn.prepareStatement(sql);

// 设置变量
pstmt.setString(1, "Pappu");
pstmt.setInt(2, 1);
// 添加到 batch
pstmt.addBatch();

// 设置变量
pstmt.setString(1, "Pawan");
pstmt.setInt(2, 2);
// 添加到 batch
pstmt.addBatch();

//执行,并获取结果
int[] count = pstmt.executeBatch();

Mybatis 如何实现

Mybatis 只有对 update 有支持批量操作,并且需要手动 flushStatements。

insert、delete、update,都是update操作

    BatchExecutor batchExecutor = new BatchExecutor(configuration, jdbcTransaction);

    final MappedStatement update = configuration
        .getMappedStatement("dm.UserMapper.updateName");
    final MappedStatement delete = configuration
        .getMappedStatement("dm.UserMapper.deleteById");
    final MappedStatement get = sessionFactory.getConfiguration()
        .getMappedStatement("dm.UserMapper.getUserByID");
    final MappedStatement insertUser = sessionFactory.getConfiguration()
        .getMappedStatement("dm.UserMapper.insertUser");

    // query
    batchExecutor.doUpdate(insertUser, new User().setName("" + new Date()));
    batchExecutor.doUpdate(insertUser, new User().setName("" + new Date()));

    // batch update
    User user = new User();
    user.setId(2);
    user.setName("" + new Date());
    batchExecutor.doUpdate(update, user);

    user.setId(3);
    batchExecutor.doUpdate(update, user);

    batchExecutor.doUpdate(insertUser, new User().setName("" + new Date()));

    //
    final List<BatchResult> batchResults = batchExecutor.flushStatements(false);
    jdbcTransaction.commit();
    printBatchResult(batchResults);

执行日志:

[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==>  Preparing: insert into `user` (name) values(?); 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==> Parameters: Sat Jul 04 15:07:30 CST 2020(String) 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==> Parameters: Sat Jul 04 15:07:30 CST 2020(String) 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==>  Preparing: update `user` set name=? where id = ? 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==> Parameters: Sat Jul 04 15:07:30 CST 2020(String), 2(Integer) 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==> Parameters: Sat Jul 04 15:07:30 CST 2020(String), 3(Integer) 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==>  Preparing: insert into `user` (name) values(?); 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==> Parameters: Sat Jul 04 15:07:30 CST 2020(String) 
[DEBUG][main] o.a.i.t.j.JdbcTransaction.commit Committing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@4b3ed2f0] 
第 1 个结果
[1, 1]
第 2 个结果
[1, 1]
第 3 个结果
[1]

从日志可以看到看到清晰的执行过程。

  • 第一个insert语句后面跟着两个参数,是一个statement。对应第 1 个结果
  • 第二个update语句后面跟着两个参数,是一个statement。对应第 2 个结果
  • 第三个insert语句后面跟着两个参数,是一个statement。对应第 3 个结果

整体逻辑和程序是一致的,但是有个问题,为什么三个相同的 insert,会分开两个结果返回呢?

这是因为 Mybatis 为了保证批次和逻辑顺序一致做了优化,并不是相同的sql就放到相同的statement。而是要按照执行顺序把相同的sql当作一个批次。

从代码中可以看到这部分逻辑:

public int doUpdate(MappedStatement ms, Object parameterObject) throws SQLException {
  if (sql.equals(currentSql) && ms.equals(currentStatement)) {
    使用当前的 statement
  } else {
    创建新的statement
  }
}

总结

网络上有些文章介绍使用 foreach 的方式执行批量操作,我个人不建议这样操作。

  1. 因为 JDBC 已经提供了批量操作的接口,符合规范,兼容性和性能更好。
  2. foreach拼接的 sql 比较长,会增加网络流量,而且驱动对sql长度是有限制的,并且要增加allowMultiQueries参数。
  3. foreach 拼接的 sql 每次都不一定相同,服务器会重新编译。

Mysql 的 sql 执行流程是连接器,查询缓存,分析器,优化器,执行器。分析器先会做“词法分析”。优化器是在表里面有多个索引的时候,决定使用哪个索引;或者在一个语句有多表关联(join)的时候,决定各个表的连接顺序。在适合的场景使用 ReuseExecutor 或 BatchExecutor 不仅可以提高性能,还可以减少对 Mysql 服务器的压力。

参考

更新流程图片来自源码阅读网