本文以
Tomcat5.0.28
和
Mysql
为环境来描述,其中
%Tomcat _Home%
为
Tomcat
的安装目录,
bookstore
为我的测试项目,放置在
%Tomcat _Home%/webapps
下
1
、
%Tomcat _Home%/conf
下找到
server.xml
文件,在
<Host></Host>
结点之间加入数据库链接池的详细信息,如下所示:
<
Context
path
="/bookstore"
docBase
="bookstore"
debug
="5"
reloadable
="true"
crossContext
="true"
>

<
Logger
className
="org.apache.catalina.logger.FileLogger"
prefix
="localhost_bookstore_log."
suffix
=".txt"
timestamp
="true"
/>

<
Resource
name
="jdbc/bookstore"
auth
="Container"
type
="javax.sql.DataSource"
/>

<
ResourceParams
name
="jdbc/bookstore"
>
<
parameter
>
<
name
>
factory
</
name
>
<
value
>
org.apache.commons.dbcp.BasicDataSourceFactory
</
value
>
</
parameter
>

<!--
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 0 for no limit.
-->
<
parameter
>
<
name
>
maxActive
</
name
>
<
value
>
100
</
value
>
</
parameter
>

<!--
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.
-->
<
parameter
>
<
name
>
maxIdle
</
name
>
<
value
>
30
</
value
>
</
parameter
>

<!--
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.
-->
<
parameter
>
<
name
>
maxWait
</
name
>
<
value
>
10000
</
value
>
</
parameter
>

<!--
MySQL dB username and password for dB connections
-->
<
parameter
>
<
name
>
username
</
name
>
<
value
>
root
</
value
>
</
parameter
>
<
parameter
>
<
name
>
password
</
name
>
<
value
>
sa
</
value
>
</
parameter
>

<!--
Class name for the old mm.mysql JDBC driver - uncomment this entry and comment next
if you want to use this driver - we recommend using Connector/J though
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>
-->
<!--
Class name for the official MySQL Connector/J driver
-->
<
parameter
>
<
name
>
driverClassName
</
name
>
<
value
>
com.mysql.jdbc.Driver
</
value
>
</
parameter
>
<!--
The JDBC connection url for connecting to your MySQL dB.
The autoReconnect=true argument to the url makes sure that the
mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
connection. mysqld by default closes idle connections after 8 hours.
-->
<
parameter
>
<
name
>
url
</
name
>
<
value
>
jdbc:mysql://localhost:3306/bookstore?autoReconnect=true
</
value
>
</
parameter
>
</
ResourceParams
>
</
Context
>
其中
bookstore
为你的
Web
项目,具体相关信息就是数据库连接池信息
2
、在
%Tomcat _Home%/common/lib
和
bookstore/WEB-INF/lib
下加入
mysql
的
jdbc
驱动,笔者所用为
mysql-connector-java-5.0.3-bin.jar
3
、修改
bookstore/WEB-INF
下的
web.xml
文件,如下所示:
<?
xml version="1.0" encoding="ISO-8859-1"
?>
<
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
>
MySQLDBCP
</
description
>
<
resource-ref
>
<
description
>
DB Connection
</
description
>
<
res-ref-name
>
jdbc/bookstore
</
res-ref-name
>
<
res-type
>
javax.sql.DataSource
</
res-type
>
<
res-auth
>
Container
</
res-auth
>
</
resource-ref
>
</
web-app
>
4
、在
bookstore
下建立测试文件,
index.jsp
,关键代码如下所示:
<%
Context ctx
=
new
InitialContext();
DataSource ds
=
(DataSource)ctx.lookup(
"
java:comp/env/jdbc/bookstore
"
);
Connection conn
=
ds.getConnection();
。。。。。。。。
%>
具体信息可参考官方网站:
http://tomcat.apache.org/tomcat-5.0-doc/jndi-datasource-examples-howto.html
本文介绍如何在Tomcat环境中配置MySQL数据库连接池,包括修改server.xml和web.xml文件,添加JDBC驱动,以及创建测试页面。
1万+

被折叠的 条评论
为什么被折叠?



