MyBatis openSession(),close(),和commit() 底层代码剖析
一:MyBatis工具类 中openSession到底做了什么?
Mybatis工具类
private static final String RESOURCE = "mybatis-config.xml";
private static SqlSessionFactory sqlSessionFactory = null;
private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>(); //关闭sqlsession
public static void closeSession(){
SqlSession session = (SqlSession) threadLocal.get(); //
threadLocal.set(null);
if (session !=null){
session.close();
}
} public static SqlSession getSessionTwo() {
//读取配置文件
try {
InputStream stream = Resources.getResourceAsStream(RESOURCE);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream); return sqlSessionFactory.openSession(); //返回openSession
} catch (IOException e) {
e.printStackTrace();
} return null;
}
首先点开openSession 发现踏实sqlsessionFactory的一个方法
package org.apache.ibatis.session;
import java.sql.Connection;
public interface SqlSessionFactory {
SqlSession openSession();
SqlSession openSession(boolean var1);
SqlSession openSession(Connection var1);
然后再看这 个方法的实现类DefaultSqlSessionFactory

这里主要返回他自己类的一个方法openSessionFroDataSource() (数据源) 并赋值 autoCommit 为false
然后 进入这个方法
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
Transaction tx = null;
DefaultSqlSession var8;
try {
Environment environment = this.configuration.getEnvironment();
TransactionFactory transactionFactory = this.getTransactionFactoryFromEnvironment(environment);
tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
Executor executor = this.configuration.newExecutor(tx, execType);
var8 = new DefaultSqlSession(this.configuration, executor, autoCommit);
} catch (Exception var12) {
this.closeTransaction(tx);
throw ExceptionFactory.wrapException("Error opening session. Cause: " + var12, var12);
} finally {
ErrorContext.instance().reset();
}
return var8;
}
可以看到他这个方法主要是初始化一些configure.xml的配置信息和DefaultSqlSession 所以openSession的主要就是初始化了
configure.xml的配置信息和DefaultSqlSession
二:MyBatis工具类 中sqlSession.close()底层为什么回滚事务?
首先进入 close()找到他的实现类DefaultsqlSession
DefaultsqlSession里的close方法
session.close(); 底层为什么可以回滚事务?????
DefaultsqlSession里的close方法
public void close() {
try {
this.executor.close(this.isCommitOrRollbackRequired(false));
this.dirty = false;
} finally {
ErrorContext.instance().reset();
}
}
进入Executor接口 查看他的实现类BaseExecutor 找到close()方法
public void close(boolean forceRollback) { //需要传入一个boolean参数
try {
try {
this.rollback(forceRollback); //为true则rollback
} finally {
if(this.transaction != null) {
this.transaction.close();
}
}
} catch (SQLException var11) {
log.warn("Unexpected exception on closing transaction. Cause: " + var11);
} finally {
this.transaction = null;
this.deferredLoads = null;
this.localCache = null;
this.localOutputParameterCache = null;
this.closed = true;
}
}
然后再看下executor.close()里的参数
this.executor.close(this.isCommitOrRollbackRequired(false));
------>isCommitOrRollbackRequired
private boolean isCommitOrRollbackRequired(boolean force) {
return true || false;
}
//可以看到如果没有提交之前调用close() ,isComitOrRollbackRequired()返回的是true
然后Executor的实现类BaseExecutor的close()方法为参数
forceRollback为true 然后进入rollback(forceRollBack)参数为true
看下最终的rollback方法
public void rollback(boolean required) throws SQLException {
if(!this.closed) {
try {
this.clearLocalCache();
this.flushStatements(true);
} finally {
if(required) {
this.transaction.rollback(); //如果传入的参数为 true 则进行事务 回滚
}
}
}
另外至于为什么没有提交之前close会回滚事务,提交了之后则是关闭事务
主要在于Executor的实现类BaseExecutor的
isCommitOrRollbackRequired()参数改变了。 三:commit()
session.commit(); 为什么能提交事务,他的实现和close()的实先基本差不多,主要是
isCommitOrRollbackRequired()方法
参数的真假,真则提交,假便关闭
session会话
000000000写的好low啊自嘲
MyBatis openSession(),close(),和commit() 底层代码剖析的更多相关文章
- Mybatis原理和代码剖析
参考资料(官方) Mybatis官方文档: https://mybatis.org/mybatis-3/ Mybatis-Parent : https://github.com/mybatis/par ...
- 抛开 Spring ,你知道 MyBatis 加载 Mapper 的底层原理吗?
原文链接:抛开 Spring ,你知道 MyBatis 加载 Mapper 的底层原理吗? 大家都知道,利用 Spring 整合 MyBatis,我们可以直接利用 @MapperScan 注解或者 @ ...
- MySQL底层索引剖析
1:Mysql索引是什么 mysql索引: 是一种帮助mysql高效的获取数据的数据结构,这些数据结构以某种方式引用数据,这种结构就是索引.可简单理解为排好序的快速查找数据结构.如果要查“mysql” ...
- mybatis与spring的整合(代码实现)
mybatis与spring的整合(代码实现) 需要jar包: mybatis核心包:依赖包:log4j包:spring croe;beans;tx;aop;aspects;context;expre ...
- 《VR入门系列教程》之22---GearVR SDK代码剖析
GearVR SDK代码剖析 接下来我们来了解一下GearVR开发包的底层代码,它底层的代码和之前在第三章中讲的桌面SDK代码非常类似,当然,也有许多不同的地方. 首先,我们看看如何构 ...
- ArrayList底层代码解析笔记
通过底层代码可以学习到很多东西: public class ArrayList<E> extends AbstractList<E> implements List<E& ...
- Jquery UI 组合树 - ComboTree 集成Wabacus4.1 代码剖析
Jquery UI 1.3 (组合树 - ComboTree ) 集成Wabacus4.1 集成Spring 代码剖析 使用时,请下载需要Jquery ui包进行配置 combotree.js 的代码 ...
- FreeRTOS代码剖析
FreeRTOS代码剖析之1:内存管理Heap_1.c FreeRTOS代码剖析之2:内存管理Heap_2.c FreeRTOS(V8.0.1)系统之xTaskGenericCreate() ...
- java中CRUD(增删查改)底层代码的实现
java中CRUD(增删查改)底层代码的实现: package com.station.dao; import com.station.model.Product; import java.sql.* ...
随机推荐
- python全栈开发-前方高能-内置函数2
python_day_15 一.今日主要内容 1. lambda 匿名函数 语法: lambda 参数:返回值 不能完成复杂的操作 2. sorted() 函数 排序. 1. 可迭代对象 2. key ...
- youtube视频下载和搬运的方法
youtube全球最大的视频网站, 全世界每天有三分之一的网民在youtube上观看视频, 可是大部分人不知道, 在这些网民有一小部分人是依靠youtube生存的, 他们上传视频到youtube, y ...
- idea项目 run、debug变灰色的问题
点击如图所示位置的下来三角按钮,然后选择Edit Configurations,或者点击菜单栏Run>Edit Configurations 2 在运行配置窗口,选择一条springboot的运 ...
- 小白初识 - 计数排序(CountingSort)
计数排序,属于桶排序特殊的一种. 当要排序n个数据的时候,如果所处的范围不大,我们可以取其中的最大值K,并将数据分散在K个桶里面, 每个桶里面的数据都是相同的(这样省去了桶内排序的时间),然后顺序取出 ...
- 高可用Kubernetes集群-9. 部署kubelet
十一.部署kubelet 接下来两个章节是部署Kube-Node相关的服务,包含:kubelet,kube-proxy. 1. TLS bootstrap用户授权 # kubelet采用TLS Boo ...
- 剑指offer-数值的整数方
数值的整数方 一.问题描述 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 二.算法思路 按照指数Exp的情况进行讨论. Exp> ...
- AutoCAD 自动管理字体插件[使用ObjectARX C++]
概述: 使用AutoCAD的过程中,我们常常因为缺失字体而烦恼,本插件就是为了解决这个问题. 插件采用WEB服务器 + CAD插件方式.WEB服务器使用Python编写,部署在百度BAE上:CAD插件 ...
- python3【基础】-list&tuple
一.list概述 list (列表)是python中最常用的数据类型之一,通过列表可以对数据实现最方便的存储,修改等操作.在python3中,list支持如下方法: Help on class lis ...
- 冲刺ing-6
第六次Scrum冲刺 队员完成的任务 队员 完成任务 吴伟华 Leangoo的看板截图,燃尽图 蔺皓雯 编写博客,界面设计 蔡晨旸 界面设计 曾茜 测试 鲁婧楠 学习后端设计 杨池宇 界面设计 成员遇 ...
- C++:const用法的简单总结
一.对变量的修饰 在c++中,如果我们希望定义一个值不会被改变的变量,那么可以用关键字const对它进行修饰,被修饰后的变量其作用相当于一个常量 //这两种方式等价 2 语法1:const 类型名 变 ...