Issue Injecting dataSource into Grails Service

1.7k views Asked by At

I am running into an issue when trying the following in my Serivce:

class SendingMailService {

def dataSource // the Spring-Bean "dataSource" is auto-injected

def sendXLSMail() {

def db = new Sql(dataSource) 

//additional code such as query, execution follows

}
}

When I execute this code I get the error: "Must specify a non-null Connection". From what I have read I believe the code above should work (this is not a Unit Test)...so clearly I am missing something with respect to Grails Services?

Thank you for any help here,

1

There are 1 answers

2
Dennis On BEST ANSWER

Ok months later I had some time to look into this one.

I tried a couple of things to get the dataSource to autoinject into a service but I kept getting a null connection. Ultimately I landed on passing the datasource from the controller which is not very elegant but at least it works. I hope this helps someone else out as I have seen some similar issues out there. Still would like to know why the autoinject does not work but I think I would have to dig deeper into Grails to understand that.

class MyController {

def dataSource

def someControllerMethod() {

MyService myService = new MyService()
myService.serviceMethodQuery(dataSource) 

}
}

//////////////////////

class MyService {

def serviceMethodQuery(Object dataSource){

//use your datasource as you normally would
}

}