码迷,mamicode.com
首页 > 数据库 > 详细

SSM-MyBatis-08:Mybatis中SqlSession的commit方法为什么会造成事物的提交

时间:2018-02-24 20:40:30      阅读:343      评论:0      收藏:0      [点我收藏+]

标签:实现类   require   sed   size   res   not   ctrl+h   mit   计算   

 

 

------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

 

 

如题目所示,本小章节讨论为什么SqlSession的commit会造成事物的提交

首先先看SqlSession的commit()他是一个接口的方法,所以去他的实现类找(Ctrl+H)DefaultSqlSession类中查找他的commit无参数的方法,因为我们调用的也是他的无参方法,往下看

 

public void commit() {
        this.commit(false);
    }

    public void commit(boolean force) {
        try {
            this.executor.commit(this.isCommitOrRollbackRequired(force));
            this.dirty = false;
        } catch (Exception var6) {
            throw ExceptionFactory.wrapException("Error committing transaction.  Cause: " + var6, var6);
        } finally {
            ErrorContext.instance().reset();
        }

    }

 

看出来了吗?他调用了他下面的一个有参数的方法,并且传进去了参数,我们稍做记录,force是false

dirty变成false在执行器的提交下面,说明数据不是脏的了

executor.commit(XXX)执行器的提交,我们看里面的那个方法,commit在方法参数里面调用了方法并拿到返回值,看一下那个里面的方法,注意传进去的值为false

 

private boolean isCommitOrRollbackRequired(boolean force) {
        return !this.autoCommit && this.dirty || force;
    }

 

这个方法说实话,一眼看上去有点懵,但是你知道      !        &&        ||     的使用优先级,你就可以计算出来了      &&>||>!                    结果return回去的是true

上面的this.dirty是在上回说到的增删改都会底层调用update方法,里面改为true的

上面的autoCommit则创建sqlSession的时候就早早的改为false,OpenSession方法底层

接下来看上面的上面的executor.commit(XXX)方法,,他是Executor执行器接口的,找他的实现类BaseExecutor类的commit的带boolean参数的方法

 

 public void commit(boolean required) throws SQLException {
        if(this.closed) {
            throw new ExecutorException("Cannot commit, transaction is already closed");
        } else {
            this.clearLocalCache();
            this.flushStatements();
            if(required) {
                this.transaction.commit();
            }

        }
   }

 

里面有一些不需要关注,不过简单提一嘴,clearLocalCache()清理缓存,flushStatements刷新参数

最终级的关注点就是因为if中的required是传进来的参数,上面已经解释过是true,所以他执行下一行代码就是transaction.commit(),哦,transaction的英文名就叫做事物

终结一句话:session.commit()最终调度到了事物的提交 ,this.transaction.commit()

 

SSM-MyBatis-08:Mybatis中SqlSession的commit方法为什么会造成事物的提交

标签:实现类   require   sed   size   res   not   ctrl+h   mit   计算   

原文地址:https://www.cnblogs.com/DawnCHENXI/p/8467392.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!