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

5
在一个应用程序中,我想使用自己的实现javax.sql.DataSource,该实现扩展了Grails使用的标准org.apache.commons.dbcp.BasicDataSource,并添加了基于当前登录用户在Grails应用程序中设置客户端标识符的功能。
在Grails应用程序中更改底层javax.sql.DataSource实现的最佳方法是什么?
目前我看到两种可能性:
  • 更改Grails使用的DataSource的实现
  • 代理Grails使用的DataSource,并使用AOP添加功能
对于如何处理此要求,有任何提示吗?
2个回答

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
    }

}

我正在使用 c3p0 ComboPooledDataSource


谢谢!这对我来说看起来非常有前途。 - stefanglase
嗨。这对于Grails 2.3.6仍然有效吗?谢谢! - Jesús Zazueta

3

你尝试过在resources.groovy中配置自己的数据源吗?这里有一篇博客文章(不是我写的),介绍了该过程。

http://burtbeckwith.com/blog/?p=312

你需要的内容在文章末尾。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接