DriverManager attempts to load the wrong driver?
843859Sep 26 2007 — edited Sep 26 2007This is odd.
I'm using Java 5 to write a program to test connectivity to JDBC and ODBC services running on target machines, reporting the appropriate error when a failure occurs. In order to test ODBC connectivity from a Java application, I'm using Sun's JDBC-ODBC bridge. I know that the bridge is somewhat buggy and not recommended for production environments, but I don't think it's the cause of this particular problem.
My issue is that when an ODBC failure occurs during connect time, the SQLException thrown appears to be coming from my native JDBC driver, not Sun's JDBC-ODBC driver. It appears as if the wrong driver was used to establish the connection, even though my driver URL is valid.
This occurs occasionally. It only occurs if I've also loaded the native JDBC driver. If I skip my JDBC test entirely and only test with the JDBC-ODBC bridge, then the exception is accurate, and comes from the JDBC-JDBC bridge driver. And when the server is up and running and no exception occurs, the JDBC-ODBC bridge is loaded successfully and the test passes.
It seems like during the problem case, the JDBC-ODBC connection fails, and so the DriverManager attempts to find another suitable driver. For some reason, it chooses my native JDBC driver, even though the driver URL is totally different. This seems like a bug in Driver or DriverManager, but perhaps I'm misunderstanding the situation.
Some code would help. I am new to the forum and if someone could tell me how to use the code tags I'd appreciate it -- search did not help me. Sorry for that!
Here's the code where I run the ODBC test:
// Build the JDBC URL for DSN-less connections.
String driverUrl = getMyOdbcUrl(); // this builds a URL that looks like "jdbc:odbc:Driver={My ODBC driver name and props}"
try {
// Obtain an ODBC connection
// Assumes the ODBC driver has been installed.
Class.forName("sun.jdbc.jdbc.JdbcOdbcDriver");
conn = DriverManager.getConnection(driverUrl, username, password);
...
} catch(SQLException e) {
e.printStackTrace();
return;
}
The stack trace includes the following (company info excluded):
com.<Company>.jdbc.<Company>SQLException: The required url format is jdbc<Company>:<Proprietary URL format>
at com.<Company>.jdbc.<CompanyDriver>.connect(...)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
...
and there is no stack trace from the jdbc-odbc bridge!
This is not expected, as this JDBC driver should not have been used. I could understand this happening if the driver URL happens to match the jdbc-odbc bridge URL, but it does not.
Does anyone have any ideas what would cause this problem, or has anyone seen it before? Need more info -- let me know, thanks.