Home > Database, Java > grails-p6spy-0.2

grails-p6spy-0.2

by paul on September 19, 2007

This information is out of date – please see http://grails.org/plugin/p6spy for the current documentation.

Grails 0.6 was recently released, and this new version included changes to the way the DataSource is defined. So, to make my grails-p6spy-plugin compatible with 0.6, here is a new release:

grails-p6spy-0.2.zip

P6Spy lets you monitor the JDBC queries by proxying your database driver. In addition to logging the prepared statements, it also logs the sql with parameters in place so you can copy and paste the exact sql into your favourite database client to test the results.

This Grails plugin makes it easy to utilize P6Spy in your Grails applications.

Introduction

This plugin contains 2 files – the p6spy jar and spy.properties. After installing the plugin in your Grails application, you can find them in:

  • <project directory>/plugins/p6spy-0.2/grails-app/conf/spy.properties
  • <project directory>/plugins/p6spy-0.2/lib/p6spy-1.3.jar

The install script updates your DataSource.groovy to add a commented line containing the p6spy driver class:

  • // driverClassName = “com.p6spy.engine.spy.P6SpyDriver”

Example use

To give the plugin a quick run through, you can either run the following shell script, or work through the step-by-step instructions below.

Shell script:

wget http://javathinking.com/grails/grails-p6spy-plugin/0.2/grails-p6spy-0.2.zip
wget http://javathinking.com/grails/grails-p6spy-plugin/0.2/test/regression/Book
wget http://javathinking.com/grails/grails-p6spy-plugin/0.2/test/regression/BookTests
wget http://javathinking.com/grails/grails-p6spy-plugin/0.2/test/regression/DataSource

grails create-app spytest
cd spytest
grails install-plugin ../grails-p6spy-0.2.zip
grails create-domain-class book
cp ../Book grails-app/domain/Book.groovy
cp ../BookTests test/integration/BookTests.groovy
cp ../DataSource grails-app/conf/DataSource.groovy
grails test-app
tail spy.log

Step-by-Step

Download the plugin from

http://javathinking.com/grails/grails-p6spy-plugin/0.2/grails-p6spy-0.2.zip

Create a new Grails application, add a domain class, and install the plugin:

grails create-app spytest
cd spytest
grails install-plugin ../grails-p6spy-0.2.zip
grails create-domain-class book

In DataSource.groovy, comment out the HSQLDB driver and uncomment the p6spy driver:

// driverClassName = “org.hsqldb.jdbcDriver”
driverClassName = “com.p6spy.engine.spy.P6SpyDriver” // use this driver to enable p6spy logging

Edit Book.groovy and add a property:

class Book {
String author
}

Now update BookTest.groovy to exercise the database:

class BookTests extends GroovyTestCase {
void testBook() {
def book1 = new Book(author:’paul’)
book1.save()
assertEquals(1, Book.count())

def book2 = Book.get(book1.id)
assertEquals(‘paul’, book2.author)

def books = Book.list()
assertEquals(1, books.size())
}
}

Now run your tests:

grails test-app

The file spy.log is created, which contains a trace of the sql statements invoked during the test.
06:46:24|17|3|statement||select sequence_name from information_schema.system_sequences
06:46:24|3|3|statement||create table book (id bigint generated by default as identity (start with 1), version bigint not null, author varchar(255) not null, primary key (id))
06:46:29|1|5|statement|insert into book (id, version, author) values (null, ?, ?)|insert into book (id, version, author) values (null, 0, ‘paul’)
06:46:29|0|5|statement|call identity()|call identity()
06:46:29|3|5|statement|select count(*) as y0_ from book this_|select count(*) as y0_ from book this_
06:46:29|-1||resultset|select count(*) as y0_ from book this_|y0_ = 1
06:46:29|0|5|statement|select this_.id as id0_0_, this_.version as version0_0_, this_.author as author0_0_ from book this_|select this_.id as id0_0_, this_.version as version0_0_, this_.author as author0_0_ from book this_
06:46:29|-1||resultset|select this_.id as id0_0_, this_.version as version0_0_, this_.author as author0_0_ from book this_|
06:46:29|0|5|statement|select this_.id as id0_0_, this_.version as version0_0_, this_.author as author0_0_ from book this_|select this_.id as id0_0_, this_.version as version0_0_, this_.author as author0_0_ from book this_
06:46:29|-1||resultset|select this_.id as id0_0_, this_.version as version0_0_, this_.author as author0_0_ from book this_|
06:46:29|1|5|statement|delete from book where id=? and version=?|delete from book where id=1 and version=0

This file has several columns, delimited by the | (pipe) character. The last column is the most interesting – it contains the SQL with the parameters inlined. More information about configuring P6Spy can be found on the P6Spy website.

{ 12 comments… read them below or add one }

Eric P February 10, 2008 at 3:10 pm

Hey there – quick heads up that the P6Spy plugin is broken for Grails 1.0. A few things moved around (no TestDataSource, etc).

paul February 20, 2008 at 4:51 pm

Thanks, I’ll fix it and produce a new release soon. I appreciate the feedback.

paul February 22, 2008 at 1:25 am

Ah, the wiki hasn’t been updated with the release 0.2 information (sorry, I should have done this last September). I’ve just tried grails-p6spy-0.2 with grails-1.0 and it works fine.

I have updated the wiki with the information about 0.2 as per this page – you can see the changes at http://docs.codehaus.org/display/GRAILS/p6spy+plugin but it appears the changes have not made it through to http://grails.codehaus.org/p6spy+plugin – there must be some delay in syncing.

If you work through the steps in the article above, you should find it works.

DANNY March 22, 2009 at 1:06 am

Hi prule ,

I install the p6spy 0.4 plugin in grails 1.1, but I cant find spy.log

Because I have SQL error , But I dont know the grails use detail SQL
I add datasource add loggingSql = true
It’s only show “select aa from test where id = ? ”

hope the help , thx

paul March 22, 2009 at 5:23 pm

Danny – have you changed the driverClassName in DataSource.groovy?

If you follow the instructions, you should find spy.log in the root of your grails application.

In DataSource.groovy, comment out the HSQLDB driver and uncomment the p6spy driver:

//String driverClassName = "org.hsqldb.jdbcDriver"
String driverClassName = "com.p6spy.engine.spy.P6SpyDriver"
DANNY March 22, 2009 at 6:56 pm

Hi paul , I uncomment the “com.p6spy.engine.spy.P6SpyDriver”
And comment “org.hsqldb.jdbcDriver”
Then I get follow exception :
org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class ‘com.p6s
py.engine.spy.P6SpyDriver’ for connect URL ‘jdbc:hsqldb:mem:testDb’

What can I do ?

thank you

paul March 24, 2009 at 8:32 am

I’ve successfully tried grails 1.1 with the p6spy plugin, following the instructions on the plugin page http://grails.org/plugin/p6spy

After creating the grails application ‘test’ and installing the plugin I have the p6spy jar in:

~/.grails/1.1/projects/test/plugins/p6spy-0.4/lib/p6spy-1.3.jar

Can you please confirm that you’ve created a new grails 1.1 project and installed the plugin with

grails create-app test
grails install-plugin p6spy

Can you see the p6spy jar in the .grail directory (in your home directory).

Are you on Windows, Linux, or Mac?

DANNY March 24, 2009 at 9:59 pm

Hi paul , I try to create project and install-plugin in linux , then it’s work

But it’s not work in Windows , I can see p6spy in .grail directory (Windows)

paul March 26, 2009 at 3:36 pm

Ok, I’ll try it on windows and see. (I use Linux – but there shouldn’t be anything OS specific). I’ll get back to you when I’ve tried it.

DANNY March 30, 2009 at 2:43 pm

Hi , thank you
hope your reuslt :)

hbagchi November 11, 2009 at 2:07 am

I am still facing the same problem while installing p6spy plugin in windows XP prof SP 3

Cannot create JDBC driver of class ‘com.p6spy.engine.spy.P6SpyDriver’ for connect URL
‘jdbc:hsqldb:file:devDB;shutdown=true’
java.sql.SQLException: No suitable driver

paul December 7, 2010 at 11:41 pm

See http://jira.codehaus.org/browse/GRAILSPLUGINS-1351 – Seems to be a windows issue because it has a space in the path to the configuration ie. “C:\ Documents and Settings\\.grails\1.1.1\projects\\resources”.

Leave a Comment

Previous post:

Next post: