美文网首页
grails 3.0以上版本配置多数据源

grails 3.0以上版本配置多数据源

作者: 在路上的小海贼 | 来源:发表于2018-11-28 12:01 被阅读0次

    因为工作需要,grails升级了3.0.9版本,而且需要配置多数据源。
    grails 2.1的多数据源配置比较简单,但3.0以上的版本数据库配置更改到application.yml中进行配置,如何配置多数据源,百度和bing上搜索无答案(原谅我不太会翻墙用谷歌)。
    这个问题找来找去,困扰了我两三天,终于解决了。虽然官方的文档中解释的比较全面了,但还是有些误差。
    先说一下,grails 3.0以上版本数据库的基本配置:
    dataSource:
    pooled: true
    jmxExport: true
    driverClassName: oracle.jdbc.driver.OracleDriver
    username: test
    password: test
    properties:
    maxActive = -1
    minEvictableIdleTimeMillis=1800000
    timeBetweenEvictionRunsMillis=1800000
    numTestsPerEvictionRun=3
    testOnBorrow=true
    testWhileIdle=true
    testOnReturn=true
    validationQuery="SELECT 1 FROM DUAL"
    environments:
    development:
    dataSource:
    dbCreate: update
    url: jdbc:oracle:thin:@ip:port:sid
    production:
    dataSource:
    dbCreate: update
    url: jdbc:oracle:thin:@ip:port:sid
    需要注意的是,application.yml中对于数据的对齐方式有严格的要求,如果未正常对齐,可能会报错;而且,冒号后面如果有内容,必须空一格,如:
    dbCreate: update
    多数据源的配置如下:
    dataSources:
    dataSource:
    pooled: true
    jmxExport: true
    driverClassName: oracle.jdbc.driver.OracleDriver
    username: test
    password: test
    properties:
    maxActive = -1
    minEvictableIdleTimeMillis=1800000
    timeBetweenEvictionRunsMillis=1800000
    numTestsPerEvictionRun=3
    testOnBorrow=true
    testWhileIdle=true
    testOnReturn=true
    validationQuery="SELECT 1 FROM DUAL"
    gibmsSource:
    pooled: true
    jmxExport: true
    driverClassName: oracle.jdbc.driver.OracleDriver
    username: test1
    password: test1
    properties:
    maxActive = -1
    minEvictableIdleTimeMillis=1800000
    timeBetweenEvictionRunsMillis=1800000
    numTestsPerEvictionRun=3
    testOnBorrow=true
    testWhileIdle=true
    testOnReturn=true
    validationQuery="SELECT 1 FROM DUAL"
    environments:
    development:
    dataSources:
    dataSource:
    dbCreate: update
    url: jdbc:oracle:thin:@ip:port:sid
    gibmsSource:
    dbCreate: update
    url: jdbc:oracle:thin:@ip1:port1:sid1
    production:
    dataSources:
    dataSource:
    dbCreate: update
    url: jdbc:oracle:thin:@ip:port:sid
    gibmsSource:
    dbCreate: update
    url: jdbc:oracle:thin:@ip1:port1:sid1
    多数据源也可以配置domian,mapping中配置如下:
    class TestGibms {
    String code
    static mapping = {
    datasource 'gibmsSource'
    }
    static constraints = {
    code nullable: true, maxSize: 80
    }
    }
    class TestGibms {
    String code
    static mapping = {
    datasources(['gibmsSource','ds1'])
    }
    static constraints = {
    code nullable: true, maxSize: 80
    }
    }
    关于配置其他数据源的domain如何使用.save()方法我还没研究好,按照文档中的配置使用是无法保存的,此处有待研究。。。
    在service中如果使用Gsql,可以使用多数据源,定义数据源时规则如下:def dataSource_gibmsSource。不是官方文档中的static datasource = 'gibmsSource',此为直接给datasource 定义为一个字符串变量;也不是static dataSource = 'gibmsSource',这样定义和def dataSource 效果一样,使用的是默认数据源;也不是def gibmsSource,这样得到的是null。service示例如下:
    class TestService {
    def dataSource_gibmsSource
    def insertTest(){
    Sql sql = new Sql(dataSource_gibmsSource)
    def gibmsSql = "insert into test_gibms(code) values ('000')"
    def result = sql.executeInsert(gibmsSql)
    sql.close()
    }
    }
    在我解决此问题之后,有同事告诉我可以在controllers中用以下方式配置多数据源,暂时未经验证,应该也是可以的,但感觉比较麻烦,还是通过application.yml配置起来比较正规和方便些。示例如下:
    Sql sql = Sql.newInstance("jdbc:mysql://localhost:1081/wyl","root","root","com.mysql.jdbc.Driver");
    感想:
    1、这个问题在网上中文的搜索里连提问的都很少,答案更是没有了,所以记录一下,以备他人使用;
    2、技术性的问题,如果中文搜索不到,可以考虑使用英文搜索;
    3、太过于懒惰,想直接找到别人的使用经验,实际上最后还是转回到grails的文档上,以后遇到问题还是需要多查查文档;
    4、官方文档也有错的地方,调试怎么也无法通过,查询了另外一篇英文资料才找到解决方法。
    参考:
    http://stackoverflow.com/questions/31105556/multiple-datasources-in-grails-3-service
    http://fossies.org/linux/www/grails-docs-3.1.3.zip/guide/pages/multipleDatasources.html
    http://burtbeckwith.com/blog/?p=70

    相关文章

      网友评论

          本文标题:grails 3.0以上版本配置多数据源

          本文链接:https://www.haomeiwen.com/subject/vjeaqqtx.html