Menu

NullPointerException when we use craftsman.spy JDBC logging driver

Help
2016-01-06
2016-01-13
  • Mihai Dinca-Panaitescu

    We register the spy driver (http://zer0.free.fr/craftsman/spy.php) like:

    System.setProperty("spy.driver", "net.ucanaccess.jdbc.UcanaccessDriver");
    DriverManager.registerDriver(new SpyDriver());
    System.setProperty("spy.log", "D:/logs/jdbc-spy.log");

    When we try to connect we get:

    java.lang.NullPointerException
    at craftsman.spy.SpyConnection.getMetaData(SpyConnection.java:252)
    at net.ucanaccess.jdbc.DBReference.getHSQLDBConnection(DBReference.java:346)
    at net.ucanaccess.jdbc.UcanaccessDriver.connect(UcanaccessDriver.java:190)

    meaning .getHSQLDBConnection returns null
    
     
  • Marco Amadei

    Marco Amadei - 2016-01-06

    The stacktrace you posted can't be generated by the situation you described, unless you're using a very out-of-date ucanaccess version. Please upgrade ucanaccess to the the latest version and send the resulting stacktrace again, along the jdbc url used (you omitted the call to the DriverManager).

     

    Last edit: Marco Amadei 2016-01-06
  • Mihai Dinca-Panaitescu

    Hi.

    I call DriverManager.getConnection(...) . The ideea is the connection works when we do not register spy driver and it does not when we register it. I tested also with the last version and get :

    java.util.concurrent.ExecutionException: net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.3.1 nullnet.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.3.1 nulljava.lang.NullPointerException

    UCAExc:::3.0.3.1 null
    net.ucanaccess.jdbc.UcanaccessDriver.connect(UcanaccessDriver.java:260)
    craftsman.spy.SpyDriver.connect(SpyDriver.java:265)
    java.sql.DriverManager.getConnection(DriverManager.java:571)
    java.sql.DriverManager.getConnection(DriverManager.java:215)
    .....
    null
    craftsman.spy.SpyConnection.getMetaData(SpyConnection.java:252)
    net.ucanaccess.jdbc.DBReference.getHSQLDBConnection(DBReference.java:444)
    net.ucanaccess.jdbc.UcanaccessDriver.connect(UcanaccessDriver.java:227)
    craftsman.spy.SpyDriver.connect(SpyDriver.java:265)
    java.sql.DriverManager.getConnection(DriverManager.java:571)
    java.sql.DriverManager.getConnection(DriverManager.java:215)
    .....

    The jdbc url used is like :
    jdbc:ucanaccess://D:/Temp/access/test.accdb;showschema=true

     
  • Marco Amadei

    Marco Amadei - 2016-01-06

    Okay, I see. This time the stacktrace is meaningful. Once I have reproduced the issue within spy, I'll let you know my findings(today I can't, but hopefully tomorrow I should be able to).

     
  • Marco Amadei

    Marco Amadei - 2016-01-12

    Sorry for the late answer, this code works fine for me:

    public static void main(String[] args) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
            System.setProperty("spy.driver", "net.ucanaccess.jdbc.UcanaccessDriver");
            DriverManager.registerDriver(new SpyDriver());
            System.setProperty("spy.log", "D:/jdbc-spy.log");
            Connection conn=DriverManager.getConnection("jdbc:spy:net.ucanaccess.jdbc.UcanaccessDriver:ucanaccess://C:\\db\\test.accdb");
            Statement st=conn.createStatement();
            ResultSet rs=st.executeQuery("select * from ab");
            while(rs.next()){
                ...
            }
            ...
        }
    
     

    Last edit: Marco Amadei 2016-01-12
  • Mihai Dinca-Panaitescu

    Hi.

    Sorry to forget about this.

    We do one more thing just before we register the Spy driver, we deregister all existing drivers , meaning ucanaccess driver is also unregistered:

                Enumeration<Driver> drivers = DriverManager.getDrivers();
                while (drivers.hasMoreElements()) {
                    DriverManager.deregisterDriver(drivers.nextElement());
                }
    

    and we use :

         Connection conn=DriverManager.getConnection("jdbc:ucanaccess://C:\\db\\test.accdb");
    

    without spy.

    We can connect like this to any jdbc driver we have (using the normal url) like oracle, mysql, postgresql, derby and so on, except ucanaccess.

     
  • Marco Amadei

    Marco Amadei - 2016-01-13

    Nothing wrong in the ucanaccess behaviour.
    So, you deregister each driver forcing each connection be wrapped by spy. Unfortunately ucanaccess in turn wraps another driver: the hsqldb driver. In the wrapping chain spy-> ucanaccess-> spy-> hsqldb spy fails some association. Therefore spy invokes ucanaccess to connect to an hsqldb url(resulting in a null returned by ucanaccess and so in a NullPointerException).
    You know, supporting this approach wouldn't be worth for me but...

    Immediately after the drivers' deregistration, you have to re-re-register the hsqldb driver (which fortunately isn't in your list and so hopefully it's used just by ucanaccess):

    DriverManager.registerDriver(new org.hsqldb.jdbc.JDBCDriver());
    Those young java hackers...

     

    Last edit: Marco Amadei 2016-01-13
  • Mihai Dinca-Panaitescu

    Yes. It makes sense if a driver uses another driver. I will do as suggested.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.