Search code examples
javajdbcprepared-statementderbylog4jdbc

log4jdbc force return ConnectionSpy driver


For some reason when I use:

connection = DriverManager.getConnection("jdbc:log4jdbc:derby:/home/user/mydb");

I tend to get differing connections back. In this case, it's a mixture between:

  1. connection = (org.apache.derby.impl.jdbc.EmbedConnection) org.apache.derby.impl.jdbc.EmbedConnection@2001280736 (XID = 598), (SESSIONID = 1), (DATABASE = /home/user/mydb), (DRDAID = null) - Which I don't want

and:

  1. connection = (net.sf.log4jdbc.ConnectionSpy) net.sf.log4jdbc.ConnectionSpy@32b260fa - Which I do want

To be more specific, when I run using a public static void main method then it works and I get back #2 but when I run the same code after running the web application's startup cycle then I get #1.

Is there a way to force log4jdbc to always return net.sf.log4jdbc.ConnectionSpy?


Solution

  • I figured it out. You can force wrap the connection in this way:

    Connection temp = DriverManager.getConnection("jdbc:log4jdbc:derby:/home/user/mydb");
    connection = new net.sf.log4jdbc.ConnectionSpy(temp);