DefaultSqlSessionFactory.java

  1. /*
  2.  *    Copyright 2009-2024 the original author or authors.
  3.  *
  4.  *    Licensed under the Apache License, Version 2.0 (the "License");
  5.  *    you may not use this file except in compliance with the License.
  6.  *    You may obtain a copy of the License at
  7.  *
  8.  *       https://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  *    Unless required by applicable law or agreed to in writing, software
  11.  *    distributed under the License is distributed on an "AS IS" BASIS,
  12.  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  *    See the License for the specific language governing permissions and
  14.  *    limitations under the License.
  15.  */
  16. package org.apache.ibatis.session.defaults;

  17. import java.sql.Connection;
  18. import java.sql.SQLException;

  19. import org.apache.ibatis.exceptions.ExceptionFactory;
  20. import org.apache.ibatis.executor.ErrorContext;
  21. import org.apache.ibatis.executor.Executor;
  22. import org.apache.ibatis.mapping.Environment;
  23. import org.apache.ibatis.session.Configuration;
  24. import org.apache.ibatis.session.ExecutorType;
  25. import org.apache.ibatis.session.SqlSession;
  26. import org.apache.ibatis.session.SqlSessionFactory;
  27. import org.apache.ibatis.session.TransactionIsolationLevel;
  28. import org.apache.ibatis.transaction.Transaction;
  29. import org.apache.ibatis.transaction.TransactionFactory;
  30. import org.apache.ibatis.transaction.managed.ManagedTransactionFactory;

  31. /**
  32.  * @author Clinton Begin
  33.  */
  34. public class DefaultSqlSessionFactory implements SqlSessionFactory {

  35.   private final Configuration configuration;

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

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

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

  47.   @Override
  48.   public SqlSession openSession(ExecutorType execType) {
  49.     return openSessionFromDataSource(execType, null, false);
  50.   }

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

  55.   @Override
  56.   public SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level) {
  57.     return openSessionFromDataSource(execType, level, false);
  58.   }

  59.   @Override
  60.   public SqlSession openSession(ExecutorType execType, boolean autoCommit) {
  61.     return openSessionFromDataSource(execType, null, autoCommit);
  62.   }

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

  67.   @Override
  68.   public SqlSession openSession(ExecutorType execType, Connection connection) {
  69.     return openSessionFromConnection(execType, connection);
  70.   }

  71.   @Override
  72.   public Configuration getConfiguration() {
  73.     return configuration;
  74.   }

  75.   protected SqlSession createSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {
  76.     return new DefaultSqlSession(configuration, executor, autoCommit);
  77.   }

  78.   private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level,
  79.       boolean autoCommit) {
  80.     Transaction tx = null;
  81.     try {
  82.       final Environment environment = configuration.getEnvironment();
  83.       final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
  84.       tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
  85.       final Executor executor = configuration.newExecutor(tx, execType);
  86.       return createSqlSession(configuration, executor, autoCommit);
  87.     } catch (Exception e) {
  88.       closeTransaction(tx); // may have fetched a connection so lets call close()
  89.       throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
  90.     } finally {
  91.       ErrorContext.instance().reset();
  92.     }
  93.   }

  94.   private SqlSession openSessionFromConnection(ExecutorType execType, Connection connection) {
  95.     try {
  96.       boolean autoCommit;
  97.       try {
  98.         autoCommit = connection.getAutoCommit();
  99.       } catch (SQLException e) {
  100.         // Failover to true, as most poor drivers
  101.         // or databases won't support transactions
  102.         autoCommit = true;
  103.       }
  104.       final Environment environment = configuration.getEnvironment();
  105.       final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
  106.       final Transaction tx = transactionFactory.newTransaction(connection);
  107.       final Executor executor = configuration.newExecutor(tx, execType);
  108.       return createSqlSession(configuration, executor, autoCommit);
  109.     } catch (Exception e) {
  110.       throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
  111.     } finally {
  112.       ErrorContext.instance().reset();
  113.     }
  114.   }

  115.   private TransactionFactory getTransactionFactoryFromEnvironment(Environment environment) {
  116.     if (environment == null || environment.getTransactionFactory() == null) {
  117.       return new ManagedTransactionFactory();
  118.     }
  119.     return environment.getTransactionFactory();
  120.   }

  121.   private void closeTransaction(Transaction tx) {
  122.     if (tx != null) {
  123.       try {
  124.         tx.close();
  125.       } catch (SQLException ignore) {
  126.         // Intentionally ignore. Prefer previous error.
  127.       }
  128.     }
  129.   }

  130. }