Archive for Grails

08 Mar 2010

Beware of the default acegi Grails plugin setup

1 Comment Blog, Grails, Programming

This is firmly in the “putting it up here just in case anyone else has the same problem” category.  There is a gotcha that bites when writing a Grails application that uses the acegi security plugin along with a postgresql database as the data source.  The problem is that the acegi plugin will, by default, create a domain class with the name “User”.  When you try to run the application with a postgresql datasource, GORM will try to create a table with the name “user”, which postgresql will not like as it is a reserved word.

This is a particularly annoying bug, because “user” is not a reserved word in other RDBMs, so the error will only become apparent when you switch from your development HSQLDB to postgres, by which time you will have have references to Users scattered throughout your application.

Thankfully, the fix is not too onerous; either rename your User class to something else (and do a tentative find/replace in your code) or use a custom GORM table mapping:

class User {
  ..
  static mapping = {
      table 'myappname_user'
  }
}
12 Feb 2010

A pitfall to avoid when using the Grails UIPerformance plugin

No Comments Blog, Grails, Programming

A quick warning to anyone who’s using the excellent UIPerformance plugin for grails:  don’t do what I did, and set up your bundles with identical names:

uiperformance.bundles = [

    [type: 'css',
        name: 'bundled',
        files: ['reset',
            'style',
            'invalid',
            'jquery.jcarousel']],

    [type: 'js',
        name: 'bundled',
        files: [
            'jquery-1.3.2.min',
            'simpla.jquery.configuration',
            'facebox',
            'jquery.jcarousel.pack',
            'cert']]
]

Unsurprisingly, this causes the plugin to get confused; the following works much better:

uiperformance.bundles = [

    [type: 'css',
        name: 'bundled_css',
        files: ['reset',
            'style',
            'invalid',
            'jquery.jcarousel']],

    [type: 'js',
        name: 'bundled_javascript',
        files: [
            'jquery-1.3.2.min',
            'simpla.jquery.configuration',
            'facebox',
            'jquery.jcarousel.pack',
            'cert']]
]
05 Feb 2010

Deploying a grails app on a VPS

No Comments Blog, Grails, Programming

I recently set up a grails app on a Slicehost VPS. I’ve been very happy with the Slicehost setup so far; here are a few notes that might be helpful.

  • I started out on a 256MB slice, but that was too slow, so I’ve upgraded to a 512MB slice, which seems to be working out OK.  Memory usage is still higher than I’d like, but there are a few options for tweaking that I haven’t looked into yet.
  • Using the Grail UI Performance plugin has made a massive difference in the responsiveness of the app.
  • I have gone for a straightforward setup, using Tomcat to serve both dynamic and static content.  From what I have read, this will probably slow things down a bit, but has the advantage of making admin much simpler. The main hassle so far has been implementing a 301 redirect for SEO purposes, but I fixed that as per my post here.

UPDATE:  after I added meshCMS to my VPS I started running out of PermGen space on a regular basis.  A quick search turned up some articles pointing out that this is a common problem with frameworks that generate a lot of classes dynamically.  I’ve since added -XX:MaxPermSize=256m to my CATALINA_OPTS and have had no problems since.

21 Jan 2010

Hibernate magic with lazy evaluation

No Comments Blog, Grails, Programming

I’ve just run across this phenomenon for the first time and it turns out that hibernate can be even lazier than I thought!  To illustrate lets use this time-honoured example in Grails.  

Here are our domain objects:

class Book {
    String title
    static belongsTo = [author : Author]
    static constraints = {
    }
}
class Author {
    String name
    static hasMany = [books : Book]
    static constraints = {
    }
}

and we’ll add some sample data in our bootstrap.groovy:

def init = { servletContext ->
    def alice = new Author(name: 'alice').save()
    def book1 = new Book(title: 'book1')
    def book2 = new Book(title: 'book2')
    alice.addToBooks(book1)
    alice.addToBooks(book2)
}

Now we’ll turn on SQL logging in Datasource.groovy, so we can see exactly when hibernate is issuing SQL queries:

dataSource {
	dbCreate = "create-drop" // one of 'create', 'create-drop','update'
	url = "jdbc:hsqldb:mem:devDB"
        loggingSql = true
}

Now let’s create a controller to hold our test code (just for convenience, this could also be done in a groovy console):

class TestController {

    def index = {

        println "starting controller"
        def alice = Author.findByName('alice')
        println "author name is $alice.name"
        def myBooks = alice.books
        println "books are now stored in a variable"
        myBooks.each({println it.title})
        println myBooks.class.name
        render "OK!"
    }
}

This is pretty straightforward – we’re using a dynamic finder to retrieve the Author we added in the bootstrap script, then getting the list of Books and storing them in a variable, then printing the title of each book.

Question: when do we expect to see the SQL query issued to retrieve the books? Grails uses lazy evaluation by default, so it won’t be at line 6, because we haven’t actually asked for the books field yet. I expected to see it at line 8 – here we are asking for the books field, so surely this is where hibernate will retrieve them. I was wrong – in fact the books are not retrieved until line 10, where we actually want to do something with the books. The output make this clear:

starting controller
Hibernate:
    select
        this_.id as id0_0_,
        this_.version as version0_0_,
        this_.name as name0_0_
    from
        author this_
    where
        this_.name=?
author name is alice
books are now stored in a variable
Hibernate:
    select
        books0_.author_id as author3_1_,
        books0_.id as id1_,
        books0_.id as id1_0_,
        books0_.version as version1_0_,
        books0_.author_id as author3_1_0_,
        books0_.title as title1_0_
    from
        book books0_
    where
        books0_.author_id=?
book2
book1
org.hibernate.collection.PersistentSet

So hibernate does not actually execute the SQL query to get the books when we ask for alice.books, but it is lazy enough to wait until we actually we try to iterate over the results. The last line of the output reveals how the magic happens – the object that myBooks points to is not a straightforward List, but a PersistentSet.

This caused me a fair amount of confusion when I was trying to optimise a Grails action by minimising the number of SQL queries issued when retrieving a large number of objects from a database. Now that I know the way that it really works, I find it both cool, and a little bit scary that this is going on behind the scenes. But that’s the trade-off you make when using a framework – you sacrifice a bit of your understanding for more rapid development.