Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain handle to Grails datasource

I want a handle to connection object in grails controller class (written in groovy). I read somewhere that I need to first obtain datasource object and call a method getConnection on it to get the connection object.

I am unable to find how to obtain the datasource object in grails controller. Kindly help.

like image 571
Vishal Avatar asked Jan 22 '23 13:01

Vishal


1 Answers

The datasource is simply a bean. It can be injected by Spring into pretty much anything.

    def dataSource

But I would recommend that you inject the dataSource into a Service, do your getConnection call there and whatever work you need done. I think that's a better separation of responsibilities.

Then inject your service into your controller and call it.

like image 175
zentuit Avatar answered Jan 25 '23 04:01

zentuit