Hibernate: Log SQL Statements
At 11:06 PM on Aug 21, 2005, R.J. Lorimer wrote:
Fresh Jobs for Developers Post a job opportunity
» Marix Servicing seeks a Sr. Developer--C#, MS SQL, .NET 3.5, ASP.NET, JavaScript, Silverlight, WCF in Phoenix, Az
Hibernate, in the end, is all about dispatching SQL statements. SQL is at the heart of communication with an RDBMS system, and it is extremely important when struggling through performance problems or other bugs, that you know what is going on. After all, knowing the executed SQL allows you to determine the number of queries to complete an O/R mapping task, not to mention that seeing SQL queries is critical to understanding how to optimize the queries via indices and other database settings.
There are two ways (due to the legacy of Hibernate) to enable SQL logging. The first is to simply enable SQL logging in the Hibernate configuration properties by setting the hibernate.show_sql property to true:
SessionFactory sf = new Configuration().setProperty("hibernate.show_sql", "true")// ....buildSessionFactory();
This is a nice quick and dirty solution, but it is relatively inflexible. SQL statements are always logged to System.out when that property is enabled, and on some servers, System.out isn't accessible by the average developer; or is cluttered with a million other log statements.
Alternatively, Hibernate uses the Apache-Jakarta Commons Logging package, which means that it will utilize log4j ,java.util.logging , or potentially another logging framework. Typically, log messages are sent to commons logging, and then they are dispatched to one of these logging frameworks. Then, those logging frameworks determine where the message should be sent (log files, sockets, emails, database records, system out, system err, etc). It is easy to see how using these log frameworks will increase the flexibility of log statements. With Hibernate, simply setting the org.hibernate.SQL logger to 'DEBUG' or 'ALL' will ensure that all SQL statements are logged to the console. Here is an example of the log4j configuration for this:
log4j.logger.org.hibernate.SQL=DEBUG, SQL_APPENDERlog4j.additivity.org.hibernate.SQL=false
Note that in this case I have turned 'additivity' off for the org.hibernate.SQL logger. I like using this setting because it ensures that log messages aren't bubbled up to parent handlers - it is, of course, optional depending on your logging preference.
Until next time,
R.J. Lorimer
Contributing Editor - rj -at- javalobby.org
Author - http://www.coffee-bytes.com
Software Consultant - http://www.crosslogic.com
9 replies so far ( Post your own)
1 . At 11:27 AM on Nov 3, 2005, jedfonner wrote:
Reply
Re: Hibernate: Log SQL Statements
Is it possible to get Hibernate to output the unparameterized SQL?
By default, it seems to output SQL like "where assessment0_.id=?". I'd like to be able to see what value it is passing for ?.
2 . At 1:24 PM on Nov 3, 2005, R.J. Lorimer wrote:
Reply
Re: Hibernate: Log SQL Statements
Hibernate uses prepared statements internally, so it doesn't ever have the SQL in a format where they values would be embedded. That means that to produce SQL statements with the values embedded, it would have to generate them just for logging.
While it may exist, I'm fairly certain that form of logging is not available in Hibernate directly.
Best, R.J. Lorimer
3 . At 10:18 AM on Aug 1, 2006, Richard Hurrell wrote:
Reply
Re: Hibernate: Log SQL Statements
Set up a log4j category for org.hibernate.type. Get it to write out to the same log file as the org.hibernate.SQL
The type one will list the parameters for you after the SQL e.g.
2006-07-28 09:57:12,061 DEBUG org.hibernate.SQL - insert into BASKET_LINE_ALLOC (LAST_UPDATED, QUANTITY, CUSTOMER_REF, NOTES, BRANCH_ID, FUND_ID, TEMPLATE_ID,
BASKET_LINE_ALLOC_ID) values (?, ?, ?, ?, ?, ?, ?, ?)
2006-07-28 09:57:12,081 DEBUG org.hibernate.type.TimestampType - binding '2006-07-28 09:57:12' to parameter: 1
2006-07-28 09:57:12,081 DEBUG org.hibernate.type.IntegerType - binding '3' to parameter: 2
2006-07-28 09:57:12,082 DEBUG org.hibernate.type.StringType - binding '' to parameter: 3
2006-07-28 09:57:12,082 DEBUG org.hibernate.type.StringType - binding '' to parameter: 4
2006-07-28 09:57:12,082 DEBUG org.hibernate.type.LongType - binding '511' to parameter: 5
2006-07-28 09:57:12,082 DEBUG org.hibernate.type.LongType - binding '512' to parameter: 6
2006-07-28 09:57:12,082 DEBUG org.hibernate.type.LongType - binding null to parameter: 7
2006-07-28 09:57:12,082 DEBUG org.hibernate.type.LongType - binding '180030' to parameter: 8
4 . At 12:04 PM on Feb 14, 2007, Davide Baroncelli wrote:
Reply
Re: Hibernate: Log SQL Statements
> Set up a log4j category for org.hibernate.type. Get
> it to write out to the same log file as the
> org.hibernate.SQL
> The type one will list the parameters for you after
> the SQL e.g.
> 2006-07-28 09:57:12,082 DEBUG
> org.hibernate.type.LongType - binding '180030' to
> parameter: 8
Note: Hibernate actually uses commons logging "trace" level, so with latest log4j versions (supporting "trace" as well) this level must be enabled. Older versions treated trace just as debug, so enabling debug was enough.
5 . At 6:53 AM on Mar 19, 2007, Israr Ahmed wrote:
Reply
Re: Hibernate: Log SQL Statements
So how can we get parameterized values in Sql Statement while SQL statements are enabled in Configuration file.
There certainly have been performance issues with Java. We've been working really hard on them. The primary way we've attacked the problem is with advanced virtual machines. The performance has been getting very nice. --James Gosling, 1999.
6 . At 11:19 PM on Mar 21, 2007, !vS_ wrote:
Reply
Re: Hibernate: Log SQL Statements
This is a good trick
Planet Java
7 . At 1:39 AM on Oct 28, 2007, Tee Bishopric wrote:
Reply
Re: Hibernate: Log SQL Statements
I just want to say that your posts are consistently among the most useful things I find when I Google random Hibernate issues. Thanks!
8 . At 11:05 AM on Nov 29, 2007, Victor Ionescu wrote:
Reply
Re: Hibernate: Log SQL Statements
Use p6spy jdbc driver. Works great.
Registrul de casa - www.cdigroup.ro
9 . At 7:08 AM on Jan 29, 2008, Thread wrote:
Reply
Re: Hibernate: Log SQL Statements
Good tip, can be useful
Java