commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r754776 - /commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/BasicDataSource.java
Date Sun, 15 Mar 2009 22:12:29 GMT
Author: markt
Date: Sun Mar 15 22:12:28 2009
New Revision: 754776

URL: http://svn.apache.org/viewvc?rev=754776&view=rev
Log:
Fix DBCP-214.
Fall back to trying to load drive from ContextClassLoader.
Patch provided by Bernd Kolb

Modified:
    commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/BasicDataSource.java

Modified: commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/BasicDataSource.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/BasicDataSource.java?rev=754776&r1=754775&r2=754776&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/BasicDataSource.java (original)
+++ commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/BasicDataSource.java Sun Mar
15 22:12:28 2009
@@ -1330,9 +1330,16 @@
      */
     protected ConnectionFactory createConnectionFactory() throws SQLException {
         // Load the JDBC driver class
+        Class driverFromCCL = null;
         if (driverClassName != null) {
             try {
-                Class.forName(driverClassName);
+                try {
+                    Class.forName(driverClassName);
+                } catch (ClassNotFoundException cnfe) {
+                    driverFromCCL = Thread.currentThread(
+                            ).getContextClassLoader().loadClass(
+                                    driverClassName);
+                }
             } catch (Throwable t) {
                 String message = "Cannot load JDBC driver class '" +
                     driverClassName + "'";
@@ -1345,7 +1352,16 @@
         // Create a JDBC driver instance
         Driver driver = null;
         try {
-            driver = DriverManager.getDriver(url);
+            if (driverFromCCL == null) {
+                driver = DriverManager.getDriver(url);
+            } else {
+                // Usage of DriverManager is not possible, as it does not
+                // respect the ContextClassLoader
+                driver = (Driver) driverFromCCL.newInstance();
+                if (!driver.acceptsURL(url)) {
+                    new SQLException("No suitable driver", "08001"); 
+                }
+            }
         } catch (Throwable t) {
             String message = "Cannot create JDBC driver of class '" +
                 (driverClassName != null ? driverClassName : "") +



Mime
View raw message