<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Piraguablog &#187; sql</title>
	<atom:link href="http://www.piragua.com/category/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.piragua.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Tue, 22 Jun 2010 22:03:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Grails, p6spy and Sql Profiler</title>
		<link>http://www.piragua.com/2009/06/17/grails-p6spy-and-sql-profiler/</link>
		<comments>http://www.piragua.com/2009/06/17/grails-p6spy-and-sql-profiler/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 04:46:21 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[grails]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.piragua.com/?p=35</guid>
		<description><![CDATA[There are several ways to have Grails log SQL statements that Hibernate is sending to the database under the covers of GORM.  The first is the loggingSql flag in DataSource.groovy:

loggingSql=true

This will show you the prepared statement that Hibernate is generating, like this:

SELECT
        this_.id AS id6_0_,
    [...]]]></description>
			<content:encoded><![CDATA[<p>There are several ways to have Grails log SQL statements that Hibernate is sending to the database under the covers of GORM.  The first is the loggingSql flag in DataSource.groovy:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy">loggingSql<span style="color: #66cc66;">=</span><span style="color: #000000; font-weight: bold;">true</span></pre></div></div>

<p>This will show you the prepared statement that Hibernate is generating, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="sql"><span style="color: #993333; font-weight: bold;">SELECT</span>
        this_.id <span style="color: #993333; font-weight: bold;">AS</span> id6_0_,
        this_.version <span style="color: #993333; font-weight: bold;">AS</span> version6_0_,
        this_.authority <span style="color: #993333; font-weight: bold;">AS</span> authority6_0_,
        this_.description <span style="color: #993333; font-weight: bold;">AS</span> descript4_6_0_
    <span style="color: #993333; font-weight: bold;">FROM</span>
        role this_
    <span style="color: #993333; font-weight: bold;">WHERE</span>
        this_.authority<span style="color: #66cc66;">=</span>?</pre></div></div>

<p>This is a good first step &#8211; but what if I want to see the parameter that is being sent as well?  You could turn logging for the org.hibernate.type package to TRACE level:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #808080; font-style: italic;">//in Config.groovy</span>
log4j <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
    trace  <span style="color: #ff0000;">'org.hibernate.type'</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>YIKES!  That gets REALLY noisy, really fast:</p>

<div class="wp_syntax"><div class="code"><pre class="apache">Hibernate:
    select
        this_.id as id6_0_,
        this_.version as version6_0_,
        this_.authority as authority6_0_,
        this_.description as descript4_6_0_
    <span style="color: #00007f;">from</span>
        role this_
    where
        this_.authority=?
TRACE type.StringType  - binding <span style="color: #7f007f;">'ROLE_USER'</span> to parameter: <span style="color: #ff0000;">1</span>
TRACE type.LongType  - returning <span style="color: #7f007f;">'1'</span> as column: id6_0_
TRACE type.LongType  - returning <span style="color: #7f007f;">'1'</span> as column: version6_0_
TRACE type.StringType  - returning <span style="color: #7f007f;">'ROLE_USER'</span> as column: authority6_0_
TRACE type.StringType  - returning <span style="color: #7f007f;">'Default user role'</span> as column: descript4_6_0_</pre></div></div>

<p>Besides, what I really want is the ability to copy and paste a query, with the parameters bound, directly into a SQL editor to execute the query and see the results myself.  Enter the <a href="http://grails.org/plugin/p6spy" onclick="javascript:pageTracker._trackPageview('/outbound/article/grails.org');">p6spy plugin for Grails</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="text">grails install-plugin p6spy</pre></div></div>

<p>Swap the driver in your DataSource.groovy file from your driver to the p6spy driver (the plugin will automatically add this, you just need to comment it in):</p>

<div class="wp_syntax"><div class="code"><pre class="groovy">environments <span style="color: #66cc66;">&#123;</span>
  development <span style="color: #66cc66;">&#123;</span>
    dataSource <span style="color: #66cc66;">&#123;</span>
      <span style="color: #808080; font-style: italic;">//driverClassName = &quot;org.hsqldb.jdbcDriver&quot;</span>
      driverClassName <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;com.p6spy.engine.spy.P6SpyDriver&quot;</span>
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Fire up your Grails application &#8211; p6spy will create a spy.log file in your application directory and log a pipe delimited line &#8211; the last column has the actual SQL being executed, with the parameters!</p>

<div class="wp_syntax"><div class="code"><pre class="text">select this_.id as id2_0_, this_.version as version2_0_, this_.authority as authority2_0_, this_.description as descript4_2_0_ from role this_ where this_.authority='ROLE_USER'</pre></div></div>

<p>This is a great start, and most people stop here.  But sometimes there are so many queries being executed that it&#8217;s difficult to wade through spy.log to find the one you&#8217;re looking for.  Thanks to a tip from the excellent <a href="http://www.manning.com/gsmith/" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.manning.com');">Grails In Action</a> book that was recently released &#8211; SQL Profiler can help!</p>
<p>Download SQL Profiler (<a href="http://sourceforge.net/projects/sqlprofiler" onclick="javascript:pageTracker._trackPageview('/outbound/article/sourceforge.net');">http://sourceforge.net/projects/sqlprofiler</a>) and add these lines to grails-app/conf/spy.properties:</p>

<div class="wp_syntax"><div class="code"><pre class="text">log4j.appender.SQLPROFILER_CLIENT=org.apache.log4j.net.SocketAppender
log4j.appender.SQLPROFILER_CLIENT.RemoteHost=localhost
log4j.appender.SQLPROFILER_CLIENT.Port=4445
log4j.appender.SQLPROFILER_CLIENT.LocationInfo=true</pre></div></div>

<p>Add the appender to the logger definition:</p>

<div class="wp_syntax"><div class="code"><pre class="text">log4j.logger.p6spy=INFO,STDOUT,SQLPROFILER_CLIENT</pre></div></div>

<p>Finally, make sure the Log4j Appender is enabled:</p>

<div class="wp_syntax"><div class="code"><pre class="text">#specifies the appender to use for logging
appender=com.p6spy.engine.logging.appender.Log4jLogger
#appender=com.p6spy.engine.logging.appender.StdoutLogger
#appender=com.p6spy.engine.logging.appender.FileLogger</pre></div></div>

<p>Then, in the directory in which you downloaded the SQL Profiler jar, launch the GUI with this line:</p>

<div class="wp_syntax"><div class="code"><pre class="text">java -jar sqlprofiler.jar</pre></div></div>

<p>Now, p6spy will log SQL to the SQL Profiler GUI!  You can use it to profile SQL statements, but you can also use the &#8216;Logger&#8217; tab to filter the log.  I click the little trash can icon in the upper left to clear the log, run a test in the application, then filter the results using the filter fields available.  For instance, if I only want to see queries related to the &#8216;ROLE&#8217; table, just put the word &#8216;role&#8217; in the Filter message text box, and you&#8217;ll only see queries related to that table.  It&#8217;s a great way to get straight at a particular SQL query when you&#8217;re drowning in tons and tons of SQL messages.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.piragua.com/2009/06/17/grails-p6spy-and-sql-profiler/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
