• 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

57.52
/src/main/java/org/apache/ibatis/datasource/unpooled/UnpooledDataSource.java
1
/*
2
 *    Copyright 2009-2025 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.datasource.unpooled;
17

18
import java.io.PrintWriter;
19
import java.sql.Connection;
20
import java.sql.Driver;
21
import java.sql.DriverManager;
22
import java.sql.DriverPropertyInfo;
23
import java.sql.SQLException;
24
import java.util.Enumeration;
25
import java.util.Map;
26
import java.util.Properties;
27
import java.util.concurrent.ConcurrentHashMap;
28
import java.util.concurrent.Executors;
29
import java.util.logging.Logger;
30

31
import javax.sql.DataSource;
32

33
import org.apache.ibatis.io.Resources;
34

35
/**
36
 * @author Clinton Begin
37
 * @author Eduardo Macarron
38
 */
39
public class UnpooledDataSource implements DataSource {
40

41
  private ClassLoader driverClassLoader;
42
  private Properties driverProperties;
43
  private static final Map<String, Driver> registeredDrivers = new ConcurrentHashMap<>();
1✔
44

45
  private String driver;
46
  private String url;
47
  private String username;
48
  private String password;
49

50
  private Boolean autoCommit;
51
  private Integer defaultTransactionIsolationLevel;
52
  private Integer defaultNetworkTimeout;
53

54
  static {
55
    Enumeration<Driver> drivers = DriverManager.getDrivers();
1✔
56
    while (drivers.hasMoreElements()) {
1✔
57
      Driver driver = drivers.nextElement();
1✔
58
      registeredDrivers.put(driver.getClass().getName(), driver);
1✔
59
    }
1✔
60
  }
1✔
61

62
  public UnpooledDataSource() {
1✔
63
  }
1✔
64

65
  public UnpooledDataSource(String driver, String url, String username, String password) {
1✔
66
    this.driver = driver;
1✔
67
    this.url = url;
1✔
68
    this.username = username;
1✔
69
    this.password = password;
1✔
70
  }
1✔
71

72
  public UnpooledDataSource(String driver, String url, Properties driverProperties) {
1✔
73
    this.driver = driver;
1✔
74
    this.url = url;
1✔
75
    this.driverProperties = driverProperties;
1✔
76
  }
1✔
77

78
  public UnpooledDataSource(ClassLoader driverClassLoader, String driver, String url, String username,
UNCOV
79
      String password) {
×
80
    this.driverClassLoader = driverClassLoader;
×
81
    this.driver = driver;
×
82
    this.url = url;
×
83
    this.username = username;
×
84
    this.password = password;
×
85
  }
×
86

UNCOV
87
  public UnpooledDataSource(ClassLoader driverClassLoader, String driver, String url, Properties driverProperties) {
×
88
    this.driverClassLoader = driverClassLoader;
×
89
    this.driver = driver;
×
90
    this.url = url;
×
91
    this.driverProperties = driverProperties;
×
92
  }
×
93

94
  @Override
95
  public Connection getConnection() throws SQLException {
96
    return doGetConnection(username, password);
1✔
97
  }
98

99
  @Override
100
  public Connection getConnection(String username, String password) throws SQLException {
UNCOV
101
    return doGetConnection(username, password);
×
102
  }
103

104
  @Override
105
  public void setLoginTimeout(int loginTimeout) {
UNCOV
106
    DriverManager.setLoginTimeout(loginTimeout);
×
107
  }
×
108

109
  @Override
110
  public int getLoginTimeout() {
UNCOV
111
    return DriverManager.getLoginTimeout();
×
112
  }
113

114
  @Override
115
  public void setLogWriter(PrintWriter logWriter) {
UNCOV
116
    DriverManager.setLogWriter(logWriter);
×
117
  }
×
118

119
  @Override
120
  public PrintWriter getLogWriter() {
UNCOV
121
    return DriverManager.getLogWriter();
×
122
  }
123

124
  public ClassLoader getDriverClassLoader() {
UNCOV
125
    return driverClassLoader;
×
126
  }
127

128
  public void setDriverClassLoader(ClassLoader driverClassLoader) {
UNCOV
129
    this.driverClassLoader = driverClassLoader;
×
130
  }
×
131

132
  public Properties getDriverProperties() {
UNCOV
133
    return driverProperties;
×
134
  }
135

136
  public void setDriverProperties(Properties driverProperties) {
137
    this.driverProperties = driverProperties;
1✔
138
  }
1✔
139

140
  public String getDriver() {
141
    return driver;
1✔
142
  }
143

144
  public void setDriver(String driver) {
145
    this.driver = driver;
1✔
146
  }
1✔
147

148
  public String getUrl() {
149
    return url;
1✔
150
  }
151

152
  public void setUrl(String url) {
153
    this.url = url;
1✔
154
  }
1✔
155

156
  public String getUsername() {
157
    return username;
1✔
158
  }
159

160
  public void setUsername(String username) {
161
    this.username = username;
1✔
162
  }
1✔
163

164
  public String getPassword() {
165
    return password;
1✔
166
  }
167

168
  public void setPassword(String password) {
169
    this.password = password;
1✔
170
  }
1✔
171

172
  public Boolean isAutoCommit() {
UNCOV
173
    return autoCommit;
×
174
  }
175

176
  public void setAutoCommit(Boolean autoCommit) {
177
    this.autoCommit = autoCommit;
1✔
178
  }
1✔
179

180
  public Integer getDefaultTransactionIsolationLevel() {
UNCOV
181
    return defaultTransactionIsolationLevel;
×
182
  }
183

184
  public void setDefaultTransactionIsolationLevel(Integer defaultTransactionIsolationLevel) {
UNCOV
185
    this.defaultTransactionIsolationLevel = defaultTransactionIsolationLevel;
×
186
  }
×
187

188
  /**
189
   * Gets the default network timeout.
190
   *
191
   * @return the default network timeout
192
   *
193
   * @since 3.5.2
194
   */
195
  public Integer getDefaultNetworkTimeout() {
UNCOV
196
    return defaultNetworkTimeout;
×
197
  }
198

199
  /**
200
   * Sets the default network timeout value to wait for the database operation to complete. See
201
   * {@link Connection#setNetworkTimeout(java.util.concurrent.Executor, int)}
202
   *
203
   * @param defaultNetworkTimeout
204
   *          The time in milliseconds to wait for the database operation to complete.
205
   *
206
   * @since 3.5.2
207
   */
208
  public void setDefaultNetworkTimeout(Integer defaultNetworkTimeout) {
UNCOV
209
    this.defaultNetworkTimeout = defaultNetworkTimeout;
×
210
  }
×
211

212
  private Connection doGetConnection(String username, String password) throws SQLException {
213
    Properties props = new Properties();
1✔
214
    if (driverProperties != null) {
1✔
215
      props.putAll(driverProperties);
1✔
216
    }
217
    if (username != null) {
1✔
218
      props.setProperty("user", username);
1✔
219
    }
220
    if (password != null) {
1✔
221
      props.setProperty("password", password);
1✔
222
    }
223
    return doGetConnection(props);
1✔
224
  }
225

226
  private Connection doGetConnection(Properties properties) throws SQLException {
227
    initializeDriver();
1✔
228
    Connection connection = DriverManager.getConnection(url, properties);
1✔
229
    configureConnection(connection);
1✔
230
    return connection;
1✔
231
  }
232

233
  private void initializeDriver() throws SQLException {
234
    try {
235
      registeredDrivers.computeIfAbsent(driver, x -> {
1✔
236
        Class<?> driverType;
237
        try {
238
          if (driverClassLoader != null) {
1!
UNCOV
239
            driverType = Class.forName(x, true, driverClassLoader);
×
240
          } else {
241
            driverType = Resources.classForName(x);
1✔
242
          }
243
          Driver driverInstance = (Driver) driverType.getDeclaredConstructor().newInstance();
1✔
244
          DriverManager.registerDriver(new DriverProxy(driverInstance));
1✔
245
          return driverInstance;
1✔
UNCOV
246
        } catch (Exception e) {
×
247
          throw new RuntimeException("Error setting driver on UnpooledDataSource.", e);
×
248
        }
249
      });
UNCOV
250
    } catch (RuntimeException re) {
×
251
      throw new SQLException("Error setting driver on UnpooledDataSource.", re.getCause());
×
252
    }
1✔
253
  }
1✔
254

255
  private void configureConnection(Connection conn) throws SQLException {
256
    if (defaultNetworkTimeout != null) {
1!
UNCOV
257
      conn.setNetworkTimeout(Executors.newSingleThreadExecutor(), defaultNetworkTimeout);
×
258
    }
259
    if (autoCommit != null && autoCommit != conn.getAutoCommit()) {
1!
260
      conn.setAutoCommit(autoCommit);
1✔
261
    }
262
    if (defaultTransactionIsolationLevel != null) {
1!
UNCOV
263
      conn.setTransactionIsolation(defaultTransactionIsolationLevel);
×
264
    }
265
  }
1✔
266

267
  private static class DriverProxy implements Driver {
268
    private final Driver driver;
269

270
    DriverProxy(Driver d) {
1✔
271
      this.driver = d;
1✔
272
    }
1✔
273

274
    @Override
275
    public boolean acceptsURL(String u) throws SQLException {
UNCOV
276
      return this.driver.acceptsURL(u);
×
277
    }
278

279
    @Override
280
    public Connection connect(String u, Properties p) throws SQLException {
UNCOV
281
      return this.driver.connect(u, p);
×
282
    }
283

284
    @Override
285
    public int getMajorVersion() {
UNCOV
286
      return this.driver.getMajorVersion();
×
287
    }
288

289
    @Override
290
    public int getMinorVersion() {
UNCOV
291
      return this.driver.getMinorVersion();
×
292
    }
293

294
    @Override
295
    public DriverPropertyInfo[] getPropertyInfo(String u, Properties p) throws SQLException {
UNCOV
296
      return this.driver.getPropertyInfo(u, p);
×
297
    }
298

299
    @Override
300
    public boolean jdbcCompliant() {
UNCOV
301
      return this.driver.jdbcCompliant();
×
302
    }
303

304
    @Override
305
    public Logger getParentLogger() {
UNCOV
306
      return Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
×
307
    }
308
  }
309

310
  @Override
311
  public <T> T unwrap(Class<T> iface) throws SQLException {
UNCOV
312
    throw new SQLException(getClass().getName() + " is not a wrapper.");
×
313
  }
314

315
  @Override
316
  public boolean isWrapperFor(Class<?> iface) throws SQLException {
UNCOV
317
    return false;
×
318
  }
319

320
  @Override
321
  public Logger getParentLogger() {
322
    // requires JDK version 1.6
UNCOV
323
    return Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
×
324
  }
325

326
}
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