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

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

问题描述

这篇文章建议Tomcat 7应用程序应该使用JDBC连接池而不是commons-dbcp连接池。然而,后者是Grails应用程序的默认设置,并且不清楚如何改变它。



我的猜测是我需要在 resources.groovy ,它会覆盖通常默认创建的bean,但我不知道该bean应该被命名或需要设置哪些属性。

解决方案

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



游泳池的配置说明此处以及作者此处的一系列博文。



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

 存储库{
继承了真正的
.. 。
ebr()
}

依赖关系{
runtime('org.apache.tomcat:com.springsource.org.apache.tomcat.jdbc:1.0。 9.3'){
transitive = false
}
}

并为中的 dataSource bean创建覆盖> resources.groovy

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

$ {

dataSource(DataSource){
//强制
driverClassName ='$ {dataSource.driverClassName}'
username =' $ {dataSource.username}'
password ='$ {dataSource.password}'
url ='$ {dataSource.url}'
//可选
minEvictableIdleTimeMillis = 1800000
timeBetweenEvictionRunsMillis = 1800000
numTestsPerEvictionRun = 3
testOnBorrow = true
testWhileIdle = true
testOnReturn = true
validationQuery =SELECT 1
}



$ b

使用带单引号的字符串 $ { } 占位符可以充分利用Spring的属性占位符功能,并且可以让事情保持干爽已经在 DataSource.groovy 中设置了驱动程序和连接信息。


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.

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.

解决方案

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.

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
   }
}

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"
   }
}

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天全站免登陆