摘要
主要讲解SqlSession对象创建过程和源码分析。sqlSessionFactory创建SqlSession对象的过程进行了源码分析,最后返回的SqlSession中包含有两个重要的对象,分别是configuration与executor。configuration对象在创建SqlSessionFactory的时候就已经被创建出来了,用来保存全局配置文件与SQL 映射文件中的信息,executor是一个执行器对象。
SqlSession
对象创建源码分析
private SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream is = Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(is);
}
@Test
public void testMyBatis3Simple() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
// 将断点打在下面代码的前面
SqlSession sqlSession = sqlSessionFactory.openSession();
}
1.首先会跳到DefaultSqlSessionFactory类中的openSession()方法中。
// ====== DefaultSqlSessionFactory 类中的方法 ======
@Override
public SqlSession openSession() {
return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
}
在上面这个方法中调用了另一个方法:
openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit)
// ====== DefaultSqlSessionFactory 类中的方法 ======
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);
/**
* ExecutorType 是一个枚举类型,默认是SIMPLE
* 根据ExecutorType 创建一个Executor 对象
* 因为Executor 对象比较重要,下面来分析一下Executor 对象创建的过程
*/
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();
}
}
2.configuration.newExecutor(tx, execType)创建过程如下:
// ====== Configuration 类中的方法 ======
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
executorType = executorType == null ? defaultExecutorType : executorType;
executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
Executor executor;
/**
* 根据executorType 类型创建对应的Executor
* BatchExecutor:批量执行器
* ReuseExecutor:会执行预处理的执行器
* SimpleExecutor:简单的执行器
*/
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 来包装executor,
* 在查询之前都会先查询缓存中是否有对应的数据
* 包装的过程使用了装饰者模式,装饰者模式可参考博文:
* http://blog.csdn.net/codejas/article/details/79112824
*/
if (cacheEnabled) {
executor = new CachingExecutor(executor);
// 最后使用每个拦截器重新包装executor 并返回
executor = (Executor) interceptorChain.pluginAll(executor);
// executor 对象创建完成并返回
return executor;
}
3.Executor 对象创建完成后,会接着执行
openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit)方法。
// ====== DefaultSqlSessionFactory 类中的方法 ======
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);
/**
* configuration 对象在创建SqlSessionFactory 对象的时候就已经创建了
* Executor 对象创建完成后,使用executor与configuration 对象来创建DefaultSqlSession
* 返回DefaultSqlSession 对象
*/
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();
}
}
到这里会接着向上一步返回,SqlSession对象创建的过程也就结束了。