Hi Stefan

You don't need to repeat the stmt.close();conn.close() etc in the 'try' body. The 'finally' by definition is ALWAYS called and that is where you should do the tidy up...

Alan Chaney



Stefan Riegel wrote:
I guess I understood the point with the "Random Connection Closed Exceptions" Problem.

See at the end of http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html

As I understand, only the connection itself must be protected this way. The statement and ResultSet must not. Is the following, simplified code also correct?

  Connection conn = null;
  Statement stmt = null;
  ResultSet rs = null;
  try {
    conn = ... get connection from connection pool ...
    stmt = conn.createStatement("select ...");
    rs = stmt.executeQuery();
    ... iterate through the result set ...

// SUPERFLUsOUS
    rs.close();
    stmt.close();
    conn.close(); // Return to connection pool
    conn = null;  // Make sure we don't close it twice
//SUPERFLUOUS


  } catch (SQLException e) {
    ... deal with errors ...
  } finally {
          try {
        rs.close();
      } catch (SQLException e) {
        // deal with errors
      }
          try {
        stmt.close();
      } catch (SQLException e) {
        // deal with errors
      }
        if (conn != null) {
              try {
            conn.close();
        } catch (SQLException e) {
            // deal with errors
        }
              conn = null;
        }
  }

Thanks.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to