配置Grails以使用自己的DataSource实现或代理标准DataSource

ste*_*ase 5 configuration proxy grails aop datasource

在一个应用程序中,我想使用我自己的实现javax.sql.DataSource,扩展了org.apache.commons.dbcp.BasicDataSourceGrails使用的标准,并添加了基于Grails应用程序中当前登录用户设置客户端标识符的功能.

javax.sql.DataSource在Grails应用程序中更改底层实现的最佳方法是什么?

目前我看到两种可能性:

  • 更改Grails使用的DataSource的实现
  • 代理Grails使用的DataSource并使用AOP添加功能

有关如何处理此要求的任何提示?

Ole*_*ndr 4

这是我的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
    }

}
Run Code Online (Sandbox Code Playgroud)

我正在使用c3p0 ComboPooledDataSource