热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

mybatis随笔二之SqlSessionFactory

在上一篇文章我们已经得到了DefaultSqlSessionFactory@OverridepublicSqlSessionopenSession(){returnopen
在上一篇文章我们已经得到了DefaultSqlSessionFactory

@Override
public SqlSession openSession() {
return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
}
在configuration内部默认指定了protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE;
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();
}
}
从代码中我们可知开启了一个jdbc事务,并调用newExecutor得到一个执行期
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) {
executor
= new CachingExecutor(executor);
}
executor
= (Executor) interceptorChain.pluginAll(executor);
return executor;
}
如果开启了cache,那么会得到一个CachingExecutor,这采用的是装饰设计模式,它的语句执行实际是交给内部的delegate来执行的。
里面值得注意的是使用了(Executor) interceptorChain.pluginAll(executor)将所有的拦截器注入进去形成一个拦截链。
public Object pluginAll(Object target) {
for (Interceptor interceptor : interceptors) {
target
= interceptor.plugin(target);
}
return target;
}
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
其实Plugin是mybatis的一个工具类,用来方便生成代理对象的,注意这个地方只会生成拦截目标是Executor的拦截类的代理对象,如下所示这种就不会调用。
@Intercepts({
@Signature(type
= StatementHandler.class,
method
= "prepare",
args
= {Connection.class})
})
然后返回DefaultSqlSession的一个实例对象
public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {
this.cOnfiguration= configuration;
this.executor = executor;
this.dirty = false;
this.autoCommit = autoCommit;
}

至此DefaultSqlSessionFactory创建DefaultSqlSession的过程完成。


 


推荐阅读
author-avatar
手机用户2602906131
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有