Issue Details (XML | Word | Printable)

Key: DBCP-214
Type: Bug Bug
Status: Resolved Resolved
Resolution: Fixed
Priority: Major Major
Assignee: Unassigned
Reporter: Bernd Kolb
Votes: 0
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
Commons Dbcp

Basic datasource should respect the context classloader

Created: 10/Apr/07 04:33 PM   Updated: 15/Mar/09 10:13 PM
Component/s: None
Affects Version/s: 1.2.2
Fix Version/s: 1.3

Time Tracking:
Not Specified

File Attachments:
  Size
Java Source File Licensed for inclusion in ASF works BasicDataSource.java 2007-05-15 08:33 AM Bernd Kolb 44 kB

Resolution Date: 15/Mar/09 10:13 PM


 Description  « Hide
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

 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Phil Steitz added a comment - 12/Apr/07 06:08 AM
Patches welcome

Bernd Kolb added a comment - 15/May/07 08:33 AM
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 added a comment - 23/Aug/07 03:10 PM
Any updates on that?

Thanks

Bernd


Mark Thomas added a comment - 15/Mar/09 10:13 PM
Thanks for the patch. Sorry it it has been a while. Your patch has been applied.