• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / mybatis-3 / 2604

03 Jan 2025 10:00AM UTC coverage: 87.524% (+0.3%) from 87.177%
2604

Pull #3146

github

web-flow
Merge 60c1f5fea into 8ac3920af
Pull Request #3146: Shared ambiguity instance

3633 of 4401 branches covered (82.55%)

4 of 4 new or added lines in 1 file covered. (100.0%)

254 existing lines in 22 files now uncovered.

9569 of 10933 relevant lines covered (87.52%)

0.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

44.19
/src/main/java/org/apache/ibatis/session/defaults/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

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) {
1✔
42
    this.configuration = configuration;
1✔
43
  }
1✔
44

45
  @Override
46
  public SqlSession openSession() {
47
    return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
1✔
48
  }
49

50
  @Override
51
  public SqlSession openSession(boolean autoCommit) {
52
    return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, autoCommit);
1✔
53
  }
54

55
  @Override
56
  public SqlSession openSession(ExecutorType execType) {
57
    return openSessionFromDataSource(execType, null, false);
1✔
58
  }
59

60
  @Override
61
  public SqlSession openSession(TransactionIsolationLevel level) {
62
    return openSessionFromDataSource(configuration.getDefaultExecutorType(), level, false);
1✔
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);
1✔
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;
1✔
88
  }
89

90
  protected SqlSession createSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {
91
    return new DefaultSqlSession(configuration, executor, autoCommit);
1✔
92
  }
93

94
  private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level,
95
      boolean autoCommit) {
96
    Transaction tx = null;
1✔
97
    try {
98
      final Environment environment = configuration.getEnvironment();
1✔
99
      final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
1✔
100
      tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
1✔
101
      final Executor executor = configuration.newExecutor(tx, execType);
1✔
102
      return createSqlSession(configuration, executor, autoCommit);
1✔
UNCOV
103
    } catch (Exception e) {
×
UNCOV
104
      closeTransaction(tx); // may have fetched a connection so lets call close()
×
UNCOV
105
      throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
×
106
    } finally {
107
      ErrorContext.instance().reset();
1✔
108
    }
109
  }
110

111
  private SqlSession openSessionFromConnection(ExecutorType execType, Connection connection) {
112
    try {
113
      boolean autoCommit;
114
      try {
115
        autoCommit = connection.getAutoCommit();
×
116
      } catch (SQLException e) {
×
117
        // Failover to true, as most poor drivers
118
        // or databases won't support transactions
119
        autoCommit = true;
×
120
      }
×
121
      final Environment environment = configuration.getEnvironment();
×
122
      final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
×
123
      final Transaction tx = transactionFactory.newTransaction(connection);
×
UNCOV
124
      final Executor executor = configuration.newExecutor(tx, execType);
×
125
      return createSqlSession(configuration, executor, autoCommit);
×
UNCOV
126
    } catch (Exception e) {
×
UNCOV
127
      throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
×
128
    } finally {
UNCOV
129
      ErrorContext.instance().reset();
×
130
    }
131
  }
132

133
  private TransactionFactory getTransactionFactoryFromEnvironment(Environment environment) {
134
    if (environment == null || environment.getTransactionFactory() == null) {
1!
UNCOV
135
      return new ManagedTransactionFactory();
×
136
    }
137
    return environment.getTransactionFactory();
1✔
138
  }
139

140
  private void closeTransaction(Transaction tx) {
UNCOV
141
    if (tx != null) {
×
142
      try {
UNCOV
143
        tx.close();
×
144
      } catch (SQLException ignore) {
×
145
        // Intentionally ignore. Prefer previous error.
UNCOV
146
      }
×
147
    }
UNCOV
148
  }
×
149

150
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc