tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alan Chaney <a...@compulsivecreative.com>
Subject Re: Random Connection Closed Exceptions - Question to the code example
Date Mon, 19 Jan 2009 21:38:09 GMT
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-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message