简体   繁体   English

在resources.groovy中定义多个dataSource

[英]Defining multiple dataSource in resources.groovy

I am trying to create multiple dataSource in DataSource.groovy something like: 我试图在DataSource.groovy中创建多个dataSource,例如:

[1..9].each {
   "dataSource_db$it" {
      driverClassName = 'oracle.jdbc.OracleDriver'
      username = 'xxx${it}xx'
      password = 'xxx${it}xx'
      url = 'jdbc:oracle:thin:@xxx:xxx:xxx'
   }
}

But this is not working. 但这是行不通的。 Seems like groovy code is not working in DataSource.groovy. 似乎groovy代码在DataSource.groovy中不起作用。 So, I used another alternative to move this to resources.groovy by specifying beans: 因此,我通过指定bean使用了另一种方法将其移动到resources.groovy:

import org.apache.commons.dbcp.BasicDataSource
// Place your Spring DSL code here
beans = {
    [1..9].each {
       "dataSource_db$it"(BasicDataSource) {
       driverClassName = 'oracle.jdbc.OracleDriver'
       username = 'xxx${it}xx'
       password = 'xxx${it}xx'
       url = 'jdbc:oracle:thin:@xxx:xxx:xxx'
    }
  }
}

But I got another issue where BasicDataSource is not recognized. 但我遇到另一个无法识别BasicDataSource的问题。 Which plugin do I need to install? 我需要安装哪个插件? Is my approach correct? 我的方法正确吗? I am using Grails 2.4.3. 我正在使用Grails 2.4.3。

We switched from the Commons pool to the the much more performant Tomcat JDBC Connection Pool a while back. 不久前,我们从Commons池切换到性能更高的Tomcat JDBC连接池 It uses most of the same syntax as the Commons pool, so it had the added benefit of being a drop-in replacement that usually requires no config changes other than the driver class name. 它使用与Commons池大多数相同的语法,因此它具有直接替换的附加优点,即通常不需要驱动程序类名称就无需进行任何配置更改。 It's developed by the Tomcat team but doesn't require any Tomcat dependencies, and can be used in any servlet container. 它是由Tomcat团队开发的,但不需要任何Tomcat依赖项,并且可以在任何servlet容器中使用。

The class name to use with this pool implementation is org.apache.tomcat.jdbc.pool.DataSource , so if you change to that in resources.groovy and you'll get past that issue. 与该池实现一起使用的类名称为org.apache.tomcat.jdbc.pool.DataSource ,因此,如果在resources.groovy更改为该名称,则可以解决该问题。

If you keep the DataSource.groovy version and run grails console , then execute 如果您保留DataSource.groovy版本并运行grails console ,则执行

for (name in ctx.beanDefinitionNames.sort()) {
   println name
}

you'll see that you end up with only a second bean with a very odd name: 您会看到最后只剩下一个名称非常奇怪的bean:

dataSource_db[1, 2, 3, 4, 5, 6, 7, 8, 9]

(plus all of the other secondary beans that are created for each DataSource with similar suffixes). (以及为每个具有类似后缀的DataSource创建的所有其他辅助bean)。

The same thing happens in resources.groovy after you fix the class name; 修复类名称后, resources.groovy也会发生相同的情况; I assume the problem here is that the Spring beans DSL handler is interfering with Groovy code that would otherwise work; 我认为这里的问题是Spring bean DSL处理程序正在干扰Groovy代码,否则它们将起作用。 I was able to get it working with a regular for loop: 我能够使其与常规的for循环一起使用:

for (int i = 1; i < 10; i++) {
   "dataSource_db$i"(BasicDataSource) {
      driverClassName = 'oracle.jdbc.OracleDriver'
      username = 'xxx${it}xx'
      password = 'xxx${it}xx'
      url = 'jdbc:oracle:thin:@xxx:xxx:xxx'
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM