Ancient Programming

What I encounter in my software part of life is in danger of being commented upon here

Archive for November, 2009

Logging sql statements and parameters using hibernate

Posted by Jacob von Eyben on 12th November 2009

If you need to log sql statements using hibernate you can turn debug on for the org.hibernate.sql logger.
That will log all sql statement, but the actual parameter values will not be logged.

To log the values bound to the hibernate prepared statements, you can turn on trace for the org.hibernate.type logger.

Unfortunately (apparently because of perfomance) the value of the org.hibernate.type logger is evaulated once in a static block, and cached, see: EnumType.java.

That prevents you from turning the log on and off during runtime using a simple jsp page like the log4jAdmin.jsp. Note that if it were possible you still had to add trace to the page as debug is currently the lowest value.

Of course there must be a reason why the hibernate implementation are caching the value of the org.hibernate.type logger, but I believe it is still valuable to be able to turn on sql logging with parameters at runtime.

For that purpose I came across the logDriver. A simple database driver written by Ryan Bloom, that is wrapping an existing jdbc driver.

To use the logdriver all you have to do is set the driver and connection url as follows:

driver:net.rkbloom.logdriver.LogDriver
url:jdbc:log_real_driver_class:real_jdbc_connection_url

Now you can turn on logging by setting the net.rkbloom.logdriver logger to debug.

Posted in how to, java | 1 Comment »