View Javadoc
1   /**
2    *    Copyright 2009-2015 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    *       http://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  
18  import java.sql.Connection;
19  import java.sql.SQLException;
20  
21  import org.apache.ibatis.exceptions.ExceptionFactory;
22  import org.apache.ibatis.executor.ErrorContext;
23  import org.apache.ibatis.executor.Executor;
24  import org.apache.ibatis.mapping.Environment;
25  import org.apache.ibatis.session.Configuration;
26  import org.apache.ibatis.session.ExecutorType;
27  import org.apache.ibatis.session.SqlSession;
28  import org.apache.ibatis.session.SqlSessionFactory;
29  import org.apache.ibatis.session.TransactionIsolationLevel;
30  import org.apache.ibatis.transaction.Transaction;
31  import org.apache.ibatis.transaction.TransactionFactory;
32  import org.apache.ibatis.transaction.managed.ManagedTransactionFactory;
33  
34  /**
35   * @author Clinton Begin
36   */
37  public class DefaultSqlSessionFactory implements SqlSessionFactory {
38  
39    private final Configuration configuration;
40  
41    public DefaultSqlSessionFactory(Configuration configuration) {
42      this.configuration = configuration;
43    }
44  
45    @Override
46    public SqlSession openSession() {
47      return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
48    }
49  
50    @Override
51    public SqlSession openSession(boolean autoCommit) {
52      return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, autoCommit);
53    }
54  
55    @Override
56    public SqlSession openSession(ExecutorType execType) {
57      return openSessionFromDataSource(execType, null, false);
58    }
59  
60    @Override
61    public SqlSession openSession(TransactionIsolationLevel level) {
62      return openSessionFromDataSource(configuration.getDefaultExecutorType(), level, false);
63    }
64  
65    @Override
66    public SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level) {
67      return openSessionFromDataSource(execType, level, false);
68    }
69  
70    @Override
71    public SqlSession openSession(ExecutorType execType, boolean autoCommit) {
72      return openSessionFromDataSource(execType, null, autoCommit);
73    }
74  
75    @Override
76    public SqlSession openSession(Connection connection) {
77      return openSessionFromConnection(configuration.getDefaultExecutorType(), connection);
78    }
79  
80    @Override
81    public SqlSession openSession(ExecutorType execType, Connection connection) {
82      return openSessionFromConnection(execType, connection);
83    }
84  
85    @Override
86    public Configuration getConfiguration() {
87      return configuration;
88    }
89  
90    private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
91      Transaction tx = null;
92      try {
93        final Environment environment = configuration.getEnvironment();
94        final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
95        tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
96        final Executor executor = configuration.newExecutor(tx, execType);
97        return new DefaultSqlSession(configuration, executor, autoCommit);
98      } catch (Exception e) {
99        closeTransaction(tx); // may have fetched a connection so lets call close()
100       throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
101     } finally {
102       ErrorContext.instance().reset();
103     }
104   }
105 
106   private SqlSession openSessionFromConnection(ExecutorType execType, Connection connection) {
107     try {
108       boolean autoCommit;
109       try {
110         autoCommit = connection.getAutoCommit();
111       } catch (SQLException e) {
112         // Failover to true, as most poor drivers
113         // or databases won't support transactions
114         autoCommit = true;
115       }      
116       final Environment environment = configuration.getEnvironment();
117       final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
118       final Transaction tx = transactionFactory.newTransaction(connection);
119       final Executor executor = configuration.newExecutor(tx, execType);
120       return new DefaultSqlSession(configuration, executor, autoCommit);
121     } catch (Exception e) {
122       throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
123     } finally {
124       ErrorContext.instance().reset();
125     }
126   }
127 
128   private TransactionFactory getTransactionFactoryFromEnvironment(Environment environment) {
129     if (environment == null || environment.getTransactionFactory() == null) {
130       return new ManagedTransactionFactory();
131     }
132     return environment.getTransactionFactory();
133   }
134 
135   private void closeTransaction(Transaction tx) {
136     if (tx != null) {
137       try {
138         tx.close();
139       } catch (SQLException ignore) {
140         // Intentionally ignore. Prefer previous error.
141       }
142     }
143   }
144 
145 }