Integerate P6spy with Liferay to format Hibernate SQL

Integerate P6spy with Liferay to format Hibernate SQL

When working on the customer's tickets, we may analysis the hibernate SQL to get some useful information, but it is really difficult to do that, the SQL file is disorganized, besides the most cursing problem is that there are so many “?” there, is there any way to make the hibernate SQL file be much more readable and legible ?

Introduction

P6Spy is a framework that enables database activity to be seamlessly intercepted and logged with no code changes to existing applications. It imitates a standard JDBC driver, for short, if adding the real driver to the p6spy configure file. then we can monitor the SQL processing steps and analyze the results.

IronTrack SQL is a software based on p6spy,but it is much more convenient to obserive the results.

Configration and Integeration

  • P6spy
  1. Download from http://sourceforge.net/projects/p6spy , “p6spy-install.jar”

  2. Extract a clean Liferay Portal bundles,like “liferay-portal-tomcat-6.2-ee-sp7.zip”.

  3. Extract “p6spy-install.jar”.

    copy “p6spy.jar” to “{server/tomcat}/lib/ext/”
    copy “spy.properties” to “{server/tomcat}//webapps/ROOT/WEB-INF/classes/”

  4. Edit “spy.properties”

    set “logfile =” to you own log path:{$path$}/spy.log.
    set “realdriver =” to “realdriver=com.mysql.jdbc.Driver”
    set “deregisterdrivers =” to “deregisterdrivers = true”

  5. Edit “portal-ext.properites”.

    set “jdbc.default.driverClassName =” value to ”com.p6spy.engine.spy.P6SpyDriver”

  6. Edit “log4j.properties”.

    add “log4j.logger.p6spy=INFO,STDOUT”

  7. Start up your portal server,the log has been written in the “spy.log”

 

  • IronTrack SQL

  1. Download from http://www.ibm.com/developerworks/cn/java/j-lo-p6spy/IronTrackSQL.zip , “IronTrackSQL.zip”

  2. Extract a clean Liferay Portal bundles,like “liferay-portal-tomcat-6.2-ee-sp7.zip”.

  3. Extract “IronTrackSQL.zip”.

    copy “p6spy.jar, log4j-1.2.8.jar , irontracksql.jar” to “{server/tomcat}/lib/ext/”
    copy “spy.properties” to “{server/tomcat}/webapps/ROOT/WEB-INF/classes/”

  4. Edit “spy.properties”

    set “logfile =” to you own log path:{$path$}/spy.log.
    set “realdriver =” to “realdriver=com.mysql.jdbc.Driver”
    set “deregisterdrivers =” to “deregisterdrivers = true”

  5. Edit “portal-ext.properites”.

    set “jdbc.default.driverClassName =” value to ”com.p6spy.engine.spy.P6SpyDriver”

  6. Edit “log4j.properties”.

    add “log4j.logger.p6spy=INFO,STDOUT”

  7. Run command “java -jar irontracksql.jar” to start “IronTrack SQL”

  8. Start up your portal server,the log has been written in the “spy.log” and logs have been show in “IronTrack SQL”.

Blogs
Nice post! Thanks for sharing.

It seems that page you've mentioned (http://www.irongrid.com) doesn't exists anymore and serves only ads right now emoticon

The tool itself - 'IronTrackSQL' - looks like very old (2003?) profiler and unsupported since very long time but you can still get the src/bin of whle 'Iron' suite from 3rd party pages for example: http://www.cascadetg.com/hibernate/

Is there any particular reason You have chosen 'IronTrackSQL' to profile SQL in Liferay? (I have very little experience with this kind of tools)
Hi Konrad,

Thanks for figuring out the wrong link.

there are no special reasons for choosing "IronTrackSQL", but as you know there is another tool "SQL Profiler", it includes some code of "log4j" which may result in
"Multiple loading problem".

David.
Really cool David!

Useful and well-written! emoticon Thank you!
Glad I'm not the only one who has this problem. I've used log4jdbcspy before, just take warning you may not want to use it in production.
I have already configured portal-ext.properties as below :
jdbc.default.driverClassName=com.mysql.jdbc.Driver
jdbc.default.url=jdbc:mysql://localhost/demoschema?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.default.username=root
jdbc.default.password=root

Then how to configure it with
jdbc.default.driverClassName =” value to ”com.p6spy.engine.spy.P6SpyDriver

This?
I have already configured portal-ext.properties as below :
jdbc.default.driverClassName=com.mysql.jdbc.Driver
jdbc.default.url=jdbc:mysql://localhost/demoschema?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.default.username=root
jdbc.default.password=root

Then how to configure it with
jdbc.default.driverClassName =” value to ”com.p6spy.engine.spy.P6SpyDriver

This?
Hi Abhishek,

right, one more thing, without "",

jdbc.default.driverClassName=com.p6spy.engine.spy.P6SpyDriver.

David.
Then it will replace the previous property.

You are asking to do below thing right?

In my portal-ext.properties:
jdbc.default.driverClassName=com.mysql.jdbc.Driver
jdbc.default.url=jdbc:mysql://localhost/demoschema?useUnicode=true&characterEnco­ding=UTF-8&useFastDateParsing=false
jdbc.default.username=root
jdbc.default.password=root

jdbc.default.driverClassName =com.p6spy.engine.spy.P6SpyDriver

This will not work!!
Hi Abhishek,

replace "com.mysql.jdbc.Driver" to "com.p6spy.engine.spy.P6SpyDriver", we just change the driver,could you set up the environment the step by step ?

jdbc.default.driverClassName =com.p6spy.engine.spy.P6SpyDriver
jdbc.def­ault.url=jdbc:mysql://localhost/demoschema?useUnicode=true&characterEnco­ding=UT­F-8&useFastDateParsing=false
jdbc.default.username=root
jdbc.default.password=root

let me know if you have other question.
David.
does this work on Liferay-6.2-.CE-GA2? unable to make it work for me even after following all the steps that are listed here. The file spy.log is created. But there are no contents being filled into it.
Thanks David!

Just one trick, if you want to print SQL log into your server's log file, set the logfile property to blank into the spy.properties file.

Cheers!
The realdriver and deregisterdrivers properties are no longer supported as of version 2.1.4 of p6spy at least. The realdriver property has been replaced by driverlist while deregisterdrivers appears to have been removed altogether.