Commons Dbcp
  1. Commons Dbcp
  2. DBCP-214

Basic datasource should respect the context classloader

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.2.2
    • Fix Version/s: 1.3
    • Labels:
      None

      Description

      I tried to use DBCP in an OSGi environment. This this not possible at the moment, as OSGi uses different classloaders and DBCP does not respect this.
      The easiest way to solve this problem would be to use the contextclassloader in case of a ClassNotFoundException

        Activity

        Hide
        Phil Steitz added a comment -

        Patches welcome

        Show
        Phil Steitz added a comment - Patches welcome
        Hide
        Bernd Kolb added a comment -

        Version: 1.2.2
        Class: org.apache.commons.dbcp.BasicDataSouce
        Method: protected synchronized DataSource createDataSource() (changes made between //### START CHANGE ### and //### END CHANGE ###

        Changed code:
        //### START CHANGE ###
        Class driverFromCCL = null;
        if (driverClassName != null) {
        try {
        try

        { Class.forName(driverClassName); }

        catch (ClassNotFoundException e)

        { driverFromCCL = Thread.currentThread().getContextClassLoader().loadClass(driverClassName); }

        } catch (Throwable t)

        { String message = "Cannot load JDBC driver class '" + driverClassName + "'"; logWriter.println(message); t.printStackTrace(logWriter); throw new SQLNestedException(message, t); }

        }

        // Create a JDBC driver instance
        Driver driver = null;
        try {
        if (driverFromCCL!= null) {
        // 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"); }

        }else

        { driver = DriverManager.getDriver(url); }

        //### END CHANGE ###

        If you need more information, please let me know

        Cheers,

        Bernd

        Show
        Bernd Kolb added a comment - Version: 1.2.2 Class: org.apache.commons.dbcp.BasicDataSouce Method: protected synchronized DataSource createDataSource() (changes made between //### START CHANGE ### and //### END CHANGE ### Changed code: //### START CHANGE ### Class driverFromCCL = null; if (driverClassName != null) { try { try { Class.forName(driverClassName); } catch (ClassNotFoundException e) { driverFromCCL = Thread.currentThread().getContextClassLoader().loadClass(driverClassName); } } catch (Throwable t) { String message = "Cannot load JDBC driver class '" + driverClassName + "'"; logWriter.println(message); t.printStackTrace(logWriter); throw new SQLNestedException(message, t); } } // Create a JDBC driver instance Driver driver = null; try { if (driverFromCCL!= null) { // 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"); } }else { driver = DriverManager.getDriver(url); } //### END CHANGE ### If you need more information, please let me know Cheers, Bernd
        Hide
        Bernd Kolb added a comment -

        Any updates on that?

        Thanks

        Bernd

        Show
        Bernd Kolb added a comment - Any updates on that? Thanks Bernd
        Hide
        Mark Thomas added a comment -

        Thanks for the patch. Sorry it it has been a while. Your patch has been applied.

        Show
        Mark Thomas added a comment - Thanks for the patch. Sorry it it has been a while. Your patch has been applied.

          People

          • Assignee:
            Unassigned
            Reporter:
            Bernd Kolb
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development