在Grails 2.3.6中定义一个备用连接池 [英] Defining an alternate connection pool in Grails 2.3.6

查看:133
本文介绍了在Grails 2.3.6中定义一个备用连接池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,在Grails 1.X和Grails 2.X之间的某个时刻,默认连接池库从 commons-dbcp 更改为 tomcat-dbcp



现在,我正在尝试将BoneCP或HikariCP配置为Grails应用程序的连接池库。

但是,我发现提供了一个可能只适用于Grails 1.X的解决方案。



我还发现<一个href =https://gist.github.com/allnightlong/929543 =nofollow noreferrer>这个Gist ,但是我不知道它适用于哪个Grails版本。



那么,是否可以在Grails 2.3.6应用程序中定义自定义连接池?谢谢!

解决方案

更新:确定您确实需要告诉Grails strong>汇集数据源,因为HikariCP现在正在处理这个问题。



如果我离开开关,我的应用程序中就会看到连接奇怪。所以请说:

  pooled = false 



好​​的,@Joshua Moore是对的。

我试着用更新的Grails方法做这件事,这是我的相关部分 resources.groovy 文件。据我所知, Datasource.groovy 中的配置值在运行时被拉入到 resources.groovy 中,之后目标运行时环境已被识别(开发测试生产

  def config = Holders.config 
def dataSources = config.findAll {
it .key.toString()。contains(dataSource_)
}

dataSources.each {key,value - >
def ds = value
$ {key}(HikariDataSource,{bean - >

def hp = new Properties()
hp.username = ds .username
hp.password = ds.password
hp.connectionTimeout = 6000
hp.maximumPoolSize = 60
hp.jdbcUrl = ds.url
hp.driverClassName = ds.driverClassName

HikariConfig hc = new HikariConfig(hp)
bean.constructorArgs = [hc]
})
}

这是我的 DataSource.groovy 配置的相关部分:

  //特定于环境的设置
环境{
开发{
dataSource_myapp1 {
pooled = false
username =CONFIGURE_ME_EXTERNALLY
password =CONFIGURE_ME_EXTERNALLY
driverClassName ='oracle.jdbc.OracleDriver'
dialect ='org.hibernate.dialect.Oracle10gDialect'
url ='jdbc:oracle:thin: @ MYDBHOST1:1521 / MYSERVICEID1'
}
dataSource_myApp2 {
pooled = false
username =CONFIGURE_ME_EXTERNALLY
password =CONFIGURE_ME_EXTERNALLY
driverClassName =' oracle.jdbc.OracleDriver'
dialect ='org.hibernate.dialect.Oracle10gDialect'
url ='jdbc:oracle:thin:@ MYDBHOST2:1521 / MYSERVICEID2'
}
}
}

在我的情况中,它几乎与 test 生产环境。谢谢!


I know that, at some point between Grails 1.X and Grails 2.X, the default connection pooling library changed from commons-dbcp to tomcat-dbcp.

Now, I'm trying to configure either BoneCP or HikariCP as the connection pooling library for my Grails application.

However, I see that this answer offers a solution which might only apply to Grails 1.X.

I also found this Gist, but again, I don't know which Grails version it applies to.

So, is it possible to define a custom connection pool inside a Grails 2.3.6 application? Thanks!

解决方案

UPDATE: OK so you actually need to tell Grails not to pool the datasources, since HikariCP is now taking care of this.

I saw connection weirdness in my apps if I left that switch on. So instead say:

pooled = false

OK yeah, @Joshua Moore is right.

I tried doing it with updated Grails methods and this is the relevant section of my resources.groovy file. As far as I can understand, the configuration values in Datasource.groovy are pulled into resources.groovy at runtime, after the target runtime environment has been identified (development, test or production).

def config = Holders.config
def dataSources = config.findAll {
  it.key.toString().contains("dataSource_")
}

dataSources.each { key, value ->
  def ds = value
  "${key}"(HikariDataSource, { bean ->

     def hp = new Properties()
     hp.username = ds.username
     hp.password = ds.password
     hp.connectionTimeout = 6000
     hp.maximumPoolSize = 60
     hp.jdbcUrl = ds.url
     hp.driverClassName = ds.driverClassName

     HikariConfig hc = new HikariConfig(hp)
     bean.constructorArgs = [hc]
  })
}

And this is the relevant section of my DataSource.groovy configuration:

// environment specific settings
environments {
   development {
      dataSource_myapp1 {
         pooled = false
         username = "CONFIGURE_ME_EXTERNALLY"
         password = "CONFIGURE_ME_EXTERNALLY"
         driverClassName = 'oracle.jdbc.OracleDriver'
         dialect = 'org.hibernate.dialect.Oracle10gDialect'
         url = 'jdbc:oracle:thin:@MYDBHOST1:1521/MYSERVICEID1'
      }
      dataSource_myApp2 {
         pooled = false
         username = "CONFIGURE_ME_EXTERNALLY"
         password = "CONFIGURE_ME_EXTERNALLY"
         driverClassName = 'oracle.jdbc.OracleDriver'
         dialect = 'org.hibernate.dialect.Oracle10gDialect'
         url = 'jdbc:oracle:thin:@MYDBHOST2:1521/MYSERVICEID2'
      }
   }
}

In my case, it's pretty much the same for test and production environments. Thanks!

这篇关于在Grails 2.3.6中定义一个备用连接池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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