6

Configuring Grails to use your own DataSource or Standard DataSource proxy

 3 years ago
source link: https://www.codesd.com/item/configuring-grails-to-use-your-own-datasource-or-standard-datasource-proxy.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Configuring Grails to use your own DataSource or Standard DataSource proxy

advertisements

In an application I want to use my own implementation of javax.sql.DataSource that extends the standard org.apache.commons.dbcp.BasicDataSource used by Grails and adds the functionality to set the client identifier based on the currently logged in user at the Grails application.

What is the best way to change the underlying javax.sql.DataSource implementation in a Grails application?

At the moment I see two possibilities:

  • change the implementation of the DataSource that is used by Grails
  • proxy the DataSource that is used by Grails and add the functionality with AOP

Any hints on how to deal with this requirement?


here is my resources.groovy

import org.codehaus.groovy.grails.commons.ConfigurationHolder as CH

// Place your Spring DSL code here
beans = {

    /**
     * c3P0 pooled data source that forces renewal of DB connections of certain age
     * to prevent stale/closed DB connections and evicts excess idle connections
     * Still using the JDBC configuration settings from DataSource.groovy
     * to have easy environment specific setup available
     */
    dataSource(com.mchange.v2.c3p0.ComboPooledDataSource) { bean ->
        bean.destroyMethod = 'close'
        //use grails' datasource configuration for connection user, password, driver and JDBC url
        user = CH.config.dataSource.username
        password = CH.config.dataSource.password
        driverClass = CH.config.dataSource.driverClassName
        jdbcUrl = CH.config.dataSource.url
        //force connections to renew after 2 hours
        maxConnectionAge = 2 * 60 * 60
        //get rid too many of idle connections after 30 minutes
        maxIdleTimeExcessConnections = 30 * 60
    }

}

i'm using c3p0 ComboPooledDataSource


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK