Hmm, this example code should get added to the Tomcat JNDI-DataSource-HOWTO. :-)
Craig R. McClanahan wrote: > > On Tue, 27 Aug 2002, Short, Dave wrote: > > >>Date: Tue, 27 Aug 2002 09:08:58 -0700 >>From: "Short, Dave" <[EMAIL PROTECTED]> >>Reply-To: Tomcat Users List <[EMAIL PROTECTED]> >>To: 'Tomcat Users List' <[EMAIL PROTECTED]> >>Subject: RE: Does closing a Connection variable and setting it to null >> clo se all of the ResultSet and Statements? >> >>By closing you mean set the ResultSet and Statement objects to null - >>correct? >> > > > No ... explicitly call close() on them first. My most common pattern for > JDBC calls goes like this: > > 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; > } catch (SQLException e) { > ... deal with errors ... > } finally { > 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; > } > } > > This way, you always clean up after yourself as quickly as possible, and > never forget to return the connection to the connection pool -- even if > exceptions occur. > > Craig > > > -- > To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> > For additional commands, e-mail: <mailto:[EMAIL PROTECTED]> -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>