MyBatis 3源码解析(二)
2024-09-16 13:52:17
二、获取SqlSession对象
1.首先调用DefaultSqlSessionFactory 的 openSession 方法,代码如下:
@Override
public SqlSession openSession() {
//configuration.getDefaultExecutorType 是调用configuration的Executor执行器的类型,默认simple
return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
}
- 下面是openSessionFromDataSource方法:
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
Transaction tx = null;
try {
//从configuration中获取环境配置信息
final Environment environment = configuration.getEnvironment();
final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
//获取一些信息,创建了一个事物
tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
//根据Executor在configuration中的配置,创建一个新的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();
}
}
创建Executor的代码如下:
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);
}
//如果二级缓存配置开启了,创建CachingExecutor
if (cacheEnabled) {
executor = new CachingExecutor(executor);
}
executor = (Executor) interceptorChain.pluginAll(executor);
return executor;
}
最后返回了DefaultSqlSession,DefaultSqlSession中包含配置信息。
总结:返回SqlSession的实现类DefaultSqlSession对象。他里面包含了Executor和Configuration;Executor会在这一步被创建
最新文章
- Meta http-equiv的属性详解 来自wanglehui
- HTTP必知必会
- jq 获取table元素,ajax 静态填加数据
- php.ini中Magic_Quotes_Gpc开关设置
- linux常用命令之--文件打包与压缩命令
- hdu2058java
- 简析TCP的三次握手与四次挥手
- Nginx 防CC攻击拒绝代理访问
- oracle_oracle中修改日期的显示格式
- 分别用css3、JS实现图片简单的无缝轮播功效
- mysql基于binlog回滚工具_flashback(python版本)
- C# linq左连接与分组
- 炸金花的JS实现从0开始之 -------现在什么都不会(1)
- 关于JAVA中Byte类型的取值范围的推论(*零为正数,-128在计算机中的表示方法...)
- hdu2036
- uboot kernel 博客
- set 和hash_set和海量数据的处理问题
- php知识点-1
- Qt下libusb-win32的使用方法
- flex作图
热门文章
- 【java线程池】
- js数组遍历(for in ,for of ,map,foreach,filter)的区别
- 一个box-sizing: border-box和felx混合使用中遇到的问题
- Golang之变量去哪儿
- 图像检索(3):BoW实现
- Flutter 即学即用系列博客——10 混淆
- selenium+python自动化测试系列---基础知识篇(1、HTML基础知识1)
- 基于Html5 Plus + Vue + Mui 移动App开发(三)-文件操作(读取、保存、更新数据)
- C# 离线人脸识别 ArcSoft V2.0 Demo
- 放下技术,是PM迈出的第一步