配置Grails以使用自己的DataSource实现或代理标准DataSource [英] Configuring Grails to use own DataSource implementation or to proxy the standard DataSource

查看:515
本文介绍了配置Grails以使用自己的DataSource实现或代理标准DataSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

两种可能性:


  • 更改Grails使用的DataSource的实现

  • 代理DataSource由Grails使用并添加AOP的功能

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

Any hints on how to deal with this requirement?

推荐答案

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

i'm using c3p0 ComboPooledDataSource

这篇关于配置Grails以使用自己的DataSource实现或代理标准DataSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆