Tomcat下配置MySQL数据库连接池
http://www.dvbbs.net/tech/program/2006042760299.asp
1.在$CATALINA_HOME/conf/server.xml中添加配置信息,声明连接池的具体信息,添加内容如下:
<!--声明连接池-->
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"/>
<!-- 对连接池的参数进行设置 -->
<ResourceParams name="jdbc/mysql">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>username</name>
<value>shopadm</value>
</parameter>
<parameter>
<name>password</name>
<value>123</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost/shopdb?useUnicode=true&charact-erEncoding=gb2312</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
</ResourceParams>
2. 在$CATALINA_HOME/conf/web.xml的</web-app>前添加如下信息:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
其中<res-ref-name>中的参数名必须和server.xml中声明的连接名一样。
3. 在$CATALINA_HOME/conf/catalina/localhost目录下找到需要进行数据库连接的当前程序的配置信息,比如这里是shopping.xml,在这个文件中添加如下信息:
<Context …>
…
<ResourceLink name=”jdbc/mysql” global=”jdbc/mysql” type=”javax.sql.DataSource”/>
…
</Context>
大功告成!
在此基础上,参考Tomcat官方网站的用户手册,摸索出另外一种配置连接池的方法,这个方法不需要对server.xml进行修改,只要对需要使用到连接池的程序的配置文档进行修改就可以了。方法如下:
1.$CATALINA_HOME/conf/catalina/localhost目录下找到需要数据库连接池的程序的配置文档,此处是shopping.xml。在<Context> </Context>之间添加如下信息,声明一个数据库连接池:
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/mysql">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>password</name>
<value>123</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost/shopdb?useUnicode=true&characterEncoding=gb2312</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
<parameter>
<name>username</name>
<value>shopadm</value>
</parameter>
</ResourceParams>
这里的参数和上一种方法中添加到server.xml里的信息几乎是完全一样的。
2.在对应程序的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">
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
===============================================================
Tomcat 5.5 配置 MySQL 数据库连接池
http://edu.chinadownz.com/Article/Mckf/Jsp/JSPDataBase/200505/10961.html
用了最新的几个咚咚,搞了整整一天终于搞清楚了Tomcat 5.5 配置 MySQL 数据库连接池,网上的经验并不能完全用到新环境里面,我写出整个过程以方便大家配置。
1 环境描述
JDK 1.5
Tomcat 5.5.4
MySQL 4.0.20
MySQL JDBC 3.0.15
Commons dbcp 1.2.1
2 准备工作
JDK, Tomcat, MySQL安装过程并非我的重点,略过。
从http://dev.mysql.com/downloads/下载mysql-connector-java-3.0.15-ga.zip,将其中的mysql-connector-java-3.0.15-ga-bin.jar放到jre/lib/ext和Tomcat 5.5/common/lib里面。
从http://jakarta.apache.org/commons/dbcp/下载commons-dbcp-1.2.1.zip,将其中的commons-dbcp-1.2.1.jar放到jre/lib/ext和Tomcat 5.5/common/lib里面。
3 配置Tomcat
在Tomcat 5.5/conf/server.xml的<GlobalNamingResources>中添加:
<Resource name="JDBC for MySQL" type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver" password="" maxIdle="2" maxWait="5000" username="root" url="jdbc:mysql://localhost/test" maxActive="4"/>。
在Tomcat 5.5/webapps/test/WEB-INF/web.xml的<web-app>中添加:
<resource-ref>
<description>MySQL Connection Pool</description>
<res-ref-name>JDBC for MySQL</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
在Tomcat 5.5/webapps/test/META-INF/context.xml的<Context>中添加:
<ResourceLink name="JDBC for MySQL" global="JDBC for MySQL" type="javax.sql.DataSourcer"/>
4 测试
重启Tomcat服务器,写一个test.jsp:
<!doctype html public "-//w3c//dtd html 4.0 transitional//en"
"http://www.w3.org/TR/REC-html40/strict.dtd">
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page session="false" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Test of MySQL connection pool</title>
</head>
<body>
<%
out.print("Start<br/>");
try{
Context initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:comp/env");
Object obj = (Object) ctx.lookup("JDBC for MySQL");
javax.sql.DataSource ds = (javax.sql.DataSource)obj;
Connection conn = ds.getConnection();
out.print("MySQL connection pool runs perfectly!");
conn.close();
}
catch(Exception ex){
out.print(ex.getMessage());
ex.printStackTrace();
}
%>
</body>
</html>
在浏览器中浏览该jsp文件,成功了哦。记得千万要用完close哦,不然一会就耗尽了。这一点可以通过以root身份登录MySQL,运行show processlist;命令来查看当前所有连接。