Create Sql Bean

Example code is available on

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

results matching ""

    No results matching ""