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


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

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

    同样找到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在底层调用了事务的回滚的方法,当然会造成事务的回滚啊~~~~~~

  • 相关阅读:
    git常用命令
    thinkjs框架发布上线PM2管理,静态资源访问配置
    登陆服务器提示“You need to run "nvm install N/A" to install it before using it.”
    CentOS 7.x 用shell增加、删除端口
    CentOS 7.X 安全手记
    Centos 7.x nginx隐藏版本号
    centos7磁盘挂载及取消
    CentOS 7.4上网速度慢,修改DNS!
    Centos7.4 安装Docker
    Nodejs 使用log4js日志
  • 原文地址:https://www.cnblogs.com/DawnCHENXI/p/8467447.html
Copyright © 2020-2023  润新知