commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Lev Assinovsky <...@peterlink.ru>
Subject Re: [DBCP] - 2 Serious Bugs
Date Fri, 29 Mar 2002 17:57:27 GMT
The following is from DriverManager's doc:

public static Driver getDriver(String url)
                        throws SQLException

       Attempts to locate a driver that understands the given URL. The
DriverManager attempts to select an appropriate driver from the
       set of registered JDBC drivers. 


So to get driver via url it must be registered first.
But to register it you have to have the driver's class instance!
 
James House wrote:
> 
> In the "BasicDataSource.createDataSource()" method,  there are 2 serious
> bugs in the following block of code:
> 
> ===========================================
>          // Load the JDBC driver class
>          Class driverClass = null;
>          try {
>              driverClass = Class.forName(driverClassName);
>          } catch (Throwable t) {
>              String message = "Cannot load JDBC driver class '" +
>                  driverClassName + "'";
>              getLogWriter().println(message);
>              t.printStackTrace(getLogWriter());
>              throw new SQLException(message);
>          }
> 
>          // Create a JDBC driver instance
>          Driver driver = null;
>          try {
>              driver = (Driver) driverClass.newInstance();
>          } catch (Throwable t) {
>              String message = "Cannot create JDBC driver of class '" +
>                  driverClassName + "'";
>              getLogWriter().println(message);
>              t.printStackTrace(getLogWriter());
>              throw new SQLException(message);
>          }
> ===========================================
> 
> The if method createDataSource() encounters a problem loading or
> instantiating the JDBC driver, it calls the method
> getLogWriter().  getLogWriter() in turn calls createDataSource(), thus
> creating an infinite loop, and a stack-overflow exception.  Perhaps
> createDataSource() needs to simply log it's errors to stderr ???
> 
> The second bug in this block of code causes the creation of the data source
> to fail, even though there's no real problem.
> 
> This problem stems from the fact that many JDBC drivers (well, at least 2
> that I use regularly) do not have a public constructor, and therefore you
> can't call newInstance() on them. (the instantiate themselves in a static
> {} block, as they are loaded and registered with driver manager).
> 
> The EASY solution is to replace the second try block body with this call:
> 
>          driver = DriverManager.getDriver(url);
> 
> Simple huh?
> 
> If you want to "fix" the two bugs in the manner I've suggested, the
> replacement of the block outlined above would be:
> ===========================================
> import java.sql.DriverManager;
> ...
> 
>          // Load the JDBC driver class
>          Class driverClass = null;
>          try {
>              driverClass = Class.forName(driverClassName);
>          } catch (Throwable t) {
>              String message = "Cannot load JDBC driver class '" +
>                  driverClassName + "'";
>              System.err.println(message);
>              t.printStackTrace();
>              throw new SQLException(message);
>          }
> 
>          // Create a JDBC driver instance
>          Driver driver = null;
>          try {
>            driver = DriverManager.getDriver(url);
>          } catch (Throwable t) {
>              String message = "Cannot get a reference to JDBC driver of
> class '" +
>                  driverClassName + "' - " + t.getMessage();
>              System.err.println(message);
>              t.printStackTrace();
>              throw new SQLException(message);
>          }
> ===========================================
> 
> BTW: I REALLY appreciate the recent changes that have made the pool
> (getConnection()) throw SQLException.
> 
> James
> 
> --
> To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>

-- 
Lev Assinovsky                Peterlink Web
Programmer		      St. Petersburg, Russia
Tel/Fax: +7 812 3275343       197022 ul.Chapigina 7Á
E-mail: lev@peterlink.ru

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message