Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining an alternate connection pool in Grails 2.3.6

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!

like image 644
Jesús Zazueta Avatar asked Sep 10 '14 18:09

Jesús Zazueta


1 Answers

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!

like image 103
Jesús Zazueta Avatar answered Nov 13 '22 17:11

Jesús Zazueta