glenn 2002/08/30 06:41:25 Modified: webapps/tomcat-docs jndi-datasource-examples-howto.xml Log: Add FAQ for Random Closed Connection Exceptions Revision Changes Path 1.5 +67 -0 jakarta-tomcat-4.0/webapps/tomcat-docs/jndi-datasource-examples-howto.xml Index: jndi-datasource-examples-howto.xml =================================================================== RCS file: /home/cvs/jakarta-tomcat-4.0/webapps/tomcat-docs/jndi-datasource-examples-howto.xml,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- jndi-datasource-examples-howto.xml 18 Aug 2002 00:55:25 -0000 1.4 +++ jndi-datasource-examples-howto.xml 30 Aug 2002 13:41:25 -0000 1.5 @@ -749,6 +749,73 @@ </subsection> +<subsection name="Random Connection Closed Exceptions"> +<p> +These can occur when one request gets a db connection from the connection +pool and closes it twice. When using a connection pool, closing the +connection just returns it to the pool for reuse by another request, +it doesn't close the connection. And Tomcat uses multiple threads to +handle concurrent requests. Here is an example of the sequence +of events which could cause this error in Tomcat: +<pre> + Request 1 running in Thread 1 gets a db connection. + + Request 1 closes the db connection. + + The JVM switches the running thread to Thread 2 + + Request 2 running in Thread 2 gets a db connection + (the same db connection just closed by Request 1). + + The JVM switches the running thread back to Thread 1 + + Request 1 closes the db connection a second time in a finally block. + + The JVM switches the running thread back to Thread 2 + + Request 2 Thread 2 tries to use the db connection but fails + because Request 1 closed it. +</pre> +Here is an example of properly written code to use a db connection +obtained from a connection pool: +<pre> + Connection conn = null; + Statement stmt = null; // Or PreparedStatement if needed + ResultSet rs = null; + try { + conn = ... get connection from connection pool ... + stmt = conn.createStatement("select ..."); + rs = stmt.executeQuery(); + ... iterate through the result set ... + rs.close(); + rs = null; + stmt.close(); + stmt = null; + conn.close(); // Return to connection pool + conn = null; // Make sure we don't close it twice + } catch (SQLException e) { + ... deal with errors ... + } finally { + // Always make sure result sets and statements are closed, + // and the connection is returned to the pool + if (rs != null) { + try { rs.close(); } catch (SQLException e) { ; } + rs = null; + } + if (stmt != null) { + try { stmt.close(); } catch (SQLException e) { ; } + stmt = null; + } + if (conn != null) { + try { conn.close(); } catch (SQLException e) { ; } + conn = null; + } + } +</pre> +</p> + +</subsection> + </section> </body>
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>