0

After reading about UCanAccess in this question I've started building it into my app. It seems to be working, but I've noticed an oddity.

I put in the following code at start-up, and found that it doesn't print anything, unless I preface it with a Class.forName("net.ucanaccess.jdbc.UcanaccessDriver") or other reference to the class. As long as I include that line, it will print out "driver found :..." as expected.

Enumeration<Driver> x = DriverManager.getDrivers();
while (x.hasMoreElements()) {
    Driver driver = x.nextElement();
    if (driver instanceof UcanaccessDriver) {
        System.out.println("driver found: " + driver.toString());
    }
}

However, I've seen it in comments on other UCanAccess questions that the Class.forName() shouldn't be needed before regular calls to DriverManager.getDriver() or whatever else. Is there a step I've missed?

Community
  • 1
  • 1
DVA
  • 322
  • 2
  • 11
  • 1
    In general a Driver class instance is seldom useful (almost never) for the applicative code so I'm wondering where you read something about DriverManager.getDriver() and Ucanaccess. I can't reproduce your example. What's your java version? Can you get the connection if you comment if( driver instanceof UcanaccessDriver block){...}(which has the same effect than a class.forName) – jamadei Nov 14 '15 at 17:10
  • I am also unable to recreate your issue. I tried your code under Java 7 (JRE 1.7.0_67) and Java 8 (JRE 1.8.0_45) and they both listed the UCanAccess driver without having first done a `Class.forName()`. – Gord Thompson Nov 14 '15 at 18:03
  • My JDK version is 1.7.0_80, my UcanAccess version is 2.0.2-1. It's a reproducible error within my project, so I suppose if you guys can't get it to happen then it's probably something to do with our infrastructure. There are a few classloaders floating around the system, so perhaps it's a matter of the jar being on the wrong one until I explicitly call for it. Thank you for at least doing a preliminary investigation, anyway. – DVA Nov 18 '15 at 19:00
  • Oh, and the driver was only being used to do `connect()` the way the driver manager does. Since things were behaving strangely I was trying to do a little debugging by bringing more of the steps into my code where I can intersperse printlns and such. – DVA Nov 18 '15 at 19:03
  • Maybe you need to call the `getDrivers()` twice as described in this comment: https://github.com/apache/tomcat/blob/35b2d79ad26535deedcc0c6bea2a057138e1bd30/java/org/apache/catalina/loader/JdbcLeakPrevention.java#L47-L55 – Oliv Oct 14 '20 at 10:41

0 Answers0