Spring/Hibernate Improved SQL Logging with log4jdbc---reference

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 [email protected]: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,布布扣,bubuko.com

Spring/Hibernate Improved SQL Logging with
log4jdbc---reference

时间: 2024-11-23 11:06:48

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

Spring/Hibernate应用性能调优

对于大多数典型的Spring/Hibernate 企业应用来说,应用程序的性能几乎完全取决于它的持久层的性能. 这篇文章将会对如何确认在“数据库约束”的应用前,使用7种“快速见效”的技巧来帮助我们提升应用性能. 如何确认一个应用受到“数据库约束” 为了验证一个应用程序是否受到“数据库约束”,首先在一些开发环境中做一些普遍的行为,即使用VisualVM来监控. VisualVM是一个搭载JDK的Java解析器,它通过调用jvisualvm来进行命令行登陆. 登陆Visual VM后按照这样做: 运

Performance Tuning of Spring/Hibernate Applications---reference

http://java.dzone.com/articles/performance-tuning For most typical Spring/Hibernate enterprise applications, the application performance depends almost entirely on the performance of it's persistence layer. This post will go over how to confirm that

spring+hibernate常见异常集合

spring+hibernate出错小结: (1)java.lang.NoClassDefFoundError: org/hibernate/context/CurrentSessionContext 原因:出现这错误时,请更改hibernate的包,更新至最新或3.1以上 (2)java.lang.NoClassDefFoundError: javax/transaction/TransactionManager 原因:缺少jta.jar 或者是找不到hbm.xml文件导致sessionfac

Struts2+Spring+Hibernate 三大框架的合并集成

这次来看看Struts2+Spring+Hibernate三大框架的整合应用,主要是Spring和Hibernate框架的整合,因为前边已经将Strtus2+Spring整合过了基本一样.  首先看一下分工吧: Struts2做的MVC的流程框架,主要完成从客户端访问到选择action的过程,其中过滤器起到了Controller的作用,action属于model,而jsp则是view页面的展示. Spring主要利用Ioc的特长来管理各种对象:action,service,dao,数据访问源,H

使用spring+hibernate+atomikos+tomcat构建分布式事务

本文通过一个demo,介绍如何使用spring+hibernate+atomikos+tomcat构建在一个事务中涉及两个数据源的web应用. demo功能:实现一个能成功提交和回滚的涉及两个数据库数据源的XA事务. demo将实现: 1.一次性在两个数据库的两张表中各插入一条数据并提交. 2.一次性在两个数据库的两张表中各插入一条数据并回滚. 测试方式:restful web api 使用工具: spring 4.1.1.RELEASE hibernate 4.2.4.Final atomik

基于注解的struts2+spring+hibernate集成

applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.spri

Spring+hibernate 配置实例

转自:http://www.cnblogs.com/hongten/archive/2012/03/10/java_spring_hibernate.html 项目结构: http://www.cnblogs.com/hongten/gallery/image/112469.html 使用的jar包: hibernate核心安装包:hibernate3.jarlib\rquired\*.jarlib\optional\encache-1.2.3.jar    (二级缓存) lib\test\sl

spring+hibernate管理多个数据源(非分布式事务)

本文通过一个demo,介绍如何使用spring+hibernate管理多个数据源,注意,本文的事务管理并非之前博文介绍的分布式事务. 这个demo将使用两个事务管理器分别管理两个数据源.对于每一个独立的事务,只涉及一个数据源. demo功能:实现一个能依靠两个独立的事务管理器互不干涉的管理自己的数据源的web demo. demo将实现: 1.独立地控制两个不同的数据源的事务管理器. 测试方式:restful web api 使用工具: spring 4.1.1.RELEASE hibernat

Struts2,Spring,Hibernate框架的优缺点

Struts2,Spring,Hibernate框架的优缺点 Struts2框架(MVC框架)的优点如下: 1)  实现了MVC模式,层次结构清晰,使程序员只需关注业务逻辑的实现:        2)  丰富的标签库,大大提高了开发的效率:        3) Struts2提供丰富的拦截器实现        3) 通过配置文件,就可以掌握整个系统各个部分之间的关系:        4) 异常处理机制,只需在配置文件中配置异常的映射,即可对异常做相应的处理: Spring框架的优点如下: 1)