DriverFactory.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.dbcp2;

  18. import java.sql.Driver;
  19. import java.sql.DriverManager;
  20. import java.sql.SQLException;

  21. /*
  22.  * Creates {@link Driver} instances.
  23.  *
  24.  * @since 2.7.0
  25.  */
  26. final class DriverFactory {

  27.     static Driver createDriver(final BasicDataSource basicDataSource) throws SQLException {
  28.         // Load the JDBC driver class
  29.         Driver driverToUse = basicDataSource.getDriver();
  30.         final String driverClassName = basicDataSource.getDriverClassName();
  31.         final ClassLoader driverClassLoader = basicDataSource.getDriverClassLoader();
  32.         final String url = basicDataSource.getUrl();

  33.         if (driverToUse == null) {
  34.             Class<?> driverFromCCL = null;
  35.             if (driverClassName != null) {
  36.                 try {
  37.                     try {
  38.                         if (driverClassLoader == null) {
  39.                             driverFromCCL = Class.forName(driverClassName);
  40.                         } else {
  41.                             driverFromCCL = Class.forName(driverClassName, true, driverClassLoader);
  42.                         }
  43.                     } catch (final ClassNotFoundException cnfe) {
  44.                         driverFromCCL = Thread.currentThread().getContextClassLoader().loadClass(driverClassName);
  45.                     }
  46.                 } catch (final Exception t) {
  47.                     final String message = "Cannot load JDBC driver class '" + driverClassName + "'";
  48.                     basicDataSource.log(message, t);
  49.                     throw new SQLException(message, t);
  50.                 }
  51.             }

  52.             try {
  53.                 if (driverFromCCL == null) {
  54.                     driverToUse = DriverManager.getDriver(url);
  55.                 } else {
  56.                     // Usage of DriverManager is not possible, as it does not
  57.                     // respect the ContextClassLoader
  58.                     // N.B. This cast may cause ClassCastException which is
  59.                     // handled below
  60.                     driverToUse = (Driver) driverFromCCL.getConstructor().newInstance();
  61.                     if (!driverToUse.acceptsURL(url)) {
  62.                         throw new SQLException("No suitable driver", "08001");
  63.                     }
  64.                 }
  65.             } catch (final Exception t) {
  66.                 final String message = "Cannot create JDBC driver of class '"
  67.                         + (driverClassName != null ? driverClassName : "") + "' for connect URL '" + url + "'";
  68.                 basicDataSource.log(message, t);
  69.                 throw new SQLException(message, t);
  70.             }
  71.         }
  72.         return driverToUse;
  73.     }

  74. }