- 浏览: 43733 次
- 性别:
- 来自: 上海
-
最新评论
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
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
发表评论
-
阅读文章 最简单解决CHM文件无法显示的办法
2009-12-03 10:07 1022在CHM文件右键——属性——解除锁定!万事大吉! -
log4j:ERROR LogMananger.repositorySelector was null likely due to error in class
2009-11-28 13:13 4769Log4j 1.2.15存在一个bug -
程序员修炼之道:正交软件架构方法
2009-11-28 13:09 1119程序员修炼之道:正交软 ... -
程序设计语言正交特性的一点思考
2009-11-28 13:04 1044程序设计语言正交特性 ... -
ICEFaces 值修改事件处理 ValueChangeEvent
2009-11-28 13:01 1097public void genreOthersChang ... -
"node to traverse cannot be null"
2009-11-28 13:00 3444java hibernate 中"node to t ... -
Debugging Tomcat Using JDB
2009-11-28 12:59 995Debugging Tomcat Using JDB Subm ... -
what is “Microsoft-WebDAV-MiniRedir/5.1.2600″ ?
2009-11-28 12:58 247210 1月 2009 So, what is “Microso ... -
漏洞扫描工具nikto使用心得
2009-11-28 12:57 24382009-03-18 12:33:14 www.hackba ... -
Windows添加移动到 右键菜单
2009-11-28 12:56 981Windows Registry Editor Version ... -
Windows隐藏登录界面用户名
2009-11-28 12:55 1016Windows Registry Editor Version ... -
Windows XP 注册表设置文件
2009-11-28 12:54 1759Windows XP 注册表设置文件 作者:admin 日期: ... -
Distributed Transaction Coordinator 服务因 3221229584 (0xC0001010) 服务性错误而停止
2009-11-28 12:53 2191Distributed Transaction Coordin ... -
安装GNOME中文桌面环境
2009-11-28 12:52 14448.3. 安装GNOME中文桌面环境 Prev Chapter ... -
Oracle命令行修改表空间大小
2009-11-28 12:50 2539alter tablespace system resize ... -
Oracle批量删除对象
2009-11-28 12:50 1567DECLARE TYPE name_list IS TABLE ... -
免安装Oracle运行pl/sql developer
2009-11-28 12:47 939Keiboc发布于 2008-5-08 | 1062次阅读 ... -
undefined reference to `_strcasestr'
2009-11-28 12:42 1577undefined reference to `_strcas ... -
使用automake的顺序
2009-11-28 12:41 1006acloacl, autohead, automake --a ... -
ORA-01658: 无法为表空间XXX中的段创建 INITIAL 区
2009-11-28 12:40 2426表空间耗尽
相关推荐
标题 "HANA SQLStatements ALL" 指的是 SAP HANA 数据库系统中的 SQL 语句大全,这通常涵盖了用于各种报告的 SQL 查询和操作。在 SAP HANA 中,SQL(结构化查询语言)是用于管理和处理数据库的标准语言,它允许用户...
标题“SQLStatements.zip”暗示了这个压缩包包含与SQL语句相关的资料,特别是针对SAP HANA数据库管理系统。描述提到“HANA script for Admin”,这表明这些内容是为HANA数据库管理员准备的,可能涉及管理和优化...
pg_log_statements pg_log_statements是一个PostgreSQL扩展,它允许记录特定数据库会话SQL语句:可以为特定服务器进程设置log_statement ,而不是在实例级别或数据库级别设置log_statement参数。安装编译中可以使用...
财务报表是广泛的商业分析的基础。管理者用它们来监督和判断 他们的公司相对于竞争对 手的表现,与外部投资者交流,帮助判断 他们应该执行的金融政策,并评估潜在的新业务 ,作为投资的一部分 策略证券分析师利用...
You don't even need to know the database is there, and you can change from one database to another simply by changing a few statements in a configuration file. If you've needed to add a database ...
在数据库应用开发中,SQL语句是核心执行逻辑的实现方式,无论是通过解释器直接执行还是通过API在后台提交。为了提高应用性能,尤其是MySQL数据库应用,优化SQL语句显得至关重要。本部分将深入探讨如何优化各种SQL...
从给定的文件信息中,我们可以提取到一系列与MySQL数据库管理相关的知识点,这些知识点主要集中在数据库的创建、用户管理、权限授予以及数据表的构建上。以下是对这些知识点的详细阐述: ### 数据库用户管理 ...
下列语句部分是MsSql语句,不可以在access...SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT) DCL—数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK)
Log4jdbc 的一个重要特性是它能自动合并 Prepared Statements 中的绑定参数,这极大地提高了 SQL 语句的可读性和调试效率。此外,它还能提供 SQL 执行时间,帮助识别性能瓶颈,生成慢 SQL 报表。对于数据库连接管理...
### Hibernate + c3p0 连接池与 SQL Server 2000 的配置与问题解决 #### 一、背景介绍 在Java开发环境中,Hibernate作为一款流行的ORM框架,能够提供一套强大的对象-关系映射机制,使得开发者可以更加便捷地进行...
If you (or others you know) are versed in SQL statements and newer to the Elasticsearch query syntax but want to benefit from the power Elasticsearch, this is the talk for you. After making its debut ...
This build of SQL Prompt includes: Improved SQL formatting The formatting feature is now more customisable More options for: CASE (UserVoice) JOIN (UserVoice) CTEs (UserVoice) CREATE statements IN ...
This build of SQL Prompt includes: Improved SQL formatting The formatting feature is now more customisable More options for: CASE (UserVoice) JOIN (UserVoice) CTEs (UserVoice) CREATE statements IN ...
- `hibernate.c3p0.max_statements`:最大预编译SQL语句数。 3. **数据源属性**: - `hibernate.connection.datasource`:数据源的JNDI名字,适用于应用服务器中的数据源。 - `hibernate.jndi.url`:JNDI提供者...
- **`hibernate.c3p0.max_statements`**:定义每个连接的最大语句数。 #### 二、数据源配置(DataSource) 当应用环境中已经存在数据源时,可以通过以下配置项将Hibernate与之连接起来: 1. **`hibernate....
This book expresses the author’s views and opinions. The information contained in this book is provided without any express, statutory, or implied warranties. Neither the authors, Microsoft ...
DB Query Analyzer是一款由马根峰开发的数据库查询分析工具,专为实现通过SQL语句访问文本文件的梦想而设计。这款工具具有强大的功能,能够高效地处理大量数据,比如在大约59秒内处理包含2,500,000条记录的文件。其...
string ls_sql, ls_line, la_sql_statements[] long ll_file ll_file = FileOpen("path_to_sql_script.sql", fmOpenRead) while !FileEOF(ll_file) ls_line = FileReadLine(ll_file) if len(ls_line) > 0 then...
It can even complete INSERT, ALTER and JOIN statements. To cut down on repetitive typing, SQL Prompt has an extensive, customizable set of snippets. To avoid complications when editing your SQL, ...
"Executing SQL statements (2).zip" 文件很可能包含一系列关于如何执行SQL语句的教程或示例,这对于我们理解和操作数据库至关重要。以下是对SQL执行核心概念的详细解释: 1. **SQL基本结构**:SQL语句通常由关键字...