一: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() 底层代码剖析的更多相关文章

  1. Mybatis原理和代码剖析

    参考资料(官方) Mybatis官方文档: https://mybatis.org/mybatis-3/ Mybatis-Parent : https://github.com/mybatis/par ...

  2. 抛开 Spring ,你知道 MyBatis 加载 Mapper 的底层原理吗?

    原文链接:抛开 Spring ,你知道 MyBatis 加载 Mapper 的底层原理吗? 大家都知道,利用 Spring 整合 MyBatis,我们可以直接利用 @MapperScan 注解或者 @ ...

  3. MySQL底层索引剖析

    1:Mysql索引是什么 mysql索引: 是一种帮助mysql高效的获取数据的数据结构,这些数据结构以某种方式引用数据,这种结构就是索引.可简单理解为排好序的快速查找数据结构.如果要查“mysql” ...

  4. mybatis与spring的整合(代码实现)

    mybatis与spring的整合(代码实现) 需要jar包: mybatis核心包:依赖包:log4j包:spring croe;beans;tx;aop;aspects;context;expre ...

  5. 《VR入门系列教程》之22---GearVR SDK代码剖析

    GearVR SDK代码剖析     接下来我们来了解一下GearVR开发包的底层代码,它底层的代码和之前在第三章中讲的桌面SDK代码非常类似,当然,也有许多不同的地方.     首先,我们看看如何构 ...

  6. ArrayList底层代码解析笔记

    通过底层代码可以学习到很多东西: public class ArrayList<E> extends AbstractList<E> implements List<E& ...

  7. Jquery UI 组合树 - ComboTree 集成Wabacus4.1 代码剖析

    Jquery UI 1.3 (组合树 - ComboTree ) 集成Wabacus4.1 集成Spring 代码剖析 使用时,请下载需要Jquery ui包进行配置 combotree.js 的代码 ...

  8. FreeRTOS代码剖析

    FreeRTOS代码剖析之1:内存管理Heap_1.c   FreeRTOS代码剖析之2:内存管理Heap_2.c   FreeRTOS(V8.0.1)系统之xTaskGenericCreate() ...

  9. java中CRUD(增删查改)底层代码的实现

    java中CRUD(增删查改)底层代码的实现: package com.station.dao; import com.station.model.Product; import java.sql.* ...

随机推荐

  1. 一个很easy的脚本--php获取服务器端的相关信息

    存档: <html> <head> <meta http-equiv="content-type" content="text/html;c ...

  2. java class file

    目录 什么是java类文件 幻数 主次版本号 常量池数和常量池 this_class super_class 接口数量和接口 字段数和字段 方法数和方法 以下内容主要还是参考<Inside JV ...

  3. Python爬虫与反爬虫(7)

    [Python基础知识]Python爬虫与反爬虫(7) 很久没有补爬虫了,相信在白蚁二周年庆的活动大厅比赛中遇到了关于反爬虫的问题吧 这节我会做个基本分享. 从功能上来讲,爬虫一般分为数据采集,处理, ...

  4. Rest-Assured 测试框架

    Rest-Assured 是一个测试 Restful Web Service 的 Java 类库,我们能够测试各种各样的请求组合,依次测试核心业务逻辑的不同组合. 它是通过发送特定的rest api, ...

  5. Python科学计算库灬numpy

    Numpy NumPy是一个功能强大的Python库,主要用于对多维数组执行计算.Numpy许多底层函数实际上是用C编写的,因此它的矩阵向量计算速度是原生Python中无法比拟的. numpy属性 维 ...

  6. c# HttpListener拒绝访问

    直接记录解决步骤: 程序代码: HttpListener httpListener = new HttpListener(); httpListener.Prefixes.Add("http ...

  7. c语言学习—图书搜索

    请问下:你说的C四大圣经指那几本啊?——<C 陷阱与缺陷> && <C程序设计语言> && <C专家编程> && & ...

  8. Alpha 冲刺5

    队名:日不落战队 安琪(队长) 今天完成的任务 组织第五次站立式会议(半冲刺总结交流会). 完成草稿箱前端界面. 明天的计划 回收站前端界面. 尝试去调用数据. 还剩下的任务 信息修改前端界面. 遇到 ...

  9. lintcode-512-解码方法

    512-解码方法 有一个消息包含A-Z通过以下规则编码 'A' -> 1 'B' -> 2 ... 'Z' -> 26 现在给你一个加密过后的消息,问有几种解码的方式 样例 给你的消 ...

  10. WebService(三)

    JAX-WS简单使用示例: 1.服务端 package com.rong.service; import javax.jws.WebMethod; import javax.jws.WebParam; ...