【方法一】在Tomcat4中,配置连接池,常见的就是在server.XML和web.xml中作相应配置。在针对单个项目文件情况下,Tomcat5中我们可以不在server.xml中配置Context(实际上,我们在server.xml中默认情况下没有看见Context标记),可以在\jakarta-tomcat-5.0.27\conf\Catalina\localhost下针对具体的文件夹名.xml(如:我的是blog.xml)中配置Context,这样也就免去了在web.xml中还要配置。
<?xml version='1.0' encoding='gb2312'?>
<Context displayName="Cactus Blog Web Application 1.0" docBase="F:\jakarta-tomcat-5.0.27\webapps\blog" path="/blog" workDir="work\Catalina\localhost\blog">
<Resource auth="Container" description="Oracle DataBase" name="jdbc/blog" type="Javax.sql.DataSource"/>
<ResourceParams name="jdbc/blog">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>-1</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>10</value>
</parameter>
<parameter>
<name>passWord</name>
<value>。。</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=blog</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</parameter>
<!--对应oracle参数
<parameter>
<name>url</name>
<value>jdbc:oracle:thin:@localhost:1521:mydata</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
-->
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
</ResourceParams>
</Context>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
总结:Tomcat5中,可以不在server.xml和web.xml中配置就能实现连接池配置;这应该是Tomcat5的功能相对Tomcat4的提高,有些朋友在Tomcat5中(使用在Tomcat4中配置方法)来配置连接池不能正常使用,可以采用这个方法,更快捷!!
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
【方法二】看到关注这方面内容人比较多,考虑到各人理解层度不一样,现把常见配置方式具体步骤也增加上来(tomcat-5.0.27),对tomcat5都适用。
第一步:在tomcat的治理员界面中设置Data Source(common\lib下增加oracle驱动:classes12.jar.classes12-for oracle8.jar)
JNDI Name: jdbc/mydata
Data Source URL: jdbc:oracle:thin:@localhost:1521:mydata
JDBC Driver Class: oracle.jdbc.driver.OracleDriver
User Name: 。。。 //如system
Password: 。。。//如manager
Max. Active Connections: 4
Max. Idle Connections:2
Max. Wait for Connection: 5000
Validation Query:
第二步:在\jakarta-tomcat-5.0.27\webapps\DeoWeb\WEB-INF的web.xml配置
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>My Web Application</display-name>
<description>
MY homesite.
</description>
<resource-ref>
<description>
oracle DataSource
</description>
<res-ref-name>
jdbc/mydata
</res-ref-name>
<res-type>
javax.sql.DataSource
</res-type>
<res-auth>
Container
</res-auth>
</resource-ref>
</web-app>
第三步:启动tomcat后conf\confserver.xml自动变成了这个样子(自动发布为全局连接池):
<?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 name="jdbc/mydata" 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/mydata">
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>4</value>
</parameter>
<parameter>
<name>password</name>
<value>manager</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:oracle:thin:@localhost:1521:mydata</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>2</value>
</parameter>
<parameter>
<name>username</name>
<value>system</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">
<Context path=" " docBase="."/>
<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>
第四步:写了个简单jsp测试:
<!DOCTYPE Html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page contentType="text/html; charset=iso-8859-1" %>
<%@ 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></title>
<%
out.println("Test begin!");
try{
InitialContext ctx=new InitialContext();
out.println("First sUCcess!");
DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mydata");
out.println("Second success!");
Connection conn = ds.getConnection();
out.println("Third success!");
Statement stmt = conn.createStatement();
String strSql = "select * from sys.signon";
ResultSet rs = stmt.executeQuery(strSql);
while(rs.next()){
out.print(rs.getString(1)); }
out.print("我的测试结束");
}
catch(Exception ex){
out.print("There is exception:"+ex.getMessage());
ex.printStackTrace();
}
%>
</head>
<body>
</body></html>