Configuration 配置Grails以使用自己的数据源实现或代理标准数据源

Configuration 配置Grails以使用自己的数据源实现或代理标准数据源,configuration,grails,proxy,aop,datasource,Configuration,Grails,Proxy,Aop,Datasource,在一个应用程序中,我想使用我自己的javax.sql.DataSource实现,它扩展了Grails使用的标准org.apache.commons.dbcp.BasicDataSource,并添加了基于Grails应用程序中当前登录用户设置客户端标识符的功能 在Grails应用程序中更改底层javax.sql.DataSource实现的最佳方法是什么 目前,我看到两种可能性: 更改Grails使用的数据源的实现 代理Grails使用的数据源,并使用AOP添加功能 关于如何处理此需求的任何提示

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

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

目前,我看到两种可能性:

  • 更改Grails使用的数据源的实现
  • 代理Grails使用的数据源,并使用AOP添加功能

关于如何处理此需求的任何提示?

您是否尝试在resources.groovy中配置自己的数据源?这里有一篇博客文章(不是我的)介绍了这个过程


你需要的东西就在最后。

这是我的参考资料。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
    }

}

我正在使用c3p0ComboPooledDataSource

谢谢!这对我来说很有前途。这对Grails2.3.6仍然有效吗?谢谢