Tomcat5.5.23下配置连接池

本文介绍如何在Tomcat服务器中配置Oracle数据库连接池,并提供了一个示例程序用于验证连接池配置的有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在conf/server.xml中的<host> </host>中间加上

         <Context path="" docBase="ROOT" debug="5" reloadable="true" crossContext="true">
         <Resource name="jdbc/newoa" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="user" password="pass" driverClassName="oracle.jdbc.driver.OracleDriver"
               url="jdbc:oracle:thin:@192.168.1.10:1521:orcl"/>
        </Context> 

如不在ROOT下面使用,则应该如下配置:

         <Context path="/Struts2" docBase="Struts2" debug="5" reloadable="true" crossContext="true">
           <Resource name="jdbc/struts" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="user" password="pass" driverClassName="oracle.jdbc.driver.OracleDriver"
               url="jdbc:oracle:thin:@192.168.1.10:1521:orcl"/>
        </Context> 

红字部分根据自己服务器情况进行更改

在ROOT/web.xml文件中加上

 <resource-ref>
   <description>sqlserver Datasource example</description>
   <res-ref-name>JNDI/newoa</res-ref-name>
   <res-type>javax.sql.DataSource</res-type>
   <res-auth>Container</res-auth>
 </resource-ref>

 

测试连接池的程序:

 

<%@page contentType="text/html; charset=gb2312" import="java.sql.*"%>
<%@page import="javax.naming.Context" %>
<%@page import="javax.sql.DataSource"%>
<%@ page import="javax.naming.InitialContext"%>
<%@ page import="java.sql.*"%>
<%  
DataSource ds=null;
Connection  cn=null;
try{
   Context initCtx = new InitialContext();
   Context envCtx=(Context)initCtx.lookup("java:comp/env");
   ds=(DataSource)envCtx.lookup("jdbc/newoa");
   if(ds!=null)
   {
     cn=ds.getConnection();
     if(cn!=null)
     {
       
        Statement stmt = cn.createStatement();
        ResultSet rst = stmt.executeQuery("select * from depart");
        while(rst.next())
         {
            out.println(rst.getInt(1));
         }
      }
    cn.close(); 
    }
   }catch(Exception e)
   {
     out.println(e);
   }          
%>

注意在不同的tomcat版本下配置连接池会有差异。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值