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

发布于 2024-09-12 18:34:02 字数 369 浏览 18 评论 0原文

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

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

目前我看到两种可能性:

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

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

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.

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

At the moment I see two possibilities:

  • change the implementation of the DataSource that is used by Grails
  • proxy the DataSource that is used by Grails and add the functionality with AOP

Any hints on how to deal with this requirement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╰つ倒转 2024-09-19 18:34:02

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

here is my 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
    }

}

i'm using c3p0 ComboPooledDataSource

打小就很酷 2024-09-19 18:34:02

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

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

你需要的东西在最后。

did you try configuring your own datasource in resources.groovy? Here is a blog post (not mine) that goes over the process

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

the stuff you need is at the end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文