I have a following piece of code which connects to database and executes a query, I have a no clarity in placing of this code(Model/Service).
def value
def url = ConfigurationHolder.config.dataSource.url
def username = ConfigurationHolder.config.dataSource.username
def password = ConfigurationHolder.config.dataSource.password
def driver = ConfigurationHolder.config.dataSource.driverClassName
def sql = Sql.newInstance(url, username, password, driver)
sql.eachRow("select field_value from application_configuration where field_name=?", [field]) {
value=it.field_value
}
I have a class called ApplicationConfiguaration, I am querying on this domain.
I have two doubts
1) where to put the Database connection logic
2) where to put the query execution logic
If you haven’t created a domain object to represent
application_configuration, you should consider that, and use domain object finders or HQL.If you need to perform the query outside of GORM, it should probably go in a service. Note that a DataSource object can be automatically injected into your service and that a Sql object can be created directly from it. You could have something like this: