MyBatis之DefaultSqlSessionFactory

MyBatis官方文档

ORM框架 SqlSessionFactory的默认实现类

源码

/**
 * @author Clinton Begin
 */
public class DefaultSqlSessionFactory implements SqlSessionFactory {
          
   

  private final Configuration configuration;

  public DefaultSqlSessionFactory(Configuration configuration) {
          
   
    this.configuration = configuration;
  }

  @Override
  public SqlSession openSession() {
          
   
    return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
  }

  @Override
  public SqlSession openSession(boolean autoCommit) {
          
   
    return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, autoCommit);
  }

  @Override
  public SqlSession openSession(ExecutorType execType) {
          
   
    return openSessionFromDataSource(execType, null, false);
  }

  @Override
  public SqlSession openSession(TransactionIsolationLevel level) {
          
   
    return openSessionFromDataSource(configuration.getDefaultExecutorType(), level, false);
  }

  @Override
  public SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level) {
          
   
    return openSessionFromDataSource(execType, level, false);
  }

  @Override
  public SqlSession openSession(ExecutorType execType, boolean autoCommit) {
          
   
    return openSessionFromDataSource(execType, null, autoCommit);
  }

  @Override
  public SqlSession openSession(Connection connection) {
          
   
    return openSessionFromConnection(configuration.getDefaultExecutorType(), connection);
  }

  @Override
  public SqlSession openSession(ExecutorType execType, Connection connection) {
          
   
    return openSessionFromConnection(execType, connection);
  }

  @Override
  public Configuration getConfiguration() {
          
   
    return configuration;
  }

  private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
          
   
    Transaction tx = null;
    try {
          
   
      final Environment environment = configuration.getEnvironment();
      final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
      tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
      final Executor executor = configuration.newExecutor(tx, execType);
      return new DefaultSqlSession(configuration, executor, autoCommit);
    } catch (Exception e) {
          
   
      closeTransaction(tx); // may have fetched a connection so lets call close()
      throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
    } finally {
          
   
      ErrorContext.instance().reset();
    }
  }

  private SqlSession openSessionFromConnection(ExecutorType execType, Connection connection) {
          
   
    try {
          
   
      boolean autoCommit;
      try {
          
   
        autoCommit = connection.getAutoCommit();
      } catch (SQLException e) {
          
   
        // Failover to true, as most poor drivers
        // or databases wont support transactions
        autoCommit = true;
      }      
      final Environment environment = configuration.getEnvironment();
      final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
      final Transaction tx = transactionFactory.newTransaction(connection);
      final Executor executor = configuration.newExecutor(tx, execType);
      return new DefaultSqlSession(configuration, executor, autoCommit);
    } catch (Exception e) {
          
   
      throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
    } finally {
          
   
      ErrorContext.instance().reset();
    }
  }

  private TransactionFactory getTransactionFactoryFromEnvironment(Environment environment) {
          
   
    if (environment == null || environment.getTransactionFactory() == null) {
          
   
      return new ManagedTransactionFactory();
    }
    return environment.getTransactionFactory();
  }

  private void closeTransaction(Transaction tx) {
          
   
    if (tx != null) {
          
   
      try {
          
   
        tx.close();
      } catch (SQLException ignore) {
          
   
        // Intentionally ignore. Prefer previous error.
      }
    }
  }

}
经验分享 程序员 微信小程序 职场和发展