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
Hibernate: Log SQL Statements
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
使用automake的順序
iteye_20584
2020-02-21 18:47:04
ORA-01658: 無法爲表空間XXX中的段創建 INITIAL 區
iteye_20584
2020-02-21 18:47:03
閱讀文章 最簡單解決CHM文件無法顯示的辦法
iteye_20584
2018-08-27 19:01:30
程序員修煉之道:正交軟件架構方法
iteye_20584
2018-08-27 19:01:29
Windows XP 註冊表設置文件
iteye_20584
2018-08-27 19:01:29
程序設計語言正交特性的一點思考
iteye_20584
2018-08-27 19:01:29
Windows隱藏登錄界面用戶名
iteye_20584
2018-08-27 19:01:29
log4j:ERROR LogMananger.repositorySelector was null likely due to error in class
iteye_20584
2018-08-27 19:01:29
Windows添加移動到 右鍵菜單
iteye_20584
2018-08-27 19:01:28
"node to traverse cannot be null"
iteye_20584
2018-08-27 19:01:28
ICEFaces 值修改事件處理 ValueChangeEvent
iteye_20584
2018-08-27 19:01:28
漏洞掃描工具nikto使用心得
iteye_20584
2018-08-27 19:01:28
安裝GNOME中文桌面環境
iteye_20584
2018-08-27 19:01:28
Distributed Transaction Coordinator 服務因 3221229584 (0xC0001010) 服務性錯誤而停止
iteye_20584
2018-08-27 19:01:28
what is “Microsoft-WebDAV-MiniRedir/5.1.2600″ ?
iteye_20584
2018-08-27 19:01:28