在grails中为应用程序使用两个数据库 [英] Using two databases for an application in grails
问题描述
我在grails中创建应该从一个数据库读取并写入另一个数据库的应用程序。我已经为这个需求创建了datasources.groovy,并安装了datasources插件。但是,我在执行一个sql查询(select * from ........ etc。)时遇到了如何使用这个数据源。
对于eg 。下面是我在我的操作中运行查询。
我使用自定义查询而不是gorm。
< 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)
}
}
在上述情况下,数据源未读取,并且具有空值。
我在这里缺少一些东西?
编辑:
我的Datasources.groovy条目如下。
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测试')
环境(['development'])
dbCreate('do-not-bother')
logSql(true)
dialect(org.hibernate.dialect.Oracle10gDialect )
hibernate {
cache {
use_second_level_cache(false)
use_query_cache(false)
}
}
}
}
辅助数据源可以使用依赖注入,基于Datasources.groovy中的名称。例如,如果你定义了一个名为foo的数据源,那么你将注入 def dataSource_foo
:
class MyController {
def dataSource_foo
def list = {
String nameSql =select name from emp where id = 3345
Sql sql = new Sql(dataSource_foo)
def rows = sql.rows(nameSql)
...
}
}
请注意,您必须将 def dataSource_foo
作为类范围字段,不在你的动作(或方法)内。这对于每个依赖注入是正确的 - 如果它在一个方法或闭包中,它只是一个方法范围变量。
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
:
class MyController {
def dataSource_foo
def list = {
String nameSql = "select name from emp where id=3345"
Sql sql = new Sql(dataSource_foo)
def rows = sql.rows(nameSql)
...
}
}
Note that you must put def dataSource_foo
as 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.
这篇关于在grails中为应用程序使用两个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!