Create Sql Bean

Example code is available on github.com/vaadin-on-grails/groovy-sql.

We can use Groovy Sql class to access a database. The proper way to create new instance of Sql class is to define new bean in resources.groovy.

import groovy.sql.Sql

beans = {
    sql(Sql, ref('dataSource')) { bean ->
        bean.destroyMethod = 'close'
    }
}

If, from any reason, you would need to create Sql manually, you can the alternative approach to create Sql each time before SQL execution.

Sql sql = new Sql(dataSource: Grails.applicationContext.getBean('dataSource'))
// execute your queries
sql.close()

Last updated