MyBatisMyBatis openSession(),close(),和commit() 底层代码剖析

  

 

 

 

然后 进入这几个法子  

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()找到他的落到实处类DefaultsqlSession

 二:MyBatis工具类
中sqlSession.close()底层为何回滚事务?

 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;
    }

 
 DefaultsqlSession里的close方法

 

别的至于为啥一直不交给从前close会回滚事务,提交了以后则是倒闭工作

 

 

 

 1     private static final String RESOURCE = "mybatis-config.xml";
 2     private static SqlSessionFactory sqlSessionFactory = null;
 3     private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>();
 4 
 5 
 6 
 7 
 8     //关闭sqlsession
 9     public static void closeSession(){
10         SqlSession session = (SqlSession) threadLocal.get(); // 2
11         threadLocal.set(null);
12         if (session !=null){
13             session.close();
14         }
15     }
16 
17     public static SqlSession getSessionTwo() {
18         //读取配置文件
19         try {
20             InputStream stream = Resources.getResourceAsStream(RESOURCE);
21             sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
22 
23             return sqlSessionFactory.openSession();   //返回openSession
24         } catch (IOException e) {
25             e.printStackTrace();
26         }
27 
28         return null;
29     }

 

 首先点开openSession 发现踏实sqlsessionFactory的一个主意

MyBatis 1

一:MyBatis工具类
中openSession到底做了哪些?

可以看到他这个方法主要是初始化一些configure.xml的配置信息和DefaultSqlSession

所以openSession的主要就是初始化了

configure.xml的配置信息和DefaultSqlSession

     参数的真伪,真则交由,假便关门大吉
    session会话

     

 

驷不及舌在于Executor的落到实处类BaseExecutor的

然后再看那 个点子的贯彻类DefaultSqlSessionFactory

 

   

isCommitOrRollbackRequired()方法

 

        Mybatis工具类

 1 package org.apache.ibatis.session;
 2 
 3 import java.sql.Connection;
 4 
 5 public interface SqlSessionFactory {
 6     SqlSession openSession();
 7 
 8     SqlSession openSession(boolean var1);
 9 
10     SqlSession openSession(Connection var1);

那里紧要重回她自己类的一个措施openSessionFroDataSource()  (数据源)
并赋值 autoCommit 为false

 

000000000写的好low啊自嘲
isCommitOrRollbackRequired()参数改变了。

三:commit()

    session.commit();
为何能交到业务,他的兑现和close()的实先基本大致,首如若

 

网站地图xml地图