第二个过程sqlSessionFactory.openSession()
第一个步骤,获得sqlsessionFactory简单分析完之后,现在开始接着分析.sqlSessionFactory.openSession()
现在开始记录:debug进入sqlSessionFactory.openSession();
方法体里面,代码如图所示:
@Override
public SqlSession openSession() {
return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
}
configuration.getDefaultExecutorType()
获得的DefaultExecutorType默认执行器的类型,官网文档上有介绍:
默认是SIMPLE,
debug当中显示的信息如下:
代码#1接着看: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);
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();
}
}
主要的步骤是获取一些信息,如environment ,创建事务tx,然后执行了一条语句Executor executor = configuration.newExecutor(tx, execType);
这个是myabtis架构图中的一个重要组件Excutor,(执行器);
重点看这个executor
是如何new出来的,
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);
}
if (cacheEnabled) {//#7
executor = new CachingExecutor(executor);
}
executor = (Executor) interceptorChain.pluginAll(executor);
return executor;
}
根据executorType,执行对应的new操作,我这里是:executor = new SimpleExecutor(this, transaction);
然后接着往下执行#7处,if (cacheEnabled)
,判断是否开启二级缓存,如果开启了二级缓存的话,用CachingExecutor
这个类包装一下,生成一个被包装过的executor
.,(被包装过的executor,在实行查询操作时的时候会先从缓存当中,查找。截图如下:)
代码接着往下走,执行到executor = (Executor) interceptorChain.pluginAll(executor);
,执行拦截器链的的pluginAll()
方法,进去看一下这个方法,代码如下:
public Object pluginAll(Object target) {
for (Interceptor interceptor : interceptors) {
target = interceptor.plugin(target);
}
return target;
}
这段代码的意思比较好理解,就是拿到所有的拦截器,执行每一个拦截器的plugin(target)
方法,这里的target
就是我们传入的executor
,就是对exexutor进行重新包装,这里应该是自定义myatis插件的时候,这个方法会起作用,我这里没有自定义拦截器。
调用这个方法之后,我们的executor
对象就返回啦。
然后返到openSessionFromDataSource()
方法里(代码#1),然后执行new DefaultSqlSession(configuration, executor, autoCommit);
这个就是我们执行opeanSession();所获得的SqlSession.。
为了加强自己的记忆,简单画了一下openSession()方法过程的时序图:
小结:openSession()的大概步骤如下:
1.执行openSessionFromDataSource()方法
1.1获得基本配置信息,创建事务
1.2创建executor执行器,如果开启二级缓存,包装成带有缓存的executor
1.3调用拦截器链,包装executor
根据返回的executor 执行new DefaultSqlSession(configuration, executor, autoCommit)方法
返回DefaultSqlSession 就是我们需要的SqlSessionFactory