tomcat6数据库连接池(DBCP)配置(mysql为例)

因为JNDI配置方式在tomcat5.0和tomcat5.5.x进行过修改,所以tomcat6的JNDI配置与先前有些不同,这篇文章只介绍在tomcat6下面配置数据库连接池。
tomcat中的DBCP支持JDBC2.0规范,但是如果使用1.4版本一上的JVM的话,则支持JDBC3.0规范。如果想使用JDBC3.0的功能请使用1.4版本一上的JVM。
使用DBCP需要安装以下两个组件:
1.Commons DBCP
2.Commons Pool
tomcat安装目录下的lib/tomcat-dbcp.jar已经包含这两个组件了。
tomcatJNDI配置是写在context中的。在tomcat6中context有5种定义方式:
1.将数据库连接池的配置写在 $CATALINA_BASE/conf/context.xml中,这样的配置可以被tomcat服务器上所有的服务加载
2.写在文件$CATALINA_BASE/conf/[enginename]/[hostname]/context.xml.default中,同样这个文件可以被这个host下的所有服务加载。
3.写在文件$CATALINA_BASE/conf/[enginename]/[hostname]/***.xml中
4.在所要发布的web应用下的/META-INF/context.xml(如果没有则创建)中写上配置内容。这样在web应用发布的时候这个xml配置文件会自动复制到$CATALINA_BASE/conf/[enginename]/[hostname]/目录下面,并重命名为web应用名字.xml。
5.是将配置内容写在cong/server.xml文件中。(不建议这种方式,因为这样修改context配置需要重新启动tomcat服务器)
[color=red]我建议采用第四种方式[/color],这样的好处是应用开发者可以自己配置数据库连接池而不用修改服务器端的配置,而且也支持热部署。
数据库连接池context的属性
数据库连接池默认是不支持设置自动释放长时间未用的connection的。可以通过设置removeAbandoned="true"来是DBCP连接池支持自动释放长时间未用的连接。默认的时间是300秒,可以通过设置removeAbandonedTimeout="60"来将释放延时改为60秒或者其他。
下面是mysql 数据库的context配置示例
<Context path="/DBTest" docBase="DBTest"
reloadable="true" crossContext="true">

<!-- maxActive: Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to -1 for no limit.
-->

<!-- maxIdle: Maximum number of idle dB connections to retain in pool.
Set to -1 for no limit. See also the DBCP documentation on this
and the minEvictableIdleTimeMillis configuration parameter.
-->

<!-- maxWait: Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->

<!-- username and password: MySQL dB username and password for dB connections -->

<!-- driverClassName: Class name for the old mm.mysql JDBC driver is
org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
-->

<!-- url: The JDBC connection url for connecting to your MySQL dB.
-->

<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javatest"/>

</Context>

将上面的context配置通过开始时介绍的集中配置方式配置到服务器上。然后就是在web应用中修改配置,在 WEB-INF/web.xml添加如下内容:
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<description>MySQL Test App</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>

这样就可以在web应用中使用数据库连接池了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值