View Javadoc
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<>();
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();
56      while (drivers.hasMoreElements()) {
57        Driver driver = drivers.nextElement();
58        registeredDrivers.put(driver.getClass().getName(), driver);
59      }
60    }
61  
62    public UnpooledDataSource() {
63    }
64  
65    public UnpooledDataSource(String driver, String url, String username, String password) {
66      this.driver = driver;
67      this.url = url;
68      this.username = username;
69      this.password = password;
70    }
71  
72    public UnpooledDataSource(String driver, String url, Properties driverProperties) {
73      this.driver = driver;
74      this.url = url;
75      this.driverProperties = driverProperties;
76    }
77  
78    public UnpooledDataSource(ClassLoader driverClassLoader, String driver, String url, String username,
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  
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);
97    }
98  
99    @Override
100   public Connection getConnection(String username, String password) throws SQLException {
101     return doGetConnection(username, password);
102   }
103 
104   @Override
105   public void setLoginTimeout(int loginTimeout) {
106     DriverManager.setLoginTimeout(loginTimeout);
107   }
108 
109   @Override
110   public int getLoginTimeout() {
111     return DriverManager.getLoginTimeout();
112   }
113 
114   @Override
115   public void setLogWriter(PrintWriter logWriter) {
116     DriverManager.setLogWriter(logWriter);
117   }
118 
119   @Override
120   public PrintWriter getLogWriter() {
121     return DriverManager.getLogWriter();
122   }
123 
124   public ClassLoader getDriverClassLoader() {
125     return driverClassLoader;
126   }
127 
128   public void setDriverClassLoader(ClassLoader driverClassLoader) {
129     this.driverClassLoader = driverClassLoader;
130   }
131 
132   public Properties getDriverProperties() {
133     return driverProperties;
134   }
135 
136   public void setDriverProperties(Properties driverProperties) {
137     this.driverProperties = driverProperties;
138   }
139 
140   public String getDriver() {
141     return driver;
142   }
143 
144   public void setDriver(String driver) {
145     this.driver = driver;
146   }
147 
148   public String getUrl() {
149     return url;
150   }
151 
152   public void setUrl(String url) {
153     this.url = url;
154   }
155 
156   public String getUsername() {
157     return username;
158   }
159 
160   public void setUsername(String username) {
161     this.username = username;
162   }
163 
164   public String getPassword() {
165     return password;
166   }
167 
168   public void setPassword(String password) {
169     this.password = password;
170   }
171 
172   public Boolean isAutoCommit() {
173     return autoCommit;
174   }
175 
176   public void setAutoCommit(Boolean autoCommit) {
177     this.autoCommit = autoCommit;
178   }
179 
180   public Integer getDefaultTransactionIsolationLevel() {
181     return defaultTransactionIsolationLevel;
182   }
183 
184   public void setDefaultTransactionIsolationLevel(Integer defaultTransactionIsolationLevel) {
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() {
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) {
209     this.defaultNetworkTimeout = defaultNetworkTimeout;
210   }
211 
212   private Connection doGetConnection(String username, String password) throws SQLException {
213     Properties props = new Properties();
214     if (driverProperties != null) {
215       props.putAll(driverProperties);
216     }
217     if (username != null) {
218       props.setProperty("user", username);
219     }
220     if (password != null) {
221       props.setProperty("password", password);
222     }
223     return doGetConnection(props);
224   }
225 
226   private Connection doGetConnection(Properties properties) throws SQLException {
227     initializeDriver();
228     Connection connection = DriverManager.getConnection(url, properties);
229     configureConnection(connection);
230     return connection;
231   }
232 
233   private void initializeDriver() throws SQLException {
234     try {
235       registeredDrivers.computeIfAbsent(driver, x -> {
236         Class<?> driverType;
237         try {
238           if (driverClassLoader != null) {
239             driverType = Class.forName(x, true, driverClassLoader);
240           } else {
241             driverType = Resources.classForName(x);
242           }
243           Driver driverInstance = (Driver) driverType.getDeclaredConstructor().newInstance();
244           DriverManager.registerDriver(new DriverProxy(driverInstance));
245           return driverInstance;
246         } catch (Exception e) {
247           throw new RuntimeException("Error setting driver on UnpooledDataSource.", e);
248         }
249       });
250     } catch (RuntimeException re) {
251       throw new SQLException("Error setting driver on UnpooledDataSource.", re.getCause());
252     }
253   }
254 
255   private void configureConnection(Connection conn) throws SQLException {
256     if (defaultNetworkTimeout != null) {
257       conn.setNetworkTimeout(Executors.newSingleThreadExecutor(), defaultNetworkTimeout);
258     }
259     if (autoCommit != null && autoCommit != conn.getAutoCommit()) {
260       conn.setAutoCommit(autoCommit);
261     }
262     if (defaultTransactionIsolationLevel != null) {
263       conn.setTransactionIsolation(defaultTransactionIsolationLevel);
264     }
265   }
266 
267   private static class DriverProxy implements Driver {
268     private final Driver driver;
269 
270     DriverProxy(Driver d) {
271       this.driver = d;
272     }
273 
274     @Override
275     public boolean acceptsURL(String u) throws SQLException {
276       return this.driver.acceptsURL(u);
277     }
278 
279     @Override
280     public Connection connect(String u, Properties p) throws SQLException {
281       return this.driver.connect(u, p);
282     }
283 
284     @Override
285     public int getMajorVersion() {
286       return this.driver.getMajorVersion();
287     }
288 
289     @Override
290     public int getMinorVersion() {
291       return this.driver.getMinorVersion();
292     }
293 
294     @Override
295     public DriverPropertyInfo[] getPropertyInfo(String u, Properties p) throws SQLException {
296       return this.driver.getPropertyInfo(u, p);
297     }
298 
299     @Override
300     public boolean jdbcCompliant() {
301       return this.driver.jdbcCompliant();
302     }
303 
304     @Override
305     public Logger getParentLogger() {
306       return Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
307     }
308   }
309 
310   @Override
311   public <T> T unwrap(Class<T> iface) throws SQLException {
312     throw new SQLException(getClass().getName() + " is not a wrapper.");
313   }
314 
315   @Override
316   public boolean isWrapperFor(Class<?> iface) throws SQLException {
317     return false;
318   }
319 
320   @Override
321   public Logger getParentLogger() {
322     // requires JDK version 1.6
323     return Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
324   }
325 
326 }