Sunday, 30 September 2007

Why are you coding?

There is a good article here about the need to explain why you are coding. I've often found that with most projects or tasks people are reluctant to give you the high level picture - it wastes too much time and, "you don't need to know".

It reminded me of several situations where someone comes to me and says - "I want you to write a program that does this...". I say "sure", expecting that they have done the due diligence and arrived at the conclusion that this is the correct thing to do. (I've even had my knuckles wrapped several times for second-guessing these requests).

So, you proceed with development and somewhere along the line (at the watercooler, elevator or kitchen) you mention what you are doing, and they hit you with "There's no need to do that because system XYZ is being replaced with ABC" or some other equally jaw-dropping statement.

I like to know the bigger picture of where a piece of work fits in, because then I can do a much better job. After all, I'm not just a monkey. And it's not my job to just code. I am supposed to be building solutions. And without the bigger picture this is a lot harder.

Note: I am a advocate for 'project induction' where new team members are taken aside and explained how everything fits together, and how they fit in to the picture. For programmers, this is a technical session (NOT an HR or project management session) and should be run by the architects of the system. This is a good way to discover the talents of your team, and to make them feel involved. I've not had much buy-in though, anyone out there seen it being done effectively?

Wednesday, 19 September 2007

grails-p6spy-0.2

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.

Wednesday, 5 September 2007

Changing code in Grails services

I've been using Grails for the last couple of months, and I'm loving it. While looking up some reference information, I stumbled across this gem.

Its always been frustrating when updating services that the server needed to be restarted. Well, this problem has been caused because I've been specifying the type of my services instead of using the def keyword. Now that I'm not specifying the type, no more server restarts!