View Javadoc
1   /*
2    * #%L
3    * This file is part of a universal JDBC Connection factory.
4    * %%
5    * Copyright (C) 2014 - 2015 Michael Beiter <michael@beiter.org>
6    * %%
7    * All rights reserved.
8    * .
9    * Redistribution and use in source and binary forms, with or without
10   * modification, are permitted provided that the following conditions are met:
11   *     * Redistributions of source code must retain the above copyright
12   *       notice, this list of conditions and the following disclaimer.
13   *     * Redistributions in binary form must reproduce the above copyright
14   *       notice, this list of conditions and the following disclaimer in the
15   *       documentation and/or other materials provided with the distribution.
16   *     * Neither the name of the copyright holder nor the names of the
17   *       contributors may be used to endorse or promote products derived
18   *       from this software without specific prior written permission.
19   * .
20   * .
21   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
25   * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31   * #L%
32   */
33  package org.beiter.michael.db;
34  
35  import org.apache.commons.dbcp2.DriverManagerConnectionFactory;
36  import org.apache.commons.dbcp2.PoolableConnection;
37  import org.apache.commons.dbcp2.PoolableConnectionFactory;
38  import org.apache.commons.dbcp2.PoolingDataSource;
39  import org.apache.commons.lang3.Validate;
40  import org.apache.commons.pool2.impl.GenericObjectPool;
41  import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
42  import org.slf4j.Logger;
43  import org.slf4j.LoggerFactory;
44  
45  import javax.naming.Context;
46  import javax.naming.InitialContext;
47  import javax.naming.NamingException;
48  import javax.sql.DataSource;
49  import java.sql.Connection;
50  import java.sql.SQLException;
51  import java.util.Properties;
52  import java.util.concurrent.ConcurrentHashMap;
53  import java.util.concurrent.ConcurrentMap;
54  
55  /**
56   * This class creates and manages JDBC Connection instances from:
57   * <ul>
58   * <li>A named JNDI managed connection</li>
59   * <li>A connection pool that is maintained by this factory</li>
60   * </ul>
61   */
62  public final class ConnectionFactory {
63  
64      /**
65       * The logger object for this class
66       */
67      private static final Logger LOG = LoggerFactory.getLogger(ConnectionFactory.class);
68  
69  
70      /**
71       * This hash map stores the generated pools per connection
72       */
73      private static final ConcurrentHashMap<String, PoolingDataSource<PoolableConnection>> CONNECTION_POOLS =
74              new ConcurrentHashMap<>();
75  
76      /**
77       * A private constructor to prevent instantiation of this class
78       */
79      private ConnectionFactory() {
80      }
81  
82      /**
83       * Return a Connection instance for a JNDI managed JDBC connection.
84       *
85       * @param jndiName The JNDI connection name
86       * @return a JDBC connection
87       * @throws FactoryException         When the connection cannot be retrieved from JNDI
88       * @throws NullPointerException     When {@code jndiName} is null
89       * @throws IllegalArgumentException When {@code jndiName} is empty
90       */
91      public static Connection getConnection(final String jndiName)
92              throws FactoryException {
93  
94          Validate.notBlank(jndiName, "The validated character sequence 'jndiName' is null or empty");
95  
96          try {
97              // the initial context is created from the provided JNDI settings
98              final Context context = new InitialContext();
99  
100             // retrieve a data source object, close the context as it is no longer needed, and return the connection
101             final Object namedObject = context.lookup(jndiName);
102             if (DataSource.class.isInstance(namedObject)) {
103                 final DataSource dataSource = (DataSource) context.lookup(jndiName);
104                 context.close();
105 
106                 return dataSource.getConnection();
107             } else {
108                 final String error = "The JNDI name '" + jndiName + "' does not reference a SQL DataSource."
109                         + " This is a configuration issue.";
110                 LOG.warn(error);
111                 throw new FactoryException(error);
112             }
113         } catch (SQLException | NamingException e) {
114             final String error = "Error retrieving JDBC connection from JNDI: " + jndiName;
115             LOG.warn(error);
116             throw new FactoryException(error, e);
117         }
118     }
119 
120     /**
121      * Return a Connection instance from a pool that manages JDBC driver based connections.
122      * <p>
123      * The driver-based connection are managed in a connection pool. The pool is created using the provided properties
124      * for both the connection and the pool spec. Once the pool has been created, it is cached (based on URL and
125      * username), and can no longer be changed. Subsequent calls to this method will return a connection from the
126      * cached pool, and changes in the pool spec (e.g. changes to the size of the pool) will be ignored.
127      *
128      * @param poolSpec A connection pool spec that has the driver and url configured as non-empty strings
129      * @return a JDBC connection
130      * @throws FactoryException         When the connection cannot be retrieved from the pool, or the pool cannot be
131      *                                  created
132      * @throws NullPointerException     When the {@code poolSpec}, {@code poolSpec.getDriver()}, or
133      *                                  {@code poolSpec.getUrl()} are {@code null}
134      * @throws IllegalArgumentException When {@code poolSpec.getDriver()} or {@code poolSpec.getUrl()} are empty
135      */
136     public static Connection getConnection(final ConnectionProperties poolSpec)
137             throws FactoryException {
138 
139         Validate.notNull(poolSpec, "The validated object 'poolSpec' is null");
140         Validate.notBlank(poolSpec.getDriver(),
141                 "The validated character sequence 'poolSpec.getDriver()' is null or empty");
142         Validate.notBlank(poolSpec.getUrl(), "The validated character sequence 'poolSpec.getUrl()' is null or empty");
143 
144         // no need for defensive copies of Strings
145 
146         final String driver = poolSpec.getDriver();
147         final String url = poolSpec.getUrl();
148         // CHECKSTYLE:OFF
149         // this particular set of inline conditions is easy to read :-)
150         final String username = poolSpec.getUsername() == null ? "" : poolSpec.getUsername();
151         final String password = poolSpec.getPassword() == null ? "" : poolSpec.getPassword();
152         // CHECKSTYLE:OFF
153 
154         // Load the database driver (if not already done)
155         loadDriver(driver);
156 
157         // create the hash map required for the connection pool username + password
158         final ConcurrentMap<String, String> properties = new ConcurrentHashMap<>();
159         properties.put("user", username);
160         properties.put("password", password);
161 
162         // we keep a separate pool per connection
163         // a connection is identified by the URL, the username, and the password
164         final String key = String.format("%s:%s", url, username);
165 
166         // avoid if possible to create the pool multiple times, and store the data source pool for later use
167         if (!CONNECTION_POOLS.containsKey(key)) {
168             synchronized (ConnectionFactory.class) {
169                 if (!CONNECTION_POOLS.containsKey(key)) {
170 
171                     // this call is thread safe even without the double if check and extra synchronization. However, it
172                     // might happen that the pool is created multiple times. While additional copies would be simply
173                     // thrown away, we might run into problems in case that, for instance, the number of connections
174                     // from the same user / machine are restricted on the DB server.
175                     // While this does not happen a lot (it only happens if there is not already an entry and multiple
176                     // threads race this block and lose), it could still lead to a failure, and we must take this double
177                     // sync workaround. There is a solution for Java 8 - see below.
178                     CONNECTION_POOLS.putIfAbsent(key, getPoolingDataSource(url, properties, poolSpec));
179                 }
180             }
181         }
182         // This would solve the problem of multiple pools being created and all but one being throws away, but it
183         // does not work before Java 8 because the "computeIfAbsent()" method with the lambda function is not
184         // available before Java 8:
185         // TODO: add the pooled data source with the "computeIfAbsent()" method to improve performance in Java 8
186         //CONNECTION_POOLS.computeIfAbsent(key, k -> getPoolingDataSource(url, properties, poolSpec));
187 
188         try {
189             return CONNECTION_POOLS.get(key).getConnection();
190         } catch (SQLException e) {
191             final String error = "Error retrieving JDBC connection from pool: " + key;
192             LOG.warn(error);
193             throw new FactoryException(error, e);
194         }
195     }
196 
197     /**
198      * Resets the internal state of the factory.
199      * <p>
200      * <strong>This method does not release any resources that have been borrowed from the connection pools managed
201      * by this factory.</strong> To avoid resource leaks, you <strong>must</strong> close / return all connections to
202      * their pools before calling this method.
203      */
204     public static void reset() {
205 
206         // Unset the cached connections
207         CONNECTION_POOLS.clear();
208     }
209 
210     /**
211      * Make sure that the database driver exists
212      *
213      * @param driver The JDBC driver class to load
214      * @throws FactoryException When the driver cannot be loaded
215      */
216     private static void loadDriver(final String driver) throws FactoryException {
217 
218         // assert in private method
219         assert driver != null : "The driver cannot be null";
220 
221         LOG.debug("Loading the database driver '" + driver + "'");
222 
223         // make sure the driver is available
224         try {
225             Class.forName(driver);
226         } catch (ClassNotFoundException e) {
227             final String error = "Error loading JDBC driver class: " + driver;
228             LOG.warn(error, e);
229             throw new FactoryException(error, e);
230         }
231     }
232 
233     /**
234      * Get a pooled data source for the provided connection parameters.
235      *
236      * @param url        The JDBC database URL of the form <code>jdbc:subprotocol:subname</code>
237      * @param properties A list of key/value configuration parameters to pass as connection arguments. Normally at
238      *                   least a "user" and "password" property should be included
239      * @param poolSpec   A connection pool spec
240      * @return A pooled database connection
241      */
242     private static PoolingDataSource<PoolableConnection> getPoolingDataSource(final String url,
243                                                                               final ConcurrentMap<String, String> properties,
244                                                                               final ConnectionProperties poolSpec) {
245 
246         // assert in private method
247         assert url != null : "The url cannot be null";
248         assert properties != null : "The properties cannot be null";
249         assert poolSpec != null : "The pol spec cannot be null";
250 
251         LOG.debug("Creating new pooled data source for '" + url + "'");
252 
253         // convert the properties hashmap to java properties
254         final Properties props = new Properties();
255         props.putAll(properties);
256 
257         // create a Apache DBCP pool configuration from the pool spec
258         final GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
259         poolConfig.setMaxTotal(poolSpec.getMaxTotal());
260         poolConfig.setMaxIdle(poolSpec.getMaxIdle());
261         poolConfig.setMinIdle(poolSpec.getMinIdle());
262         poolConfig.setMaxWaitMillis(poolSpec.getMaxWaitMillis());
263         poolConfig.setTestOnCreate(poolSpec.isTestOnCreate());
264         poolConfig.setTestOnBorrow(poolSpec.isTestOnBorrow());
265         poolConfig.setTestOnReturn(poolSpec.isTestOnReturn());
266         poolConfig.setTestWhileIdle(poolSpec.isTestWhileIdle());
267         poolConfig.setTimeBetweenEvictionRunsMillis(poolSpec.getTimeBetweenEvictionRunsMillis());
268         poolConfig.setNumTestsPerEvictionRun(poolSpec.getNumTestsPerEvictionRun());
269         poolConfig.setMinEvictableIdleTimeMillis(poolSpec.getMinEvictableIdleTimeMillis());
270         poolConfig.setSoftMinEvictableIdleTimeMillis(poolSpec.getSoftMinEvictableIdleTimeMillis());
271         poolConfig.setLifo(poolSpec.isLifo());
272 
273 
274         // create the pool and assign the factory to the pool
275         final org.apache.commons.dbcp2.ConnectionFactory connFactory = new DriverManagerConnectionFactory(url, props);
276         final PoolableConnectionFactory poolConnFactory = new PoolableConnectionFactory(connFactory, null);
277         poolConnFactory.setDefaultAutoCommit(poolSpec.isDefaultAutoCommit());
278         poolConnFactory.setDefaultReadOnly(poolSpec.isDefaultReadOnly());
279         poolConnFactory.setDefaultTransactionIsolation(poolSpec.getDefaultTransactionIsolation());
280         poolConnFactory.setCacheState(poolSpec.isCacheState());
281         poolConnFactory.setValidationQuery(poolSpec.getValidationQuery());
282         poolConnFactory.setMaxConnLifetimeMillis(poolSpec.getMaxConnLifetimeMillis());
283         final GenericObjectPool<PoolableConnection> connPool = new GenericObjectPool<>(poolConnFactory, poolConfig);
284         poolConnFactory.setPool(connPool);
285 
286         // create a new pooled data source
287         return new PoolingDataSource<>(connPool);
288     }
289 }