I hope it is not posted twice. I wrote this mail in forenoon and when tried
to send internet connection lost. I am retyping everything.

    I have seen DBCP not closing a connection if the request is forwarded to
same page in the try block before the request is forwarded. With
sendRedirect no problem. This occurs when the connection is obtained through
JNDI look up datasource. I tested the same code in stand alone java
application using manual pooling which works fine. I am using Tomcat 4.1.27
and DBCP 1.1 (I upgraded it today), JDK 1.3.1 and Oracle 8i.
  My configuration and code is given below.
----------------------------------------------------------------------------
-----------------------
<Context path="/test" docBase="test"
        debug="0" reloadable="true" crossContext="true">

  <Logger className="org.apache.catalina.logger.FileLogger"
             prefix="test_log." suffix=".txt"
             timestamp="true"/>

  <Resource name="jdbc/Test"
               auth="Container"
               type="javax.sql.DataSource"/>

  <ResourceParams name="jdbc/Test">
    <parameter>
      <name>factory</name>
      <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
    </parameter>

 <parameter>
      <name>maxActive</name>
      <value>1</value>
    </parameter>

    <parameter>
      <name>maxIdle</name>
      <value>1</value>
    </parameter>

    <parameter>
      <name>maxWait</name>
      <value>100000</value>
    </parameter>

    <parameter>
     <name>username</name>
     <value>scott</value>
    </parameter>
    <parameter>
     <name>password</name>
     <value>tiger</value>
    </parameter>

    <parameter>
       <name>driverClassName</name>
       <value>oracle.jdbc.driver.OracleDriver</value>
    </parameter>

 <parameter>
      <name>url</name>
      <value>jdbc:oracle:thin:@localhost:1521:DB</value>
    </parameter>

 <parameter>
  <name>removeAbandoned</name>
  <value>true</value>
 </parameter>
 <parameter>
  <name>removeAbandonedTimeout </name>
  <value>30</value>
 </parameter>
  </ResourceParams>
</Context>

----------------------------------------------------------------------------
----------------------
<html>
<body>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page import="org.apache.commons.dbcp.datasources.SharedPoolDataSource"%>
<%@ page import="org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS"%>
<%
/*DriverAdapterCPDS cpds = new DriverAdapterCPDS();
cpds.setDriver("oracle.jdbc.driver.OracleDriver");
cpds.setUrl("jdbc:oracle:thin:@localhost:1521:DB");
cpds.setUser("scott");
cpds.setPassword("tiger");

SharedPoolDataSource tds = new SharedPoolDataSource();
tds.setConnectionPoolDataSource(cpds);
tds.setMaxActive(1);
tds.setMaxIdle(1);
tds.setMaxWait(3000); */

Context initContext = new InitialContext();
Context envContext  = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)initContext.lookup("java:/comp/env/jdbc/Test");

PreparedStatement pstmt = null;
ResultSet rs = null;
Connection conn = null;

String action = request.getParameter("action");
if("1".equals(action)){
 long time = 0;

 try{
  conn = tds.getConnection();
  conn = ds.getConnection();
  pstmt = conn.prepareStatement("select empno,ename from scott.emp where
empno=?");
  pstmt.setString(1,"7369");
  rs = pstmt.executeQuery();
  while(rs.next()){
   System.out.println("Ename - "+rs.getString(1)+" - "+rs.getString(2));
  }
  time = System.currentTimeMillis();
  System.out.println("Before forward - 0");time=System.currentTimeMillis();
  application.getRequestDispatcher("/TestDB.jsp?action=2").forward(request,r
esponse);
  System.out.println("After forward - "+(System.currentTimeMillis()-time));

 }catch(SQLException sqle){

  System.out.println("1 " +sqle);
 }finally{
  if(rs!=null)rs.close();
  if(pstmt!=null) pstmt.close();
  if(conn!=null)conn.close();
 }
}else if("2".equals(action)){
 long time = 0;
 System.out.println("In Two start - time =
0");time=System.currentTimeMillis();
 try{
  conn = tds.getConnection();
  conn = ds.getConnection();
  pstmt = conn.prepareStatement("select empno,ename from scott.emp where
empno=?");
  pstmt.setString(1,"7499");
  rs = pstmt.executeQuery();
  while(rs.next()){
   System.out.println("Ename - "+rs.getString(1)+" - "+rs.getString(2));
  }
 }catch(SQLException sqle){

  System.out.println("2 "+sqle);
 }finally{
  if(rs!=null)rs.close();
  if(pstmt!=null) pstmt.close();
  if(conn!=null)conn.close();
 }
 System.out.println("In Two end - "+(System.currentTimeMillis()-time));
}
%>


I work with one connection to test for connection leak and any bottlenecks
in code. It works fine if I put the forward() after the end of finally
block. I want to know whether it is bug or config error or Tomcat behaves
so.

Antony Paul.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to