-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Logging SQL Bind Variables
PostPosted: Fri Feb 23, 2007 4:02 pm 
Newbie

Joined: Fri Feb 23, 2007 3:39 pm
Posts: 2
Location: Green Bay, WI
Hibernate version: Hibernate 3

My company is working with Jboss AS, using Hibernate as our persistence engine. We would like to include all SQL and DML statements processed by hibernate in our logs. In addition, we would like the bind variables logged as well.

Setting the org.hibernate.SQL logger to DEBUG accomplishes the first task, logging all SQL/DML statements.

However, logging the bind variables does not seem to be as simple. Bear in mind we are doing this from javax persistance, so Im not quite sure what underlying hibernate functions are being called. A knowledge of javax persistence is needed to understand the following schenarios:
1 - Querying using EntityManager.find(entity, id)
2 - Querying using Query.getResultList() where Query is constructed from a NamedQuery
3 - Updating/Deleting using EntityManager.merge(entity) or EntityManager.remove(entity)
4 - Updating/Deleting using Query.executeUpdate() where Query is constructed from a NamedQuery

In case #2, logger 'org.hibernate.loader.hql.QueryLoader' records the bind variables. However, in cases 1, 3, and 4, hibernate logs the SQL/DML, but fails to log the bind variables.


Is there a way to get hibernate to log the bind variables for scenarios 1, 3, and 4? If I can't get hibernate to do it, is it possible for me to implement an interceptor and do it myself? I tried adding an inteceptor in the hibernate properties (hibernate.ejb.interceptor) that extends org.hibernate.EmptyInterceptor. The interceptor allows me to capture the SQL query (in the onPrepareStatement(String sql) function), but this does not give me access to the bind variables.

Any direction would be much appreciated. Thank you!
-Brett Birschbach


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 20, 2007 11:43 am 
Beginner
Beginner

Joined: Tue Sep 16, 2003 11:26 am
Posts: 25
Location: Berlin - Germany
To log JDBC parameters try to add this to JBoss log4j.xml:
Code:
<category name="org.hibernate.type">
   <priority value="TRACE"/>
</category>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 20, 2007 12:07 pm 
Newbie

Joined: Fri Feb 23, 2007 3:39 pm
Posts: 2
Location: Green Bay, WI
Heh, thanks for the reply n2. Forgot I had this post out here.

I came across this same tip elsewhere, so it does indeed work. However, one point to note is that the org.hibernate.type logger not only logs all bind variable (which is awesome), it also logs all return values. That is one log statement per column per row returned. The only way I know to get around the excessive return value logging, while keeping bind variable logging, is to implement a logger filter (which we have).

Thanks for the tip n2, perhaps it will help someone else looking to do the same thing.

-Brett


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.