一: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. Jmeter接口测试(三)接口测试实践

    Jmeter 脚本编写一般分五个步骤: 1. 添加线程组 2. 添加 http 请求 3. 在 http 请求中写入接入 url.路径.请求方式和参数 4. 添加查看结果树 5. 调用接口.查看返回值 ...

  2. CentOS7.2安装mysql-5.7.19多实例

    安装多实例之前首先需要先安装mysql,这里就不介绍如何安装mysql了,参考前面的博客:https://www.cnblogs.com/hei-ma/p/9505509.html 安装多实例之前需要 ...

  3. hbase 修复 hbck

    hbase 修复使用hbck 新版本的 hbck 可以修复各种错误,修复选项是: (1)-fix,向下兼容用,被-fixAssignments替代 (2)-fixAssignments,用于修复reg ...

  4. Python坑系列:可变对象与不可变对象

    在之前的文章 http://www.cnblogs.com/bitpeng/p/4748148.html 中,大家看到了ret.append(path) 和ret.append(path[:])的巨大 ...

  5. Pythagorean Triples毕达哥斯拉三角(数学思维+构造)

    Description Katya studies in a fifth grade. Recently her class studied right triangles and the Pytha ...

  6. “Hello World!”团队第七周召开的第一次会议

    今天是我们团队“Hello World!”团队第七周召开的第一次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.Todo List 六.会议照片 七.燃尽图 一.会议时间 ...

  7. Beta周王者荣耀交流协会第六次会议

    1.立会照片 成员王超,高远博,冉华,王磊,王玉玲,任思佳,袁玥全部到齐. master:袁玥 2. 时间跨度 2017年11月15日 19:00 — 19:10 ,总计10分钟. 3. 地点 一食堂 ...

  8. 02-JAVA 初始化

    构造器 概念:在创建对象时被自动调用的方法,构造器采用和类名一样的名称 创建对象时,会为其分配存储空间,并调用相应的构造器进行初始化.这就确保了在操作对象之前,这个对象已经被恰当的初始化了. 不接受仁 ...

  9. 又要开始新的征程了hhh(这次内容比较感兴趣)

    因为做英雄部分,既是我比较感兴趣,又很符合这次c++学习的目的,所以我很开心. 其实从小玩的RPG,即时战略和回合制游戏不算少,对于属性方法其实都算不上陌生.但是还是在网上找了一些学习资源. http ...

  10. HDU 5269 ZYB loves Xor I Trie树

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5269 bc:http://bestcoder.hdu.edu.cn/contests/con ...