首页 / 知识

如何在Grails中访问两个数据库

2023-04-14 00:47:00

如何在Grails中访问两个数据库

How do you access two databases in Grails

Grails可以在其DataSources.groovy文件中非常轻松地为不同环境(开发,测试,生产)配置数据源,但是似乎没有在一个环境中配置多个数据源的功能。 如果我需要从同一个Grails应用程序访问多个数据库,该怎么办?


在Grails 2.x.x中,连接不同域类中的不同数据库非常容易。

例如

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
development {
    dataSource {//DEFAULT data source
      .
      .
    }
dataSource_admin { //Convention is dataSource_name
        url ="//db url"
        driverClassName ="oracle.jdbc.driver.OracleDriver"
        username ="test"
        password = 'test123'
    }
dataSource_users {

    }
}

您可以通过以下方式使用域类中的任何数据源:

1
2
3
4
5
6
7
8
9
10
11
class Role{
   static mapping = {
      datasource 'users'
   }
}

 class Product{
    static mapping = {
      datasource 'admin'
   }
 }

有关更多详细信息,请参见此


如果使用Grails 2.0或更高版本,则不需要插件,它本身受支持。

http://www.grails.org/doc/latest/guide/single.html#multipleDatasources


现在有了Grails插件,可以直接在Grails的GORM层中使用多个数据源:

A Grails Plugin for Multiple DataSources


Grails 2.0无需插件即可处理多个数据源:

针对dev(h2 dataSource)和test(mysql dataSource_mysql)环境使用不同数据源的示例:

DataSource.groovy:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
dataSource {
    pooled = true
    driverClassName ="org.h2.Driver"
    username ="sa"
    password =""
}
dataSource_mysql {
    dialect = org.hibernate.dialect.MySQLInnoDBDialect
    driverClassName = 'com.mysql.jdbc.Driver'
    username ="user"
    password ="pass"
    url ="jdbc:mysql://mysqldb.com/DBNAME"
}
hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = false
    cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
}

// environment specific settings
environments {
    development {
        dataSource {
            configClass = HibernateFilterDomainConfiguration.class
            dbCreate ="update" // one of 'create', 'create-drop', 'update', 'validate', ''
            url ="jdbc:h2:file:../devDb;MVCC=TRUE"
            sqlLogging = true
        }
    }
    test {
        dataSource_mysql {
            configClass = HibernateFilterDomainConfiguration.class
            dbCreate ="create" // one of 'create', 'create-drop', 'update', 'validate', ''
            sqlLogging = true
        }
    }
    production {
        dataSource {
            dbCreate ="update"
            url ="jdbc:h2:prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
            pooled = true
            properties {
               maxActive = -1
               minEvictableIdleTimeMillis=1800000
               timeBetweenEvictionRunsMillis=1800000
               numTestsPerEvictionRun=3
               testOnBorrow=true
               testWhileIdle=true
               testOnReturn=true
               validationQuery="SELECT 1"
            }
        }
    }
}

您真的要这样做吗?以我的经验,通常的情况是:

  • 应用程序在自己的数据库架构中管理自己的数据
  • 通常,应用程序将需要来自其他来源的数据(例如,因此参考数据不会被复制和粘贴)
  • 通常,在一个数据库实例上驻留的所有模式通常都很豪华。因此,我的应用程序:

    • 仅具有一个数据库连接-这是它拥有的模式并且具有读/写访问权限
    • 其他应用程序通过视图"导出"其数据
    • 我的应用程序具有对这些视图的读取访问权限,并且具有该视图的同义词,从而使其在本地显示

    使用视图的原因是为了使正在公开数据的应用程序

  • 明确知道它正在导出以及正在导出什么
  • 不会公开架构的内部结构(因此,如果内部结构发生更改,只要视图正确,那么消耗应用程序就不会知道)
  • 实际上,我不需要使用Grails应用程序来执行此操作,但是该方法应该可行。

    跨应用程序共享数据的另一种方法是创建一个Web服务来公开数据。 Grails使这变得容易。

    希望能有所帮助,但是这种方法可能不适用于所有情况。


    以下帖子似乎是有关此主题的最佳信息来源:

    如何在grails中获取mutli-dataSource

    归结为:

    • 在DevelopmentDataSource中定义datasource1
    • 在resources.xml中定义datasource2
    • 使用datasource2为域对象的CRUD编写DAO
    • 在hibernate.cfg.xml中,列出所有域对象。

    只有第一个数据源将具有动态查找器方法。

    如果您的查询非常简单,并且不介意没有ORM功能,则可以使用Groovy SQL或Hibernate的本机SQL功能。


    数据库配置数据源文件

    最新内容

    相关内容

    热门文章

    推荐文章

    标签云

    猜你喜欢