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

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

问题描述

在一个应用程序中,我想使用我自己的 javax.sql.DataSource 实现,它扩展了 Grails 和使用的标准 org.apache.commons.dbcp.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?

目前我看到两种可能性:

At the moment I see two possibilities:

  • 更改 Grails 使用的 DataSource 的实现
  • 代理 Grails 使用的 DataSource 并使用 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天全站免登陆