2

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,

Dennis
  • 401
  • 5
  • 17

1 Answers1

0

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
}

}
Dennis
  • 401
  • 5
  • 17
  • 4
    It looks like you aren't auto injecting your service into the controller. You shoudln't need to call `MyService myService = new MyService()` as that will kill any auto injection that the service needs, including dataSource – Joseph Apr 07 '14 at 19:34
  • @Joseph- Thanks for this, I did not realize that side effect. – Dennis Apr 08 '14 at 20:42