• mybatis源码阅读-Transaction和TransactionFactory(四)


    Transaction

    类图

    接口定义

    public interface Transaction {
        Connection getConnection() throws SQLException;
    
        void commit() throws SQLException;
    
        void rollback() throws SQLException;
    
        void close() throws SQLException;
    
        Integer getTimeout() throws SQLException;
    }

    ManagedTransaction

    说明

    含义为托管事务,空壳事务管理器,皮包公司。仅是提醒用户,在其它环境中应用时,把事务托管给其它框架,比如托管给Spring,让Spring去管理事务。

    部分源码

    public class ManagedTransaction implements Transaction {
        private static final Log log = LogFactory.getLog(org.apache.ibatis.transaction.managed.ManagedTransaction.class);
        private DataSource dataSource;
        private TransactionIsolationLevel level;
        private Connection connection;
        private boolean closeConnection;
    
        public void commit() throws SQLException {
        }
    
        public void rollback() throws SQLException {
        }
    }

    可以看到commit和rollback都是空实现

    JDBCTransaction

    部分源码

    public class JdbcTransaction implements Transaction {
        private static final Log log = LogFactory.getLog(JdbcTransaction.class);
        protected Connection connection;
        protected DataSource dataSource;
        protected TransactionIsolationLevel level;
        protected boolean autoCommmit;
    
        public JdbcTransaction(DataSource ds, TransactionIsolationLevel desiredLevel, boolean desiredAutoCommit) {
            this.dataSource = ds;
            this.level = desiredLevel;
            this.autoCommmit = desiredAutoCommit;
        }
    
        public JdbcTransaction(Connection connection) {
            this.connection = connection;
        }
    
        public Connection getConnection() throws SQLException {
            if (this.connection == null) {
                this.openConnection();
            }
    
            return this.connection;
        }
    
        /**
         * 提交
          * @throws SQLException
         */ 
        public void commit() throws SQLException {
            if (this.connection != null && !this.connection.getAutoCommit()) {
                if (log.isDebugEnabled()) {
                    log.debug("Committing JDBC Connection [" + this.connection + "]");
                }
    
                this.connection.commit();
            }
    
        }
    
        /**
         * 回滚
         * @throws SQLException
         */
        public void rollback() throws SQLException {
            if (this.connection != null && !this.connection.getAutoCommit()) {
                if (log.isDebugEnabled()) {
                    log.debug("Rolling back JDBC Connection [" + this.connection + "]");
                }
    
                this.connection.rollback();
            }
    
        }
    
        /**
         * 关闭连接
         * @throws SQLException
         */
        public void close() throws SQLException {
            if (this.connection != null) {
                //因为close是将连接放回连接池 所以重置事物AutoCommit属性
                this.resetAutoCommit();
                if (log.isDebugEnabled()) {
                    log.debug("Closing JDBC Connection [" + this.connection + "]");
                }
    
                this.connection.close();
            }
    
        }
    
        /**
         * 重置AutoCommit属性
         */
        protected void resetAutoCommit() {
            try {
                if (!this.connection.getAutoCommit()) {
                    if (log.isDebugEnabled()) {
                        log.debug("Resetting autocommit to true on JDBC Connection [" + this.connection + "]");
                    }
    
                    this.connection.setAutoCommit(true);
                }
            } catch (SQLException var2) {
                if (log.isDebugEnabled()) {
                    log.debug("Error resetting autocommit to true before closing the connection.  Cause: " + var2);
                }
            }
        }
    }

    TransactionFactory

    类图

    2个工厂创建对应的事物管理器(这里应用的是抽象工厂模式) 内部知识检查的创建对应的管理器

    xml设置对应的事物管理

        <!-- 对事务的管理和连接池的配置 -->
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC" /><!--设置对应的事物工厂-->
                <!--UNPOOLED  非连接池    POOLED mybatis提供的POOLED连接池    JNDI mybtais提供的JNDIfacotory获取数据源
                   根据配置找到对应的工厂 创建对应的数据源  可以直接配置工厂 需要实现UnpooledDataSourceFactory
                -->
                <dataSource type="com.liqiang.datasource.C3P0DataSourceFactory">
                    <property name="driverClass" value="com.mysql.jdbc.Driver" />
                    <property name="jdbcUrl" value="jdbc:mysql://ip:port/ocms-product?characterEncoding=utf-8"/>
                    <property name="user" value="devops" />
                    <property name="password" value="Devops@123" />
                </dataSource>
            </environment>
        </environments>
  • 相关阅读:
    MySQL---exists检查数据库对象是否存在
    MySQL---自查询
    MySQL---多表查询
    等待元素
    单元测试---testsuite对象
    mysql--事务
    untieeest方法
    线性,模块,数据,关键字的作用
    selenium中隐式等待和显示等待的区别
    软件测试的原则
  • 原文地址:https://www.cnblogs.com/LQBlog/p/9277869.html
Copyright © 2020-2023  润新知