在groovy控制器/服务中使用非默认数据源
我在 dataSource.groovy 中定义了 2 个数据源,一个是默认的,
dataSource {
all variables
}
另一个是只读的
dataSource_readonly {
all variables
}
最初我曾经在控制器中使用默认数据源,所以:
class myController {
javax.sql.DataSource dataSource
def sql = new Sql(dataSource)
.............
现在要使用另一个数据源,我需要做这样的事情吗?
class myController {
javax.sql.DataSource dataSource_readonly
def sql = new Sql(dataSource_readonly)
.............
I have 2 datasources defined in my dataSource.groovy one is default
dataSource {
all variables
}
other one is readonly
dataSource_readonly {
all variables
}
Initially I used to use default datasource in my controller so:
class myController {
javax.sql.DataSource dataSource
def sql = new Sql(dataSource)
.............
so now to use the other datasource do I need to do something like this ?
class myController {
javax.sql.DataSource dataSource_readonly
def sql = new Sql(dataSource_readonly)
.............
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你有没有尝试过类似的事情:
Have you tried something like that:
是的,就像使用默认数据源一样,您可以使用自定义数据源名称在控制器/服务中声明一个属性,并且 grails 将使用自定义数据源 bean 自动初始化您的控制器/服务:
Yes, just like you can with the default datasource, you can declare a property in your controller/service with the custom datasource name, and grails will automatically initialize your controller/service with the custom datasource bean: