首先sqlSessionFactory的类为:
main[1] print sqlSessionFactory
sqlSessionFactory = "org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@3e2e18f2"
main[1]
得知,常规情况下,生成了DefaultSqlSessionFactory类实例。
接下来,打开session
sqlSession = sqlSessionFactory.openSession();
我们来看看,发生了什么
@Override
public SqlSession openSession() {
//开始
return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
}
---
configuration.getDefaultExecutorType()默认返回simple类型,
Step completed: "thread=main", org.apache.ibatis.session.Configuration.getDefaultExecutorType(), line=408 bci=0
408 return defaultExecutorType;
main[1] print defaultExecutorType
defaultExecutorType = "SIMPLE"
---继续执行openSessionFromDataSource函数
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level,
boolean autoCommit) {
// 看到这里了
// 初始化为nul
Transaction tx = null;
try {
// 开始执行
// 这里返回一个封装对象,里面有事务工厂句柄 & 连接池句柄
final Environment environment = configuration.getEnvironment();
// 取出事务工厂句柄
// org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory
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 = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
下面,我们开始来仔细研究这行代码。
--------------------------------------------------------------------------------分割线
这里的dataSource我已经设置成了阿里的druid类型,打印如下:
main[1] print dataSource.getClass()
dataSource.getClass() = "class com.alibaba.druid.pool.DruidDataSource"
main[1]
继续执行newTransaction函数,代码如下,很简单
@Override
public Transaction newTransaction(DataSource ds, TransactionIsolationLevel level, boolean autoCommit) {
return new JdbcTransaction(ds, level, autoCommit);
}
先记住1个断点
stop in org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory.newTransaction
返回的事务对象为
org.apache.ibatis.transaction.jdbc.JdbcTransaction
接下来,是执行
final Executor executor = configuration.newExecutor(tx, execType);
---继续处理
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
// 从这里开始看
executorType = executorType == null ? defaultExecutorType : executorType;
executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
// 在这里,还是simple模式
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);
}
// 生成了SimpleExecutor变量
//我觉得这里不应该用cacheEnabled,所以我在settings里把它屏蔽掉了
//接下来就不执行
if (cacheEnabled) {
executor = new CachingExecutor(executor);
}
executor = (Executor) interceptorChain.pluginAll(executor);
return executor;
}
然后,就是执行
executor = (Executor) interceptorChain.pluginAll(executor);
看字面,是interceptorChain添加这个plugin.我们看具体实现吧
public Object pluginAll(Object target) {
//从这里开始执行
//这里是插件机制了
for (Interceptor interceptor : interceptors) {
target = interceptor.plugin(target);
}
return target;
//返回自己
}
如果后面有需求,就应该在这定制插件plugin.
---------------------------------------------------继续执行
return new DefaultSqlSession(configuration, executor, autoCommit);
这个没啥好说的,总结一下:
生成了一个DefaultSqlSession对象,这个对象包含executor,
executor包含transaction对象,transaction对象包含dataSource对象。