Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

configure Grails app to use JDBC connection pool

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.

like image 815
Dónal Avatar asked Mar 03 '12 23:03

Dónal


People also ask

How do I set up connection pooling?

Click Resources > JDBC > Data Sources > data_source > [Additional Properties] Connection pool properties. Click Resources > JMS->Queue connection factories-> queue_connection_factory ->[Additional Properties] Connection pool.

What is used for JDBC connection pooling?

JDBC 3.0 API Framework. The JDBC 3.0 API provides a general framework with "hooks" to support connection pooling rather than specifying a particular connection pooling implementation. In this way, third-party vendors or users can implement the specific caching or pooling algorithms that best fit their needs.

How does Tomcat JDBC connection pool work?

Tomcat jdbc pool implements the ability retrieve a connection asynchronously, without adding additional threads to the library itself. Tomcat jdbc pool is a Tomcat module, it depends on Tomcat JULI, a simplified logging framework used in Tomcat. Retrieve the underlying connection using the javax. sql.


1 Answers

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.

like image 79
Burt Beckwith Avatar answered Oct 16 '22 23:10

Burt Beckwith