#95 DriverManagerDataSource's driverClass not effectively used

v1.0 (example)
closed-fixed
nobody
None
8
2014-03-19
2011-07-07
meinc
No

In c3p0 Version 0.9.1.2 the class com.mchange.v2.c3p.DriverManagerDataSource does not effectively take the driver class driverClass into account.

The Problem is that method ensureDriverLoaded() does not set driver if driverClass has been explicitly been set and successfully loaded. It solely sets the flag driver_class_loaded. Thus it is left up to method driver() to again lookup a driver. It does so via java.sql.DriverManager which accidently returns the first driver that is able to handle a given URL. If there is more than one Driver able to handle the URL it may happen that an other driver is used than the intended one, e.g. Oracle rather P6SPY driver.

Furthermore clearDriver() looks quite strange. If it really makes sense to clear the driver if the url is chaged, it should also handel a explicitly set driverClass appropriately, e.g. additionally reset the driver_class_loaded flag.

Porposed fix:

private void ensureDriverLoaded() throws SQLException
{
try
{
if (! isDriverClassLoaded())
{
if (driverClass != null)
this.driver = (Driver)Class.forName( driverClass );
setDriverClassLoaded( true );
}
}
catch (ClassNotFoundException e)
{
if (logger.isLoggable(MLevel.WARNING))
logger.log(MLevel.WARNING, "Could not load driverClass " + driverClass, e);
}
}

Discussion

  • meinc
    meinc
    2011-07-07

    • priority: 5 --> 8
     
  • Steve Waldman
    Steve Waldman
    2012-05-13

    What you are after is a very different behavior than is implemented. All DriverManagerDataSource does at present is assure that a specified Driver class is loaded, that it is available for resolving a JDBC URL. As you say, it does not interpose itself into the resolution among drivers given the set of drivers available, if a JDBC URL can be handled by multiple drivers.

    As you suggest, it would not be hard to modify the behavior to the one you prefer. But doing so might break working installations where the driver class set is in fact not the appropriate driver for the URL specified, but where the correct driver class has been loaded and made available to handle jdbc URLs by other means. Maybe there should be a separate input param for a "strict" mode that guarantees, and insists upon, using the Driver provided. Hmm.

     
  • Steve Waldman
    Steve Waldman
    2014-03-19

    as of c3p0-0.9.5-pre7 (within a few days), c3p0 will support a forceUseNamedDriverClass config param to enable the behavior you prefer. sorry for the long delay!

     
  • Steve Waldman
    Steve Waldman
    2014-03-19

    • status: open --> closed-fixed
    • Group: --> v1.0 (example)