Hibernate provides SQL logging out of the box, but such logging only shows prepared statements, and not the actual SQL queries sent to the database.

It also does not log the execution time of each query, which is useful for performance troubleshooting. This blog post will go over how to setup Hibernate query logging, and then compare it to the logging that can be obtained with log4jdbc .

The Hibernate query logging functionality

Hibernate does not log the real SQL queries sent to the database. This is because Hibernate interacts with the database via the JDBC driver, to which it sends prepared statements but not the actual queries.

So Hibernate can only log the prepared statements and the values of their binding parameters, but not the actual SQL queries themselves.

This is how a query looks like when logged by Hibernate:

1.select /* load your.package.Employee */ this_.code, ...
2.from employee this_
3.where this_.employee_id=?
4. 
5.TRACE 12-04-2014@16:06:02  BasicBinder - binding parameter [1] as [NUMBER] - 1000

See this post Hibernate Debugging - Finding the origin of a Query  for how to setup this type of logging.

Using log4jdbc

For a developer it's useful to be able to copy paste a query from the log and be able to execute the query directly in an SQL client, but the variable placeholders ? make that unfeasible.

Log4jdbc in an open source tool that allows to do just that, and more. Log4jdbc is a spy driver that will wrap itself around the real JDBC driver, logging queries as they go through it.

The version linked from this post provides Spring integration, unlike several other log4jdbc forks.

Setting up log4jdbc

First include the log4jdbc-remix library in your pom.xml. This library is a fork of the original log4jdbc:

1.<dependency>
2.<groupId>org.lazyluke</groupId>
3.<artifactId>log4jdbc-remix</artifactId
4.<version>0.2.7</version>
5.</dependency>

Next, find in the Spring configuration the definition of the data source. As an example, when using the JNDI lookup element this is how the data source looks like:

1.<jee:jndi-lookup id="dataSource"
2.jndi-name="java:comp/env/jdbc/some-db" />

After finding the data source definition, rename it to the following name:

1.<jee:jndi-lookup id="originalDataSource"
2.jndi-name="java:comp/env/jdbc/some-db" />

Then define a new log4jdbc data source that wraps the real data source, and give it the original name:

01.<bean id="dataSource" class="net.sf.log4jdbc.Log4jdbcProxyDataSource">        
02.<constructor-arg ref="originalDataSource" />        
03.<property name="logFormatter">                   
04.<bean class="net.sf.log4jdbc.tools.Log4JdbcCustomFormatter" >
05.<property name="loggingType" value="SINGLE_LINE" />
06.<property name="margin" value="19" />  
07.<property name="sqlPrefix" value="SQL:::" />           
08.</bean>           
09.</property>    
10.</bean>

With this configuration, the query logging should already be working. It's possible to customize the logging level of the several log4jdbc loggers available.

The original log4jdbc documentation provides more information on the available loggers:

  • jdbc.sqlonly: Logs only SQL
  • jdbc.sqltiming: Logs the SQL, post-execution, including timing execution statistics
  • jdbc.audit: Logs ALL JDBC calls except for ResultSets
  • jdbc.resultset: all calls to ResultSet objects are logged
  • jdbc.connection: Logs connection open and close events

The jdbc.audit logger is especially useful to validate the scope of transactions, as it logs the begin/commit/rollback events of a database transaction.

This is the proposed log4j configuration that will print only the SQL queries together with their execution time:

01.<logger name="jdbc.sqltiming" additivity ="false">            
02.<level value="info" />               
03.</logger
04.<logger name="jdbc.resultset" additivity ="false">             
05.<level value="error" />       
06.</logger
07.<logger name="jdbc.audit" additivity ="false">
08.<level value="error" />       
09.</logger>  
10.<logger name="jdbc.sqlonly" additivity ="false">             
11.<level value="error" />       
12.</logger>  
13.<logger name="jdbc.resultsettable" additivity ="false">          
14.<level value="error" />      
15.</logger>          
16.<logger name="jdbc.connection" additivity ="false">             
17.<level value="error" />       
18.</logger
19.<logger name="jdbc.resultsettable" additivity ="false">           
20.<level value="error" />       
21.</logger>

Conclusion

Using log4jdbc does imply some initial setup, but once it's in place it's really convenient to have. Having a true query log is also useful for performance troubleshooting (to be described in a future post).

reference from:http://java.dzone.com/articles/springhibernate-improved-sql

Spring/Hibernate Improved SQL Logging with log4jdbc---reference的更多相关文章

  1. Performance Tuning of Spring/Hibernate Applications---reference

    http://java.dzone.com/articles/performance-tuning For most typical Spring/Hibernate enterprise appli ...

  2. Spring/Hibernate 应用性能优化的7种方法

    对于大多数典型的 Spring/Hibernate 企业应用而言,其性能表现几乎完全依赖于持久层的性能.此篇文章中将介绍如何确认应用是否受数据库约束,同时介绍七种常用的提高应用性能的速成法.本文系 O ...

  3. Spring / Hibernate 应用性能调优

    来源:ImportNew - 陈晓舜 对大部分典型的Spring/Hibernate企业应用来说,应用的性能大部分由持久层的性能决定. 这篇文章会重温一下怎么去确认我们的应用是否是”数据库依赖(dat ...

  4. Servlet开发 中使用 log4jdbc 记录 hibernate 的 SQL信息

    一.前言 使用log4jdbc在不改变原有代码的情况下,就可以收集执行的SQL文和JDBC执行情况. 平时开发使用的ibatis,hibernate,spring jdbc的sql日志信息,有一点个缺 ...

  5. Spring MVC第一课:用IDEA构建一个基于Spring MVC, Hibernate, My SQL的Maven项目

    作为一个Spring MVC新手最基本的功夫就是学会如何使用开发工具创建一个完整的Spring MVC项目,本文站在一个新手的角度讲述如何一步一步创建一个基于Spring MVC, Hibernate ...

  6. spring+hibernate常见异常集合

    spring+hibernate出错小结: (1)java.lang.NoClassDefFoundError: org/hibernate/context/CurrentSessionContext ...

  7. 软件架构期末复习(Struts2+Spring+Hibernate)

    Struts2+Spring+Hibernate The Model-ViewController pattern in Struts2 is implemented with the followi ...

  8. Spring+Hibernate+Struts(SSH)框架整合

    SSH框架整合 前言:有人说,现在还是流行主流框架,SSM都出来很久了,更不要说SSH.我不以为然.现在许多公司所用的老项目还是ssh,如果改成流行框架,需要成本.比如金融IT这一块,数据库dao层还 ...

  9. SSH面试题(struts2+Spring+hibernate)

    struts2 + Spring +hibernate Hibernate工作原理及为什么要用?   原理:   1.读取并解析配置文件   2.读取并解析映射信息,创建SessionFactory ...

随机推荐

  1. jQuery响应式幻灯片插件jquery.glide.js(支持触摸&轻量级)

    找到一款好的幻灯片插件不容易,找到一款功能全并且使用很简单的幻灯片更不容易,今天为大家分享一款全能的幻灯片插件glide.js,也是我现在在使用的一款插件. jquery.glide.js是响应和触摸 ...

  2. BZOJ 1574: [Usaco2009 Jan]地震损坏Damage

    Description 农夫John的农场遭受了一场地震.有一些牛棚遭到了损坏,但幸运地,所有牛棚间的路经都还能使用. FJ的农场有P(1 <= P <= 30,000)个牛棚,编号1.. ...

  3. real-time application

    http://www.hanselman.com/blog/InstallingAndRunningNodejsApplicationsWithinIISOnWindowsAreYouMad.aspx ...

  4. Z-stack之OSAL初始化流程

    转自点击打开链接 我使用的协议栈版本及例子信息: ZigBee2006\Texas Instruments\ZStack-1.4.3-1.2.1\Projects\zstack\Samples\Sam ...

  5. 【 HDU 1255】 覆盖的面积(矩阵面积交,线段树,扫描法)

    [题目] 覆盖的面积 Problem Description 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input 输入数据的第一行是一个正整数T(1<=T<=100 ...

  6. android Mediaplayer硬件解码浅探

    在讨论stagefright如何调用硬件解码之前,我们要先清楚几个问题. 我不展开这几个结论是如何得来的,因为这部分属于进程间通信binder的理解,和多媒体本身无关. 一.问题空间 这个有点像方法学 ...

  7. 【Xamarin挖墙脚系列:最重要的布局ListView】

    原文:[Xamarin挖墙脚系列:最重要的布局ListView] 安卓的几个重要的布局 线性布局 相对布局  Table布局 Tab布局  表格Grid布局 列表布局. 这几种基本的布局的方式,最重要 ...

  8. Node.js权威指南 (12) - Node.js中的其他模块

    12.1 使用dns模块解析域名 / 313 12.1.1 使用resolve方法将域名解析为DNS记录 / 313 12.1.2 使用lookup方法查询IP地址 / 315 12.1.3 使用re ...

  9. Muduo-Base-Exception(未完待续)

    Exception类 主要功能: 能够对异常信息进行输出 能够输出调用的关系 方便我们对某些信息进行调试 系统调用:#include <execinfo.h>int backtrace(v ...

  10. android学习——activity的生命周期

    Android中主要组件之Activity的生命周期,基本都是翻译Android API和个人的理解. 首先看一下Android api中所提供的Activity生命周期图: Activity其实是继 ...