Reputation: 61
In my application there are a few places where I want to use pure SQL. When I hard code the connection details in Sql.newInstance it works fine. For obvious reasons I would prefer to not hard code the connection details.
When I use the dataSource variable it comes up as null. My code in the controller is:
import groovy.sql.Sql
def dataSource
def sql = Sql.newInstance(dataSource)
sql.executeInsert("insert into....")
My code in the datasource config file is:
environments {
development {
dataSource {
dbCreate = "create-drop"
url = "jdbc:postgresql:mydev"
username = "xxx"
password = "xxx"
As you can see I'm using PostgreSQL. I've also tried it using the default grails database with the same results.
Any ideas would be appreciated.
Upvotes: 2
Views: 2880
Reputation: 8109
You need to define dataSource
outside of your controller action. Otherwise spring cannot do the required dependency injection for you:
class YouController {
def dataSource
def yourAction() {
def sql = new Sql(dataSource)
[..]
}
}
Upvotes: 3