Announcing WhenWorksForYou.com

July 5, 2009 – 9:06 pm

WhenWorksForYou.com allows you to ask your friends exactly that question: “When works for you?” You can easily create an event and enter a few dates that work for you. Then invite your friends to visit the site and let you know which of those options work best for them. Once you have feedback from your potential guests, you can make a decision on the best date and time to schedule the actual event.

Check it out and give it a try!

Grails, p6spy and Sql Profiler

June 17, 2009 – 10:46 pm

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_,
        this_.version AS version6_0_,
        this_.authority AS authority6_0_,
        this_.description AS descript4_6_0_
    FROM
        role this_
    WHERE
        this_.authority=?

This is a good first step – 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:

//in Config.groovy
log4j = {
    trace  'org.hibernate.type'
}

YIKES! That gets REALLY noisy, really fast:

Hibernate:
    select
        this_.id as id6_0_,
        this_.version as version6_0_,
        this_.authority as authority6_0_,
        this_.description as descript4_6_0_
    from
        role this_
    where
        this_.authority=?
TRACE type.StringType  - binding 'ROLE_USER' to parameter: 1
TRACE type.LongType  - returning '1' as column: id6_0_
TRACE type.LongType  - returning '1' as column: version6_0_
TRACE type.StringType  - returning 'ROLE_USER' as column: authority6_0_
TRACE type.StringType  - returning 'Default user role' as column: descript4_6_0_

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 p6spy plugin for Grails:

grails install-plugin p6spy

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):

environments {
  development {
    dataSource {
      //driverClassName = "org.hsqldb.jdbcDriver"
      driverClassName = "com.p6spy.engine.spy.P6SpyDriver"
    }
  }
}

Fire up your Grails application – p6spy will create a spy.log file in your application directory and log a pipe delimited line – the last column has the actual SQL being executed, with the parameters!

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'

This is a great start, and most people stop here. But sometimes there are so many queries being executed that it’s difficult to wade through spy.log to find the one you’re looking for. Thanks to a tip from the excellent Grails In Action book that was recently released – SQL Profiler can help!

Download SQL Profiler (http://sourceforge.net/projects/sqlprofiler) and add these lines to grails-app/conf/spy.properties:

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

Add the appender to the logger definition:

log4j.logger.p6spy=INFO,STDOUT,SQLPROFILER_CLIENT

Finally, make sure the Log4j Appender is enabled:

#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

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

java -jar sqlprofiler.jar

Now, p6spy will log SQL to the SQL Profiler GUI! You can use it to profile SQL statements, but you can also use the ‘Logger’ 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 ‘ROLE’ table, just put the word ‘role’ in the Filter message text box, and you’ll only see queries related to that table. It’s a great way to get straight at a particular SQL query when you’re drowning in tons and tons of SQL messages.

Grails Code Coverage Plugin 1.1.4 Released!

March 5, 2009 – 9:54 pm

The Grails Code Coverage plugin has been upgraded to work with Grails 1.1 RC2.  Please give it a shot and create JIRA issues (http://jira.codehaus.org/browse/GRAILSPLUGINS/component/13147) if you encounter any problems.

This release of the plugin removes the test-app-cobertura script and utilizes changes in Grails 1.1 RC2 to hook into the test-app script instead.  In order to run coverage, simply install the plugin (grails install-plugin code-coverage) and run “grails test-app”.  There are several other changes in this release as well:
  • Upgraded Cobertura to 1.9
  • Ability to override the default exclusions list
  • Ability to bypass coverage instrumentation and report generation with the -nocoverage flag (e.g. grails test-app -nocoverage)
Full docs available on the Grails.org wiki:  http://www.grails.org/Test+Code+Coverage+Plugin

Learn to create your own Grails plugin with GroovyMag

February 2, 2009 – 9:57 am

The February issue of GroovyMag is out and has some great articles – including one on how to create a plugin for Grails written by yours truly. You can download the issue from the GroovyMag website. Enjoy!

Grails Code-Coverage plugin updated to run with Grails 1.1

January 12, 2009 – 10:01 pm

I’ve made some changes to the Grails code-coverage plugin to support Grails 1.1. If you’re using Grails 1.1-beta2, you can try it out by running grails install-plugin code-coverage.

Documentation can be found at http://www.grails.org/Test+Code+Coverage+Plugin