Thanks Alan, just to make the thing really clear. You propose code like this:

        public void execute() {
                Connection conn = null;
                Statement stmt = null;
                ResultSet rs = null;
                Context envContext = null;
                try {
                        Context initContext = new InitialContext();
                        envContext = (Context) 
initContext.lookup("java:/comp/env");
                        DataSource ds = (DataSource) 
envContext.lookup("jdbc/swex");
                        conn = ds.getConnection();
                        stmt = conn.createStatement();
                        rs = stmt.executeQuery("some sql");
                    // iterate through the result set ...
                } catch (SQLException e) {
                        e.printStackTrace();
                } catch (NamingException e) {
                        e.printStackTrace();
                } finally {
                        if (rs != null) {
                                try {
                                        rs.close();
                                } catch (SQLException e) {
                                        e.printStackTrace();
                                }
                        }
                        if (stmt != null) {
                                try {
                                        stmt.close();
                                } catch (SQLException e) {
                                        e.printStackTrace();
                                }
                        }
                        if (conn != null) {
                                try {
                                        conn.close();
                                } catch (SQLException e) {
                                        e.printStackTrace();
                                }
                        }
                        if (envContext != null) {
                                try {
                                        envContext.close();
                                } catch (NamingException e) {
                                        e.printStackTrace();
                                }
                        }
                }


For me this looks fine but I'm still confused, why they complicated things in the example of "properly written code" http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html?

Do I miss some important point here?

Stefan


Alan Chaney schrieb:
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


--
Stefan

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

Reply via email to