Grails - 从不同的数据源获取数据并将其保存在我的Grails数据库中 [英] Grails - Getting data from a different datasource and saving it in my Grails database

查看:78
本文介绍了Grails - 从不同的数据源获取数据并将其保存在我的Grails数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Grails项目,需要从运行在不同项目上的数据库中检索数据。这个其他项目运行在不同的平台(Drupal)上,并且具有不同的域。我只需要阅读这个数据库中的一些表格,并将其保存在我自己的数据源中。

I have a a Grails project that needs to retrieve data from a database that is running on a different project. This other project is running on a different platform(Drupal), and has different domains. I just need read some of the tables in this database, and save it on my own datasource.

完成此操作的最佳方法是什么?

What would be the best way of accomplishing this?

推荐答案

最快捷的方法是使用GORM的支持多个数据源,它旨在用于在两个或多个数据库之间划分域类,但不必为此第二个数据源分配任何数据源。一个小缺点是,这样做会创建一个额外的事务管理器,Hibernate会话工厂,以及更多类和Spring bean,但是如果没有任何使用它们的内存,它们将不会占用太多内存。为此,请在 DataSource.groovy 中添加一个带有唯一后缀的第二个 dataSource 块(它不影响任何内容除了Spring bean的名字以外),比如

The quickest way would be to use GORM's support for multiple datasources, which is intended to be used to partition your domain classes between two or more databases, but you don't have to assign any to this second datasource. One small downside would be that doing it this way will create an additional transaction manager, Hibernate session factory, and a couple more classes and Spring beans, but they won't take up much memory without anything using them. To do this, add a second dataSource block in DataSource.groovy with a unique suffix (it doesn't affect anything other than the Spring bean names), e.g.

dataSource_drupal {
   pooled = true
   driverClassName = '...'
   username = '...'
   password = '...'
   url = '...'
}

由于此数据源不会用于GORM,因此您无需指定 dialect dbCreate ,或 jmxExport ,并且您不需要第二个 hibernate block,只有创建连接池所需的信息(默认会创建10个初始连接)。

Since this datasource won't be used for GORM you don't need to specify dialect, dbCreate, or jmxExport, and you don't need a second hibernate block, only the information needed to create the connection pool (it'll create 10 initial connections by default).

如果您担心这种方法的额外内存(你不应该,它会很小),你可以做更多的工作,并在 grails-app / conf / spring / resources.groovy中手动注册一个Spring bean。 code>。如果您使用Grails的最新版本,则数据源实现是 Tomcat JDBC连接池,所以使用它的驱动程序类和setter属性名来指定连接信息。使用任何有效的Spring bean名称,但我会遵循多个数据源支持中的约定:

If you're concerned about the extra memory of this approach (you shouldn't be, it will be minimal) you can do a bit more work and register a Spring bean manually in grails-app/conf/spring/resources.groovy. If you're using a recent version of Grails the datasource implementation is the Tomcat JDBC Connection Pool, so use its driver class and setter property names to specify the connection information. Use any valid Spring bean name, but I'd follow the conventions from the multiple datasources support:

import org.apache.tomcat.jdbc.pool.DataSource

beans = {

   dataSource_drupal(DataSource) { bean ->
        bean.destroyMethod = 'close'

        driverClassName = '...'
        url = '...'
        username = '...'
        password = '...'

        // optional extra settings, not really needed
        // unless you expect a lot of usage
        initialSize = 42
        testOnBorrow = true
        testWhileIdle = false
        testOnReturn = false
        validationQuery = 'SELECT 1'
   }
}

因此,要使用第二个数据源,依赖注入它将用于执行数据迁移工作的服务:

So to use your second datasource, dependency-inject it into the service(s) you'll use to do the data migration work:

def dataSource_drupal

和要执行SQL查询,您最好的选择是 groovy.sql.Sql ,因为它隐藏了涉及JDBC代码的大部分仪式。添加导入

and to do the SQL queries, your best bet is groovy.sql.Sql since it does a great job of hiding most of the ceremony involved with JDBC code. Add an import

import groovy.sql.Sql

,并创建一个将 DataSource bean传递给它的构造函数的新实例,以便它可以使用它获取连接:

and create a new instance passing the DataSource bean to its constructor so it can use that to get connections:

Sql sql = new Sql(dataSource_drupal)
sql.eachRow('select name, bar from foo' ) { row ->
   Foo foo = new Foo(name: row.name, bar: row.bar)
   if (!foo.save()) {
       log.error "Validation error(s) for data $row: $foo.errors"
   }
}

这篇关于Grails - 从不同的数据源获取数据并将其保存在我的Grails数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆