Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Use context classLoader to load driver, if present. Fixes (swaldman#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
elrodro83 authored and asanguinetti committed Mar 8, 2023
1 parent da42774 commit f67735a
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions src/java/com/mchange/v2/c3p0/DriverManagerDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,16 @@ private synchronized void ensureDriverLoaded() throws SQLException
if (! isDriverClassLoaded())
{
if (driverClass != null)
Class.forName( driverClass );
{
Class driverCls = loadDriver(driverClass);
if(!driverCls.getClassLoader().equals(this.getClass().getClassLoader()))
{
logger.warning( "Driver '" + driverClass + "' was loaded with a classloader different than C3P0's. Forcing lookup by name sonce the DriverManager won't fin it from C3P0's classloader." );
logger.finest("Driver '" + driverClass + "' classloader: " + driverCls.getClassLoader().toString());
logger.finest("C3P0 classloader: " + this.getClass().getClassLoader().toString());
setForceUseNamedDriverClass(true);
}
}
setDriverClassLoaded( true );
}
}
Expand All @@ -151,6 +160,25 @@ private synchronized void ensureDriverLoaded() throws SQLException
}
}

private Class loadDriver(String driverClass) throws ClassNotFoundException
{
try
{
return Class.forName(driverClass);
}
catch (ClassNotFoundException e)
{
if (Thread.currentThread().getContextClassLoader() != null)
{
return Class.forName(driverClass, true, Thread.currentThread().getContextClassLoader());
}
else
{
throw e;
}
}
}

// should NOT be sync'ed -- driver() is sync'ed and that's enough
// sync'ing the method creates the danger that one freeze on connect
// blocks access to the entire DataSource
Expand Down Expand Up @@ -273,9 +301,9 @@ private synchronized Driver driver() throws SQLException
logger.finer( "Circumventing DriverManager and instantiating driver class '" + driverClass +
"' directly. (forceUseNamedDriverClass = " + forceUseNamedDriverClass + ")" );

try
{
driver = (Driver) Class.forName( driverClass ).newInstance();
try
{
driver = (Driver) loadDriver(driverClass).newInstance();
this.setDriverClassLoaded( true );
}
catch (Exception e)
Expand Down

0 comments on commit f67735a

Please sign in to comment.