配置 Grails 应用程序以使用 JDBC 连接池 [英] configure Grails app to use JDBC connection pool

查看:30
本文介绍了配置 Grails 应用程序以使用 JDBC 连接池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本文建议使用Tomcat 7应用程序应该使用 JDBC 连接池而不是 commons-dbcp 连接池.但是,后者是 Grails 应用程序的默认设置,如何更改它并不明显.

This article suggests Tomcat 7 apps should use a JDBC connection pool instead of a commons-dbcp connection pool. However, the latter is the default for a Grails app, and it's not obvious how to change it.

我的猜测是我需要在 resources.groovy 中定义一个 Spring bean,它覆盖一个通常默认创建的 bean,但我不知道这个 bean 应该命名或什么我需要设置的属性.

My guess is that I need to define a Spring bean in resources.groovy that overrides a bean that is normally created by default, but I've no idea what this bean should be named or what properties I need to set.

推荐答案

最简单的方法可能是使用 jdbc-pool 插件.由于此池的配置选项有意与 Commons DBCP 非常相似(它们被记录在 此处) 您可以使用该插件来定义 jar 依赖项并为您管理切换类.该插件一年没有更新,所以有点过时了(插件使用版本 1.0.9.0 但 最新版本是 1.0.9.3) 所以你可能想要定义不包括 jar 的插件依赖项,并为较新的版本添加一个.它位于 ebr 存储库中,因此您需要将其添加到 BuildConfig.groovy(请参阅 插件版本 了解他是如何做到的).

The easiest thing to do would probably be to use the jdbc-pool plugin. Since the configuration options for this pool are intentionally very similar to Commons DBCP (they're documented here) you can use the plugin to define the jar dependency and manage switching the class for you. The plugin hasn't been updated in a year so it's a little out of date (the plugin uses version 1.0.9.0 but the latest is 1.0.9.3) so you might want to define the plugin dependency excluding the jar, and add one for the newer version. It's in the ebr repo, so you'll need to add that to your BuildConfig.groovy (see the plugin's version for how he did it).

这里 和作者的一系列博客文章这里.

There are configuration notes for the pool here and a series of blog posts by the author here.

如果您确实想在不使用插件的情况下进行配置,请将 ebr 存储库和 jar 依赖项添加到 BuildConfig.groovy:

If you do want to configure this without using the plugin, add the ebr repo and the jar dependency to BuildConfig.groovy:

repositories {
   inherits true
   ...
   ebr()
}

dependencies {
   runtime('org.apache.tomcat:com.springsource.org.apache.tomcat.jdbc:1.0.9.3') {
      transitive = false
   }
}

并为 resources.groovy 中的 dataSource bean 创建一个覆盖:

and create an override for the dataSource bean in resources.groovy:

import org.apache.tomcat.jdbc.pool.DataSource

beans = {

   dataSource(DataSource) {
      // mandatory
      driverClassName = '${dataSource.driverClassName}'
      username = '${dataSource.username}'
      password = '${dataSource.password}'
      url = '${dataSource.url}'
      // optional
      minEvictableIdleTimeMillis=1800000
      timeBetweenEvictionRunsMillis=1800000
      numTestsPerEvictionRun=3
      testOnBorrow=true
      testWhileIdle=true
      testOnReturn=true
      validationQuery="SELECT 1"
   }
}

使用带有 ${} 占位符的单引号字符串来利用 Spring 的属性占位符功能并保持 DRY 很方便,因为您已经在 中设置了驱动程序和连接信息数据源.groovy.

It's convenient to use single-quoted strings with ${} placeholders to take advantage of Spring's property placeholder functionality and keep things DRY since you've already set the driver and connect info in DataSource.groovy.

这篇关于配置 Grails 应用程序以使用 JDBC 连接池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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