I am creating an application in grails that should read from one database and write into another database. I have created datasources.groovy for this requirement and have installed the datasources plugin. However, I am stuck at how to use this datasource when executing an sql query (select * from……..etc. etc).
For eg. Below is how I run a query in my action. I am using customized queries and not gorm.
EDITED:
class TuneController {
def dataSource_ds2
def list = {
String nameSql = "select name from emp where id=3345"
Sql sql = new Sql(dataSource_ds2)
String name = sql.rows(nameSql)
println(name)
}
}
In the above case, datasources is not read and has a null value.
Is there any sample code available for this requirement.
Am I missing something here?
EDIT:
My Datasources.groovy entry is as below.
datasources = {
datasource(name:'ds2') {
domainClasses([com.Tune])
readOnly(true)
driverClassName('oracle.jdbc.driver.OracleDriver')
url('jdbc:oracle:thin:@test-ofr.wellmanage.com:1521:OFRS1')
username('test')
password('test')
environments(['development'])
dbCreate('do-not-bother')
logSql(true)
dialect(org.hibernate.dialect.Oracle10gDialect)
hibernate {
cache {
use_second_level_cache(false)
use_query_cache(false)
}
}
}
}
The secondary datasources are available using dependency injection, but their names are based on the names in Datasources.groovy. For example if you’ve defined a datasource named ‘foo’, then you would inject that with
def dataSource_foo:Note that you must put
def dataSource_fooas a class-scope field and not inside your action (or method). This is true for every dependency injection – if it’s inside a method or a closure it’s just a method-scope variable.