Create convenience methods for your SQL transaction closes:

public class DBUtil {
    private DBUtil() {
    }
    public static void closeResultSetIgnoreException(ResultSet rs) {
        try {
             if ( rs != null ) rs.close();
        } catch (SQLException e ) { // ignore   }
    }
    public static void closeStatementIgnoreException(Statement stmt) {
        try {
             if ( stmt != null ) stmt.close();
        } catch (SQLException e ) { // ignore   }
    }
    public static void closeConnectionIgnoreException(Connection conn) {
        try {
             if ( conn != null ) conn.close();
        } catch (SQLException e ) { // ignore   }
    }
    .
    .
    .
}

and in your code you would have:

try {
    // some sql transaction
} catch (SQLException e)
    // handle exception
} finally {
     DBUtil.closeResultSetIgnoreException(rs);
     DBUtil.closeStatementIgnoreException(Statement stmt);
     DBUtil.closeConnectionIgnoreException(Connection conn);
}

I hope this helps.



"James A. N. Stauffer" <[EMAIL PROTECTED]>

06/07/2002 11:08 AM
Please respond to "JDJList"

       
        To:        "JDJList" <[EMAIL PROTECTED]>
        cc:        
        Subject:        [jdjlist] Re: Closing Connections!!??


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


To change your membership options, refer to: http://www.sys-con.com/java/list.cfm

Reply via email to