-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Josh,

On 10/30/2009 4:31 PM, Josh Gooding wrote:
> public static void closeResources(ResultSet rs) throws
> AardvarkResourceException {
>         Statement s = null;
>         Connection c = null;
> 
>         try {
>             s = rs.getStatement();
>             if (s != null) {
>                 c = s.getConnection();
>             }
>             rs.close();
>         }
>         catch (SQLException e) {
>             throw new AardvarkResourceException("Error closing resources
> associated with ResultSet", e);
>         }
>         finally
>         {
>             if(null != rs) try { rs.close(); }
>                 catch (SQLException sqle)
>                 { sqle.printStackTrace(); }
> 
>             if(null != s) try { s.close(); } catch (SQLException sqle)
>                 { sqle.printStackTrace(); }
> 
>             if(null != c)
>                 try { c.close(); } catch (SQLException sqle)
>                 { sqle.printStackTrace(); }
>         }
>     }

I think the finally block does everything you want. You don't really
even need the try portion... I would further simplify it to:

public static void closeResources(ResultSet rs)
  // No 'throws' clause... no reason to throw an exception
  // here because there's nothing you can do about it: just log it.
{
   Statement s = null;
   Connection conn = null;
   try
   {
      s = rs.getStatement();
   }
   catch (SQLException sqle)
   {
      sqle.printStackTrace();
   }

   if(null != s)
   {
      try
      {
         c = s.getConnection();
      }
      catch (SQLException sqle)
      {
         sqle.printStackTrace();
      }
   }

   if(null != rs) try { rs.close(); }
       catch (SQLException sqle)
       { sqle.printStackTrace(); }

   if(null != s) try { s.close(); } catch (SQLException sqle)
       { sqle.printStackTrace(); }

   if(null != c)
       try { c.close(); } catch (SQLException sqle)
       { sqle.printStackTrace(); }
}

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkrrV1EACgkQ9CaO5/Lv0PDhpQCfSABY67rM0YjoEjXn5H/jT1tQ
ZJIAoLfPGl3Ewk90y6E11sglZkSDaAWH
=Lcer
-----END PGP SIGNATURE-----

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

Reply via email to