This article suggests Tomcat 7 apps should use a JDBC connection pool instead of a commons-dbcp connection pool. However, the latter is the default for a Grails app, and it's not obvious how to change it.
My guess is that I need to define a Spring bean in resources.groovy
that overrides a bean that is normally created by default, but I've no idea what this bean should be named or what properties I need to set.
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
来源:https://stackoverflow.com/questions/9550700/configure-grails-app-to-use-jdbc-connection-pool