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

        Bernd Kolb created issue -
        Hide
        Phil Steitz added a comment -

        Patches welcome

        Show
        Phil Steitz added a comment - Patches welcome
        Phil Steitz made changes -
        Field Original Value New Value
        Fix Version/s 1.3 [ 12311977 ]
        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
        Bernd Kolb made changes -
        Attachment BasicDataSource.java [ 12357361 ]
        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.
        Mark Thomas made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Resolution Fixed [ 1 ]
        Phil Steitz made changes -
        Status Resolved [ 5 ] Closed [ 6 ]
        Transition Time In Source Status Execution Times Last Executer Last Execution Date
        Open Open Resolved Resolved
        705d 5h 39m 1 Mark Thomas 15/Mar/09 22:13
        Resolved Resolved Closed Closed
        336d 5h 55m 1 Phil Steitz 15/Feb/10 04:09

          People

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

            Dates

            • Created:
              Updated:
              Resolved:

              Development