-->
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.  [ 7 posts ] 
Author Message
 Post subject: Retrieve generated SQL
PostPosted: Wed Apr 23, 2008 1:18 pm 
Regular
Regular

Joined: Wed Sep 29, 2004 11:34 am
Posts: 62
Location: Houston, TX
Hibernate version: 3.2.4

I am looking for the SQL that is generated similarily in hibernate.show_sql. We need the sql and the parameters because we want to forward it to another database. I am well aware of the replication option available in MySQL, but that causes another problem that I don't want to get into.

I have noticed that Interceptor will solve my problem, however I don't see the actual SQL. How do I find the actual SQL that Hibernate generates at the database?

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 2:21 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
The show_sql parameter only makes the generated SQL statements to be written to the console, which might not be what you want.

In order to show the SQL statements in a log file, use log4j, and simply add a category that grabs anything from the package org.hibernate.SQL, for example:

Code:
   <category name="org.hibernate.SQL" additivity="false">
      <priority value="debug" />
      <appender-ref ref="appenderConsole" />
      <appender-ref ref="appenderFileSQL" />
   </category>   


you could also create a separate file only for SQL statements, for example:

Code:
   <appender name="appenderFileSQL" class="org.apache.log4j.RollingFileAppender">
     <param name="File" value="@log.dir@/@log.filename@.sql"/>
     <param name="Append" value="true"/>
     <param name="MaxFileSize" value="20000KB"/>
     <param name="MaxBackupIndex" value="1"/>
     <layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern" value="%d %m%n"/>
     </layout>
   </appender>


SQL logging belongs to the DEBUG level, but you can log at different levels to different destinations. For example, the rest of your applicationmight log only at "INFO" level, but you can log the SQL code to a different file at DEBUG level.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 3:14 pm 
Regular
Regular

Joined: Wed Sep 29, 2004 11:34 am
Posts: 62
Location: Houston, TX
We need the information at the code level. We need to basically have some form of an injection which can do the following:

Depending on which table is being updated, added, etc. - We would take that SQL and push it off to another database. You see we would need to catch the update/add/delete after the transaction has committed, and only then capture the SQL and after that make the appropriate action.

Thanks anyways for your input.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 3:18 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Then you face an additional problem:
Unless you force ugly explicit, code transactions boundaries pretty much on every single command you write, and flush the session all the time, you cannot tell when Hibernate will decide to actually perform those SQL statements.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 3:28 pm 
Regular
Regular

Joined: Wed Sep 29, 2004 11:34 am
Posts: 62
Location: Houston, TX
Is there not a way for me to capture the statement when Hibernate decides to write to the log file. There has to be something that is executing the SQL command in the database and then if necessary to update the logs if logging is turned on. I was hoping to capture it once it has completed those steps. I am aware that Interceptors can help me capture the updates, adds, etc. However I believe, I would still have to create the SQL Query manually - Is there not a way to automate this or extract this information on demand?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 24, 2008 10:43 am 
Regular
Regular

Joined: Wed Sep 29, 2004 11:34 am
Posts: 62
Location: Houston, TX
Anyone?


Top
 Profile  
 
 Post subject: Debugging
PostPosted: Thu Apr 24, 2008 4:20 pm 
Beginner
Beginner

Joined: Tue Jun 26, 2007 2:31 pm
Posts: 21
Try Debugging the database there you can find the query hibernate generates


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.