我可以在 groovy 中有 2 个具有不同权限的不同数据源吗

发布于 2024-12-15 21:57:14 字数 82 浏览 2 评论 0原文

我想知道我们是否可以在 Grails 应用程序中配置 2 个数据源,我希望一个具有只读访问权限(选择),另一个具有读写访问权限(创建、插入、更新..)

I was wondering if we can have 2 datasources configured in a Grails application, I want one to have readonly access (select) and the other to have read-write access (create, insert, update..)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不必在意 2024-12-22 21:57:14

对于 2.0 之前的应用程序,请使用 http://grails.org/plugin/datasources/ ,对于 2.0 版本,它是内置于 Grails 中 - 请参阅 http://grails.org/doc/2.0.0.RC1/guide/conf.html#multipleDatasources

For pre-2.0 apps use http://grails.org/plugin/datasources/ and for 2.0 it's built into Grails - see the docs at http://grails.org/doc/2.0.0.RC1/guide/conf.html#multipleDatasources

谜泪 2024-12-22 21:57:14

是的,您可以,但根据我的经验,您应该注意一些条件/限制。

  1. 知道这一点:
    Grails 2 无法使用 Spring Security 登录使用多个数据库

  2. 文档http://grails.org/doc/2.0.0.RC1/guide/conf.html#multipleDatasources 表示您可以通过域映射或使用服务和静态“数据源”在数据源之间切换" 属性。

我发现域映射确实有效,但服务本身不起作用,从我解释文档的方式来看,我们应该能够做到这一点。

2.1 在Datasources.groovy中定义数据源:
您可以声明您希望域模型使用的数据源,这很好用。另外,声明您的域使用多个数据源,然后指定在控制器中使用哪个数据源:
bookInstance.db1.save()
或者
bookInstance.db2.save()

或者,当数据源事先未知时,您可以执行以下操作:

def ds = "db1" // 或将其设置为您想要的任何内容,db1、db2、db3...
bookstance."$ds".save()

但是,如果您想使用服务来声明数据源,那对我来说根本不起作用。

static datasource = "db1"

无论如何,这在 Grails Services 中对我来说不起作用。

2.2 在默认数据库中声明数据源,然后在运行时注入其他数据源。
文档说您可以在运行时注入一个新的数据源 bean。你可以,但是 Grails 不会接受它。我无法让 Grails 了解新注入的数据源 bean。因此,数据源必须在 Datasource.grooy 中预先声明

-

Yes you can but - from my experience - with some conditions/limitations that you should be aware of.

  1. Know this:
    Grails 2 can't login with spring security when using multiple databases

  2. The documentation http://grails.org/doc/2.0.0.RC1/guide/conf.html#multipleDatasources says that you can switch between datasources either via domain mapping or by using services and the static "datasource" property.

I have found that the domain mapping does work, but the services alone does not work and from the way I interpreted the documentation we should be able to do either.

2.1 Define Datasources in Datasources.groovy:
You can declare what datasource you want your domain models to use, that works fine. As well, declare your domain to use several datasources and then specify which datasource to use in your controllers:
bookInstance.db1.save()
or
bookInstance.db2.save()

OR, when the datasource is unknown before hand you can do this:

def ds = "db1" // or set it to anything you want, db1, db2, db3...
bookstance."$ds".save()

BUT, if you want to use services to declare the datasource, that does NOT work at all for me.

static datasource = "db1"

No matter what, this has not worked for me in Grails Services.

2.2 Declare Datasource in the default Database and then inject other datasources at runtime.
The documentation says that you can inject at runtime a new datasource bean. And you can, but it does not get picked up by Grails. I have not been able to get Grails to be aware of the newly injected datasource beans. So, the datasources MUST be pre-declared in Datasource.grooy

-

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文