mybatis源码阅读-SqlSession

Posted 意犹未尽

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis源码阅读-SqlSession相关的知识,希望对你有一定的参考价值。

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>

 

以上是关于mybatis源码阅读-SqlSession的主要内容,如果未能解决你的问题,请参考以下文章

关于MyBatis sqlSession的一点整理

MyBatis 基础知识总结 5SqlSessionFactory和SqlSession

mybatis源码阅读

MyBatis源码解读——MapperProxy

通过源码分析MyBatis的缓存

Mybatis 中sqlsession源码解析