Executor 執行器
今天分享一下 Executor,它在框架中是具體sql的執行器,sqlSession(門面模式)封裝通用的api,把具體操作委派給 Executor 執行,Executor協同BoundSql,StatementHandler,ParameterHandler 和 ResultSetHandler 完成作業,
它使用裝飾器的方式組織 Executor 物件,如 CachingExecutor 裝飾了SimpleExecutor 提供二級快取功能,
可以通過插件機制擴展功能,mybatisplus 就是通過插件機制擴展的功能,
下面是更新流程,Executor 處于流程中間藍色部分,快取執行器,基礎執行器,簡單執行器三個 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 的方式執行批量操作,我個人不建議這樣操作,
- 因為 JDBC 已經提供了批量操作的介面,符合規范,兼容性和性能更好,
- foreach拼接的 sql 比較長,會增加網路流量,而且驅動對sql長度是有限制的,并且要增加allowMultiQueries引數,
- foreach 拼接的 sql 每次都不一定相同,服務器會重新編譯,
Mysql 的 sql 執行流程是連接器,查詢快取,分析器,優化器,執行器,分析器先會做“詞法分析”,優化器是在表里面有多個索引的時候,決定使用哪個索引;或者在一個陳述句有多表關聯(join)的時候,決定各個表的連接順序,在適合的場景使用 ReuseExecutor 或 BatchExecutor 不僅可以提高性能,還可以減少對 Mysql 服務器的壓力,
作者:但莫
鏈接:https://juejin.im/post/6854573210420772877
來源:掘金
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/204354.html
標籤:java
