Tomcat5.5+Oracle 配置数据库连接池

来源:互联网 发布:上海百度快照优化 编辑:程序博客网 时间:2024/06/05 17:51

一.         环境

Tomcat 5.5 + Oracle + eclipse 3.2.2 + MyEclipse 5.5.1 GA。

二.         驱动

将Oracle的驱动classes12.jar和classes111.zip(将后缀zip改为jar,据说不识别*.zip)拷到Tomcat5.5/common/lib下。

三.         配置Tomcat5.5/conf/server.xml

在<Host>标签里面的最后加上:

<Context path="/Test" docBase="Test" debug="5"reloadable="true" crossContext="true">

                           <Resource

                       name="jdbc/Test"

                       type="javax.sql.DataSource"

                       auth="Container"

                       username="myusername"

                       password="mypassword"

                       url="jdbc:oracle:thin:@localhost:1521:DBTest"

                       driverClassName="oracle.jdbc.driver.OracleDriver"

                       maxIdle="2"

                       maxWait="5000"

                       maxActive="4"/>                     

      </Context>

      

      据说在5.5版本里不能写成如下形式:

<Resource name="jdbc/epoliceQuery" auth="Container"

type="javax.sql.DataSource"/>

<ResourceParams name="jdbc/epoliceQuery">

      <parameter>

<name>factory</name>                               

<value>

org.apache.commons.dbcp.BasicDataSourceFactory

</value>

   </parameter>

   <parameter>

      <name>driverClassName</name>

      <value>

oracle.jdbc.driver.OracleDriver

</value>

   </parameter>

   <parameter>

      <name>url</name>

      <value>

jdbc:oracle:thin:@ localhost:1521:DBTest

</value>

   </parameter>

   <parameter>

       <name>username</name>

       <value>myusername</value>

   </parameter>

   <parameter>

         <name>password</name>

         <value>mypassword</value>

   </parameter>

   <parameter>

      <name>maxActive</name>

      <value>20</value>

   </parameter>

   <parameter>

      <name>maxIdle</name>

      <value>10</value>

   </parameter>

   <parameter>

      <name>maxWait</name>

      <value>10000</value>

   </parameter>

</ResourceParams>

</Resource>

      而且不能把这部分内容加到<Service>标签内,应加在<Host>标签内,不然就会抛异常报错“Cannotcreate JDBC driver of class '' for connect URL 'null'”。

   但很多网文,包括一些书上都要求加在<Service>标签内,但是我用的时候就是会出错。最后在网上搜索解决办法,改加在<Host>标签内就没有问题了。

四.         配置Tomcat5.5/conf/web.xml

在<web-app>标签内加入以下内容:

<resource-ref>

    <description>DB Connection</description>

    <res-ref-name>jdbc/Test</res-ref-name>

    <res-type>javax.sql.DataSource</res-type>

    <res-auth>Container</res-auth>

</resource-ref>

要求<res-ref-name>jdbc/Test</res-ref-name>中的“jdbc/Test”一定要与在server.xml中配置的Resource中的name一致,不然会出错。

五.         测试程序

可以写一个jsp测试连接池。

test.jsp内容如下:

<%@ page language="java" import="java.util.*"pageEncoding="gb2312"%>

<%@ page import="javax.naming.*" %>

<%@ page import="javax.sql.*" %>

<%@ page import="java.sql.*" %>

<%@ page import="java.util.*" %>

<%

String path = request.getContextPath();

String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01Transitional//EN">

<html>

  <head>

    <basehref="<%=basePath%>">

   

   <title>My JSP 'test.jsp' starting page</title>

   

      <meta http-equiv="pragma" content="no-cache">

      <meta http-equiv="cache-control" content="no-cache">

      <meta http-equiv="expires"content="0">   

      <meta http-equiv="keywords"content="keyword1,keyword2,keyword3">

      <meta http-equiv="description" content="This is my page">

      <!--

      <link rel="stylesheet" type="text/css" href="styles.css">

      -->

 

  </head>

 

  <body>

    <%

     DataSource ds = null;

     try{

           Context initCtx = new InitialContext();

           if(initCtx == null) {

               throw newException("No Context");

           }

           Context envCtx = (Context)initCtx.lookup("java:comp/env/");          

 

           //获取连接池对象并进行类型转换

                    ds = (DataSource)envCtx.lookup("jdbc/Test");

                    //ds =(DataSource)initCtx.lookup("java:comp/env/jdbc/Test");           

 

           if(ds!=null){

                 Connection cn=ds.getConnection();                

                 out.println("Connectionis OK!");

                 if(cn!=null){

                       out.println("cn is Ok!");

                       java.sql.Statement stmt = cn.createStatement();

                       ResultSet rst = stmt.executeQuery("select testName fromtestTable");

                       out.println("<p>rst is Ok!");

                       while(rst.next()){

                             out.println("<P>Ename:" + rst.getString("testName"));

                       }                                        

                        rst.close();                                        

                       stmt.close();                       

                       cn.close();

                 }else{

                       out.println("rst Fail!");

                 }

           }else{

                 out.println("Fail!");

           }

     }catch(Exception ne){

           out.println(ne);

     }

%>

  </body>

</html>

      

部署启动后,在浏览器中输入http://localhsot:8080/Test/test.jsp测试连接池是否正确工作。

六.         Tomcat启动加载类

我配置好数据库连接池后,用这种方法比以前查询数据库速度大有提高,但是很奇怪的是我重起Tomcat后,第一次查数据库总是很慢,但是之后就好多了。第一次大概要20s,但之后就不到1s。查找原因不是缓存的问题,后来调试发现第一次getConnection的时候,耗去了20s的绝大部分时间。很奇怪为什么会这样,有待继续研究。于是我就写了个Servlet,在Tomcat启动的时候执行,内容是进行一次getConnection,这样之后速度就快了。创建Servlet之后,在工程的WEB-INF/web.xml里加上:

<servlet>

<description>

This is the description of my J2EE omponent

</description>

<display-name>

This is the display name of my J2EE component

</display-name>

   <servlet-name>StartUpOnLoading</servlet-name>

<servlet-class>

com.test.servlet.StartUpOnLoading

</servlet-class>

   <load-on-startup>1</load-on-startup>

</servlet>

   主要是这句:“<load-on-startup>1</load-on-startup>”。这样在Tomcat启动的时候,就是自动运行这个Servlet了。