Defining an alternate connection pool in Grails 2.3.6

落花浮王杯 提交于 2019-12-19 02:42:13

问题


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!


回答1:


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!



来源:https://stackoverflow.com/questions/25772324/defining-an-alternate-connection-pool-in-grails-2-3-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!