configure Grails app to use JDBC connection pool

坚强是说给别人听的谎言 提交于 2019-11-29 02:04:43

The easiest thing to do would probably be to use the jdbc-pool plugin. Since the configuration options for this pool are intentionally very similar to Commons DBCP (they're documented here) you can use the plugin to define the jar dependency and manage switching the class for you. The plugin hasn't been updated in a year so it's a little out of date (the plugin uses version 1.0.9.0 but the latest is 1.0.9.3) so you might want to define the plugin dependency excluding the jar, and add one for the newer version. It's in the ebr repo, so you'll need to add that to your BuildConfig.groovy (see the plugin's version for how he did it).

There are configuration notes for the pool here and a series of blog posts by the author here.

If you do want to configure this without using the plugin, add the ebr repo and the jar dependency to BuildConfig.groovy:

repositories {
   inherits true
   ...
   ebr()
}

dependencies {
   runtime('org.apache.tomcat:com.springsource.org.apache.tomcat.jdbc:1.0.9.3') {
      transitive = false
   }
}

and create an override for the dataSource bean in resources.groovy:

import org.apache.tomcat.jdbc.pool.DataSource

beans = {

   dataSource(DataSource) {
      // mandatory
      driverClassName = '${dataSource.driverClassName}'
      username = '${dataSource.username}'
      password = '${dataSource.password}'
      url = '${dataSource.url}'
      // optional
      minEvictableIdleTimeMillis=1800000
      timeBetweenEvictionRunsMillis=1800000
      numTestsPerEvictionRun=3
      testOnBorrow=true
      testWhileIdle=true
      testOnReturn=true
      validationQuery="SELECT 1"
   }
}

It's convenient to use single-quoted strings with ${} placeholders to take advantage of Spring's property placeholder functionality and keep things DRY since you've already set the driver and connect info in DataSource.groovy.

In DataSource.groovy I use the following:

environments {
    integration {
        dataSource {
            pooled = false
            jndiName = "java:/comp/env/jdbc/myJndiName"
        }
    }
 }

And everything else is defined by Tomcat - this just needs to match it. There is no need to define any dataSource bean in resources.groovy

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