RE: Severity less - DBCP not closing connection.
You must ensure that your connection manager is closing all ResultSets, Statements, PreparedStatement and finally all connection. With that you DBCP will work. Store a copy of all those objets in Vectors and when you close the connectionm manager, it will close all objetcts stored also. > -- > De: Antony Paul[SMTP:[EMAIL PROTECTED] > Responder:Tomcat Users List > Enviada: sexta-feira, 21 de novembro de 2003 8:34 > Para: Tomcat Users List > Assunto: Severity less - DBCP not closing connection. > > 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. > -- > -- > --- > debug="0" reloadable="true" crossContext="true"> > > prefix="test_log." suffix=".txt" > timestamp="true"/> > > auth="Container" >type="javax.sql.DataSource"/> > > > > factory > org.apache.commons.dbcp.BasicDataSourceFactory > > > > maxActive > 1 > > > > maxIdle > 1 > > > > maxWait > 10 > > > > username > scott > > > password > tiger > > > >driverClassName >oracle.jdbc.driver.OracleDriver > > > > url > jdbc:oracle:thin:@localhost:1521:DB > > > > removeAbandoned > true > > > removeAbandonedTimeout > 30 > > > > > -- > -- > -- > > > <%@ 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. > > A
Re: Severity less - DBCP not closing connection.
I tested it using a sample code which is posted. All resultsets,prepared statements and connections are closed in finally block. I dont have a real problem yet. The app is still in developing stage. Antony Paul - Original Message - From: "Edson Alves Pereira" <[EMAIL PROTECTED]> To: "'Tomcat Users List'" <[EMAIL PROTECTED]> Sent: Friday, November 21, 2003 7:21 PM Subject: RE: Severity less - DBCP not closing connection. > You must ensure that your connection manager is closing all > ResultSets, Statements, PreparedStatement and finally all connection. With > that you DBCP will work. Store a copy of all those objets in Vectors and > when you close the connectionm manager, it will close all objetcts stored > also. > > > -- > > De: Antony Paul[SMTP:[EMAIL PROTECTED] > > Responder: Tomcat Users List > > Enviada: sexta-feira, 21 de novembro de 2003 8:34 > > Para: Tomcat Users List > > Assunto: Severity less - DBCP not closing connection. > > > > 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. > > -- > > -- > > --- > > > debug="0" reloadable="true" crossContext="true"> > > > >> prefix="test_log." suffix=".txt" > > timestamp="true"/> > > > >>auth="Container" > >type="javax.sql.DataSource"/> > > > > > > > > factory > > org.apache.commons.dbcp.BasicDataSourceFactory > > > > > > > > maxActive > > 1 > > > > > > > > maxIdle > > 1 > > > > > > > > maxWait > > 10 > > > > > > > > username > > scott > > > > > > password > > tiger > > > > > > > >driverClassName > >oracle.jdbc.driver.OracleDriver > > > > > > > > url > > jdbc:oracle:thin:@localhost:1521:DB > > > > > > > > removeAbandoned > > true > > > > > > removeAbandonedTimeout > > 30 > > > > > > > > > > -- > > -- > > -- > > > > > > <%@ 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
Re: Severity less - DBCP not closing connection.
Antony, 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. If you execute a "forward", then it acts just like a method call -- your connection will still be absent from the pool. When you forward, you exit your try/catch block before the browser makes the next request. You have to do your code like this: try { conn = ...; } catch (...) { } finally { conn.close(); } requestDispatcher.forward(...); Otherwise you might deadlock your application. I work with one connection to test for connection leak and any bottlenecks in code. This is a very good idea. > 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. This is a logic error - not a Tomcat bug or config error. You just have to write more careful code. -chris - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Severity less - DBCP not closing connection.
Thanks a lot for the info. I need more explanation. I thought that Tomcat creates a new thread to serve the new request and continues execution of the current servlet thread after a forward() or redirect(). Now you mean that only after the forward() method is completed the control returns to the calling servlet.. If the servlet to which it is forwareded takes lot of time to complete then what will happen ?. This is not mentioned in any articles or books that I have read. Antony Paul. - Original Message - From: "Christopher Schultz" <[EMAIL PROTECTED]> To: "Tomcat Users List" <[EMAIL PROTECTED]> Sent: Friday, November 21, 2003 7:12 PM Subject: Re: Severity less - DBCP not closing connection. > Antony, > > 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. > > If you execute a "forward", then it acts just like a method call -- your > connection will still be absent from the pool. When you forward, you > exit your try/catch block before the browser makes the next request. > > You have to do your code like this: > > try > { > conn = ...; > } > catch (...) > { > } > finally > { > conn.close(); > } > > requestDispatcher.forward(...); > > > Otherwise you might deadlock your application. > > > I work with one connection to test for connection leak and any bottlenecks > > in code. > > This is a very good idea. > > > 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. > > This is a logic error - not a Tomcat bug or config error. You just have > to write more careful code. > > -chris > > > - > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]