Friday, 29 June 2007

Grails P6Spy plugin

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


I wanted to have a go at creating a Grails plugin so I thought I'd write one to make it easy to add P6Spy to your Grails application. The experience was very good. The structure and distribution mechanism is excellent and the Grails guys have done a great job.

Here's the result:

grails-p6spy-0.1.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.1/grails-app/conf/spy.properties

  • <project directory>/plugins/p6spy-0.1/lib/p6spy-1.3.jar


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

  • // String driverClassName = "com.p6spy.engine.spy.P6SpyDriver"


Example use


Download the plugin from
	http://www.javathinking.com/grails-p6spy-0.1.zip

Create a new Grails application, add a domain class, and install the plugin:
 grails create-app spytest
cd spytest
grails create-domain-class book
grails install-plugin grails-p6spy-0.1.zip

In TestDataSource.groovy, comment out the HSQLDB driver and uncomment the p6spy driver:
//   String driverClassName = "org.hsqldb.jdbcDriver"
String driverClassName = "com.p6spy.engine.spy.P6SpyDriver"

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.
20:07:16|16|1|statement||select sequence_name from information_schema.system_sequences
20:07:16|2|1|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))
20:07:20|0|1|statement|insert into book (id, version, author) values (null, ?, ?)|insert into book (id, version, author) values (null, 0, 'paul')
20:07:20|0|1|statement|call identity()|call identity()
20:07:20|2|1|statement|select count(*) as y0_ from book this_|select count(*) as y0_ from book this_
20:07:20|-1||resultset|select count(*) as y0_ from book this_|y0_ = 1
20:07:20|0|1|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_
20:07:20|-1||resultset|select this_.id as id0_0_, this_.version as version0_0_, this_.author as author0_0_ from book this_|
20:07:21|0|1|statement|delete from book|delete from book


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.

Thursday, 28 June 2007

How To Forge

If you need to do anything on Linux, make sure you check out HowToForge - this site has some great walk throughs showing how to set up different distributions in different ways. The guys here really know their stuff and have created some fantastic documentation here. They've got guides for installing the major distros with easy to follow instructions รข€“ copy and paste command lines - quality stuff. Good work guys, it's great to see such quality available.

Friday, 22 June 2007

Hibernating to disk with Ubuntu

Since moving to Ubuntu 7.04 on my laptop I've had a couple of problems (hibernate/suspend, battery life, wireless) but just recently I did a system update which contained a kernel update. Its now running 2.6.20-16-generic and there seems to be a significant difference in hibernate to disk.

Before it was very hit and miss - sometimes it worked, sometimes it didn't. Now it seems much more reliable - hibernate and resume although it seems to take a while it looks like it works.

There is a strange battery problem though. When I'm running on battery, it is only a couple of minutes before the system starts warning me that I only have minutes left. The battery light on the laptop flashes rapidly and the battery meter in Gnome shows empty. However, if I just carry on working I can still get half and hour or more out of it.

Even stranger is that when I shutdown or hibernate while in this condition, the laptop will not boot again until it is plugged in to the power. I think it genuinely thinks the battery is flat. I can get it to boot if I plug it in to the power just long enough to get through grub and start loading linux. Then I can disconnect the power and continue working on the (not so flat as it thinks) battery.