欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

《Mybatis源码》第15章 Transaction

程序员文章站 2022-04-21 23:49:58
...

事务工厂(TransactionFactory)

TransactionFactory该接口有2个子类JdbcTransactionFactoryManagedTransactionFactory,分别代表JDBC类型 和 MANAGED类型

JdbcTransactionFactory 生产JdbcTransaction,会使用Connection对象的commit()rollback()close()方法来管理方法

ManagedTransactionFactory生产ManagedTransaction,会把事务交给容器去管理

TransactionFactory

public interface TransactionFactory {

  void setProperties(Properties props);
    
  // 通过连接创建事务对象
  Transaction newTransaction(Connection conn);
    
  // 通过数据源创建事务对象
  Transaction newTransaction(DataSource dataSource, TransactionIsolationLevel level, boolean autoCommit);
    
}

JdbcTransactionFactory

public class JdbcTransactionFactory implements TransactionFactory {

  @Override
  public void setProperties(Properties props) {
  }

  @Override
  public Transaction newTransaction(Connection conn) {
    // 创建JdbcTransaction类型的事务
    return new JdbcTransaction(conn);
  }

  @Override
  public Transaction newTransaction(DataSource ds, TransactionIsolationLevel level, boolean autoCommit) {
    return new JdbcTransaction(ds, level, autoCommit);
  }
}

ManagedTransactionFactory

public class ManagedTransactionFactory implements TransactionFactory {

  private boolean closeConnection = true;

  @Override
  public void setProperties(Properties props) {
    if (props != null) {
      String closeConnectionProperty = props.getProperty("closeConnection");
      if (closeConnectionProperty != null) {
        closeConnection = Boolean.valueOf(closeConnectionProperty);
      }
    }
  }

  @Override
  public Transaction newTransaction(Connection conn) {
    // 创建ManagedTransaction类型的事务
    return new ManagedTransaction(conn, closeConnection);
  }

  @Override
  public Transaction newTransaction(DataSource ds, TransactionIsolationLevel level, boolean autoCommit) {
    // Silently ignores autocommit and isolation level, as managed transactions are entirely
    // controlled by an external manager.  It's silently ignored so that
    // code remains portable between managed and unmanaged configurations.
    return new ManagedTransaction(ds, level, closeConnection);
  }
}

事务(Transaction)

Transaction

上面看到了对象工厂创建了Transaction类,那么接下来看一下,Transaction接口有2个子类JdbcTransactionManagedTransaction,也就是刚才两个工厂负责创建的

// 方法一看就能看得懂
public interface Transaction {
  
  Connection getConnection() throws SQLException;

  void commit() throws SQLException;

  void rollback() throws SQLException;

  void close() throws SQLException;

  Integer getTimeout() throws SQLException;
  
}

JdbcTransaction

JdbcTransaction,这里面的commit、rollback方法就重写了,仔细看一下,似乎长得一样,都是判断是否自动提交,然后通过连接去执行方法

public class JdbcTransaction implements Transaction {
  @Override
  public void commit() throws SQLException {
    // 验证连接 && 是否自动提交
    if (connection != null && !connection.getAutoCommit()) {
      if (log.isDebugEnabled()) {
        log.debug("Committing JDBC Connection [" + connection + "]");
      }
      connection.commit();
    }
  }

  @Override
  public void rollback() throws SQLException {
    // 验证连接 && 是否自动
    if (connection != null && !connection.getAutoCommit()) {
      if (log.isDebugEnabled()) {
        log.debug("Rolling back JDBC Connection [" + connection + "]");
      }
      connection.rollback();
    }
  }
}

ManagedTransaction

ManagedTransaction含义为托管事务,空壳事务管理器,皮包公司。仅是提醒用户,在其它环境中应用时,把事务托管给其它框架,比如托管给Spring,让Spring去管理事务,从源码中我们看到 commit 和 rollback 都是空实现

public class ManagedTransaction implements Transaction {
  // 省略一些....
    
  @Override
  public void commit() throws SQLException {
    // Does nothing
  }

  @Override
  public void rollback() throws SQLException {
    // Does nothing
  }
  
  // 省略一些....
}

相关标签: mybatis mybatis