您的位置:首页 > 博客中心 > 数据库 >

SSM-MyBatis-09:Mybatis中SqlSession的close为什么能造成事务的回滚

时间:2022-03-15 06:45

 

 

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

 

 

经过上几次的查找,笔者我就简单的说一下查找的思路,留给读者自己实践

同样找到sqlsession的实现类,----DefaltSqlSession,找它的close方法

 

public void close() {
        try {
            this.executor.close(this.isCommitOrRollbackRequired(false));
            this.closeCursors();
            this.dirty = false;
        } finally {
            ErrorContext.instance().reset();
        }

    }

 

executor执行器的close方法里面的这个方法,传入false

 

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

 

根据上一篇博客写的,他们三个逻辑运算符的优先级  &&>||>!

得到值为true

看executor.close(boolean XXX)的方法,同样找他实现类BaseExecutor

 

public void close(boolean forceRollback) {
        try {
            try {
                this.rollback(forceRollback);
            } 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;
        }

    }

 

从头开始看,rollback(true)这一行

 

 

 

 

public void rollback(boolean required) throws SQLException {
        if(!this.closed) {
            try {
                this.clearLocalCache();
                this.flushStatements(true);
            } finally {
                if(required) {
                    this.transaction.rollback();
                }

            }
        }

    }

 

finally中是什么事物的回滚啊,这就真相大白

SqlSession中的close在底层调用了事务的回滚的方法,当然会造成事务的回滚啊~~~~~~

 

热门排行

今日推荐

热门手游