7

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!

Community
  • 1
  • 1
Jesús Zazueta
  • 1,160
  • 1
  • 17
  • 32

1 Answers1

8

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!

Jesús Zazueta
  • 1,160
  • 1
  • 17
  • 32
  • How are you loading the Oracle driver? I get an error when using this method. It seems that the Oracle driver is not loaded by the time the app gets to the resources.groovy running. 2016-02-19 09:07:10,767 [localhost-startStop-1] WARN util.DriverDataSource - Registered driver with driverClassName=oracle.jdbc.OracleDriver was not found, trying direct instantiation. – Scott Feb 19 '16 at 15:07
  • Another error in the stack trace: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Constructor threw exception; nested exception is com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: Exception during pool initialization: HikariPool-0 - Connection is not available, request timed out after 6002ms. – Scott Feb 19 '16 at 15:08