Code like spencer's, in a utility class like the one Stacy mentioned - with each method doing a granular close operation is a Pattern I'v seen organizations eventually adopt. The Forces of memory leaks, and the desire for non repeating lengthy "cut and paste" code in Try Catch Blocks in each and every class that does database access - organizations will move to something like the below on their own over time.
DBUtil.closeResultSet(rs); DBUtil.closePreparedStatement(ps); DBUtil.closeConnection(connection); ( Each method's implementation is safe by doing the actual close in a Try Catch block within the method ). If you have connection and resultset closing code that is not as clean as that, it can make a nice refactoring project. --------------------------------------- Considering the Certified Java Programmer Exam? Get JCertify 5.0! Eckel, Baldwin, Green, and more http://www.enterprisedeveloper.com/jcertify --- "James A. N. Stauffer" <[EMAIL PROTECTED]> wrote: > If rs.close() throws an Exception then > prepStmt.close() and con.close() > won't be called. > > --- "Spencer W. Thomas" <[EMAIL PROTECTED]> wrote: > > The finally block should be executed, regardless > of whether an exception > > is thrown. Is it possible that you have a > connection, but that the > > variable "con" is null? Is it possible that there > is a place that you > > (or WebLogic?) are creating a connection that is > NOT included in a > > try...finally block? > > > > =Spencer > > > > Mark E. Zawadzki wrote: > > > > >Are you sure that a SQLException is not being > thrown, in some cases, > > w/in the > > >finally ? I see you do an empty catch. > > >--- Abhilash Nair <[EMAIL PROTECTED]> wrote: > > > > > > > > >>Hi All: > > >> > > >>We are using Oracle Thin Type 4 Driver with > Weblogic > > >>App Server version 5.1 for our JDBC Connections > and > > >>use Prepared Statements. We also have specified > the > > >>maximum connections to be 100 in the weblogic > > >>properties and in our DAO's we close the > > >>PreparedStatement, ResultSet and Connection in > the > > >>finally block. But there are STILL instances > where the > > >>connections are not being closed even when the > > >>following code is executed: > > >> > > >>try{ > > >> //create connection,etc. > > >>}catch(SQLException sqlExc){ > > >> //handle error > > >>}finally{ > > >> try { > > >> if(rs != null){ > > >> rs.close(); > > >> } > > >> if(prepStmt != null){ > > >> prepStmt.close(); > > >> } > > >> if(con != null){ > > >> con.close(); > > >> } > > >> } catch(SQLException e) {;} > > >>} > > >> > > >>Problem faced: Exceeding the maximum number of > > >>connections due to inactive open connections. > Has > > >>anyone faced this problem? > > >> > > >>I thought that the finally block is executed no > matter > > >>what and is the best place to close connections > to > > >>ensure a proper clean - up! Can anyone please > suggest > > >>what am I doing wrong here? > > >> > > >>Thanks in advance. > > >>Abhi > > >> > > > >>__________________________________________________ > > >>Do You Yahoo!? > > >>Yahoo! - Official partner of 2002 FIFA World Cup > > >>http://fifaworldcup.yahoo.com > > >> > > >>To change your membership options, refer to: > > >>http://www.sys-con.com/java/list.cfm > > >> > > >> > > > > > > > > >===== > > >Mark Zawadzki Performance Engineer/DBA/Programmer > > extraordinaire'[EMAIL PROTECTED] > [EMAIL PROTECTED]"Programming > > today is a race between software engineers > striving to build bigger and > > better idiot-proof programs, and the universe > trying to build bigger and > > better idiots. So far, the universe is > winningRobert Cringle (columnist, > > author, host of "Triumph of the Nerds") > > > > > > >__________________________________________________ > > >Do You Yahoo!? > > >Yahoo! - Official partner of 2002 FIFA World Cup > > >http://fifaworldcup.yahoo.com > > > > > >To change your membership options, refer to: > > >http://www.sys-con.com/java/list.cfm > > > > > > > > > > > > > > To change your membership options, refer to: > > http://www.sys-con.com/java/list.cfm > > > ===== > = o o o o o o o . . . __________________________ > _____=======_||___ > o _____ | James A. N. Stauffer | > | Stauffer_James | > .][__n_n_|DD[ ====____ | Spam food: [EMAIL PROTECTED] | > | @yahoo.com | > >(________|__|_[________]_|________________________|_|________________| > _/oo OOOOO oo` ooo ooo 'o¬o¬o o¬o¬o` > 'o¬o o¬o` > > __________________________________________________ > Do You Yahoo!? > Yahoo! - Official partner of 2002 FIFA World Cup > http://fifaworldcup.yahoo.com > > To change your membership options, refer to: > http://www.sys-con.com/java/list.cfm __________________________________________________ Do You Yahoo!? Yahoo! - Official partner of 2002 FIFA World Cup http://fifaworldcup.yahoo.com To change your membership options, refer to: http://www.sys-con.com/java/list.cfm