Tomcat
数据库连接池配置
前言
:
1
准备
mysql
的
jdbc
驱动程序
2
安装
Tomcat
默认全部装在
D:\Server\tomcat5.0 D:\Server\tomcat5.5 D:\Server\tomcat6.0
3
以
TOMCAT_HOME
代替个版本的安装目录
4
将
jdbc
驱动放入
TOMCAT_HOME\ common\lib\
下面
5
如果是其他数据库的话,就是换一下
jdbc
驱动以及各种参数就可以了的
6
配置
tomcat
数据源时的各种参数的详细介绍
7
其他设置。
1.
Tomcat 4.0
待添加
2.
Tomcat 5.0
2.1
建一个虚拟目录
test (TOMCAT_HOME\webapps\)
可以通过
http://localhost:8080/test
访问你的网站了
2.2
配置
Context (
在
TOMCAT_HOME\conf\Catalina\localhost\
下
建
test.xm,)
test.xml:
<?xml version="1.0" encoding="utf-8"?>
<Context docBase="dbpool" path="/test" workDir="work\Catalina\localhost\test">
</Context>
2.3
配置
server.xml
server.xml
的内容
:
红色表示加入的
(
或者通过
http://localhost:8080/admin
进行可视化配置
)
<?xml version='1.0' encoding='utf-8'?>
<Server>
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"/>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<GlobalNamingResources>
<Environment name="simpleValue" type="java.lang.Integer" value="30"/>
<Resource auth="Container" description="User database that can be updated and saved" name="UserDatabase" type="org.apache.catalina.UserDatabase"/>
<Resource auth="Container" name="jdbc/mysql" type="javax.sql.DataSource"/>
<ResourceParams name="UserDatabase">
<parameter>
<name>factory</name>
<value>org.apache.catalina.users.MemoryUserDatabaseFactory</value>
</parameter>
<parameter>
<name>pathname</name>
<value>conf/tomcat-users.xml</value>
</parameter>
</ResourceParams>
<ResourceParams name="jdbc/mysql">
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>4</value>
</parameter>
<parameter>
<name>password</name>
<value>198395</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/study?autoReconnect=true</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>2</value>
</parameter>
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
</ResourceParams>
</GlobalNamingResources>
<Service name="Catalina">
<Connector acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" port="8080" redirectPort="8443" maxSpareThreads="75" maxThreads="150" minSpareThreads="25">
</Connector>
<Connector port="8009" protocol="AJP/1.3" protocolHandlerClassName="org.apache.jk.server.JkCoyoteHandler" redirectPort="8443">
</Connector>
<Engine defaultHost="localhost" name="Catalina">
<Host appBase="webapps" name="localhost">
<Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_log." suffix=".txt" timestamp="true"/>
</Host>
<Logger className="org.apache.catalina.logger.FileLogger" prefix="catalina_log." suffix=".txt" timestamp="true"/>
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"/>
</Engine>
</Service>
</Server>
2.4
配置
虚拟目录命名的
test.xml
test.xml:
红色表示加入的
<?xml version='1.0' encoding='utf-8'?>
<Context docBase="testds" path="/testds" workDir="work\Catalina\localhost\testds">
<Resource name="jdbc/mysql" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/mysql">
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>4</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://127.0.0.1:3306/study?useUnicode=true</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>2</value>
</parameter>
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
<parameter>
<name>password</name>
<value>198395</value>
</parameter>
</ResourceParams>
</Context>
配置完毕
3.
Tomcat 5.5
3.1
建虚拟目录
test(
在
TOMCAT_HOME/webapp
或者其地方
)
可以通过
http://localhost:8080/test
访问你的网站了
3.2
配置
server..xml(
或者可以通过
http://localhost:8080/admin
来可视化界面来完成
)
server.xml
内容
红色是加入的内容
<?xml version="1.0" encoding="UTF-8"?>
<Server>
<Listener className="org.apache.catalina.core.AprLifecycleListener"/>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<Listener className="org.apache.catalina.storeconfig.StoreConfigLifecycleListener"/>
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"/>
<GlobalNamingResources>
<Environment
name="simpleValue"
type="java.lang.Integer"
value="30"/>
<Resource
auth="Container"
description="User database that can be updated and saved"
name="UserDatabase"
type="org.apache.catalina.UserDatabase"
pathname="conf/tomcat-users.xml"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"/>
<Resource
name="jdbc/mysql"
type="javax.sql.DataSource"
password="198395"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2"
maxWait="5000"
username="sa"
url="jdbc:mysql://localhost:3306/study?autoReconnect=true"
maxActive="4"/>
</GlobalNamingResources>
<Service
name="Catalina">
<Connector
port="8080"
redirectPort="8443"
minSpareThreads="25"
connectionTimeout="20000"
maxSpareThreads="75"
maxThreads="150">
</Connector>
<Connector
port="8009"
redirectPort="8443"
protocol="AJP/1.3">
</Connector>
<Engine
defaultHost="localhost"
name="Catalina">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"/>
<Host
appBase="webapps"
name="localhost">
</Host>
</Engine>
</Service>
</Server>
3.3
配置
Context (
在
TOMCAT_HOME\conf\Catalina\localhost\
下
建
test.xml
与项目名字一样
)
test.xml
红色表示新加入的
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/test" docBase="test" reloadable="true" crossContext="true" debug="0" >
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="sa" password="198395" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/study?useUnicode=true"/>
</Context>
4.
Tomcat 6.0
5.
测试用例
DatasourceCon.jsp
<%@ page language="java" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Test of Tomcat connection pool</title>
</head>
<body>
<%
out.print("strat test connection pool!<br/>");
try{
Context initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:comp/env");
Object obj = (Object) ctx.lookup("jdbc/mysql");
DataSource ds = (javax.sql.DataSource)obj;
Connection conn = ds.getConnection();
out.print("tomcat mysql connection pool runs perfectly!");
conn.close();
}
catch(Exception ex){
out.print(ex.getMessage());
ex.printStackTrace();
}
%>
</body>
</html>
6.
总结
好长时间没有写这个了,最近因为项目的需要,根据以前配置过的,现在重新配置一下,然后写成详细文档!
posted on 2007-03-23 02:12
chunkyo 阅读(20389)
评论(3) 编辑 收藏 所属分类:
Java技术