About Closing the Conncetions in Finall Block

Discussions

EJB design: About Closing the Conncetions in Finall Block

  1. About Closing the Conncetions in Finall Block (4 messages)

    In EJB , For every method in the finally Block we will write the following code to close the conncetions

    finally {
    try {
    if (rs != null)
    rs.close();
    if (stmt != null)
    stmt.close();
    if (conn != null)
    conn.close();
    }
    catch (Exception ee) {
    throw new Exception(ee.getMessage());
    }
     if there are 10 methods and 10 EJBS then this code need to be repeatedly written for each method

     Instead of this I can go for a Util Class like this ......
     
    package util;

    import java.sql.Connection;
    import java.sql.Statement;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import javax.ejb.EJBException;

    public class ConnectionUtil
    {
    public static void closeConnection( Connection
               conn, Statement st, ResultSet rs)
    {
    boolean closeError = false;
    if( rs != null )
    {
    try
    {
    rs.close();
    rs = null;
    }
    catch( SQLException e )
    {
    e.printStackTrace(
                                     System.err );
    closeError = true;
    }
    }
    if( st != null )
    {
    try
    {
    st.close();
    st = null;
    }
    catch( SQLException e )
    {
    e.printStackTrace(
                                       System.err );
    closeError = true;
    }
    }
    if( conn != null )
    {
    try
    {
    conn.close();
    conn = null;
    }
    catch( SQLException e )
    {
    e.printStackTrace(
                                      System.err );
    closeError = true;
    }
    }
    if( closeError )
    {
    throw new EJBException( "Exceptions trying to close connection!" );
    }
    }// end of closeConnection( Connection conn, Statement st )


    private ConnectionUtil()
    {

    }
    }

    Jsut call this class in the finally block of each method in EJB
    finally
         {
                ConnectionUtil.closeStatement(conn,stmt,rs);
         }


    But I came to know that there is a pitfall in this approach. The class fails during a race condition.
    To overcome that we need to synchronize the functions which is more costly .

    Do let us know the best approach.

    Regards
    B S Reddy
  2. Why does it fail? What's the exception?
  3. Race condition here is nothing but the synchronization problem. This will happen when the mutual exclusive access to that EJB/Method .

    Exactly I dont know the Exception but it may fail itseems.
  4. Exactly I dont know the Exception but it may fail itseems.

    Don't fix it if it's not broken... ;)

    Cheers,
    Martin
  5. Agreed, don't mess with what works[ Go to top ]

    Exactly I dont know the Exception but it may fail itseems.
    Don't fix it if it's not broken... ;)Cheers,Martin

    I do it all in the finally block too and I've never had a problem.

    The code is a bit redundant, but you can control that too by making very general, powerful methods to segregate sql from specific transactions via servlet, controller or whatever.