相信看过Mybatis的执行器Executor以及5个子类源码的朋友应该对FlushStatements和doFlushSatements知道吧,这里我们说说它的作用,顾名思义的话了就是刷新Statement,我们知道在执行器就是执行Statement的,在进行提交,回滚等事务操作以及DML操作时,需要刷新Statement,在不同的Executor执行器中因为缓存执行器需要缓存,SimpleStatement执行完就关闭Statement,ReuseExecutor执行完不关闭而是返回Map集合中,等,所以在各执行器中的doFlushSatements也是不同的。
BaseExecutor
我们知道BaseExecutor是Executor顶层接口的子类,也是执行器类的基类,它基本实现了Update和quart还有commit,rollback等功能,还有定义了抽象方法doUpdate(),doQuery(), doFlushStatement()
给其子类去实现符合各自的方法,它也简单的实现了flushStatement的功能,也定义了doFlushStatement抽象方法给子类去实现。
@Override
public List<BatchResult> flushStatements() throws SQLException {
return flushStatements(false);
}
public List<BatchResult> flushStatements(boolean isRollBack) throws SQLException {
if (closed) {
throw new ExecutorException("Executor was closed.");
}
return doFlushStatements(isRollBack);
}
/**
* 事务提交
*/
@Override
public void commit(boolean required) throws SQLException {
if (closed) {
throw new ExecutorException("Cannot commit, transaction is already closed");
}
//清除缓存
clearLocalCache();
//flushStatements里,不