Search code examples
javaoaf

SQL exception occurs when trying to close the connection in finally clause


I have some OAF code inside that i am initiating sql connection but when i am trying to close it , it throws sql exception . here is the code.

    try
    {
        conn = 
        (OracleConnection)oadbtransactionimpl.getJdbcConnection();
        String queryForEmpty = 
            "select ... query here"; 
        projectDetailsStatment = 
                conn.prepareStatement(queryForEmpty);
        projectDetailsStatment.setString(1,sprojectid);
        ResultSet rs = projectDetailsStatment.executeQuery();

    }
    catch(SQLException e)
   {
       String sqlErrMsg = e.getMessage();
       throw new OAException((new StringBuilder()).append("handle exception here:").append(sqlErrMsg).toString(), (byte)0);
   }
   finally
   {
      conn.close(); // throws exception here
   }

Solution

  • Try enclose the closing code with try and catch clause

        try
        {
            conn = 
            (OracleConnection)oadbtransactionimpl.getJdbcConnection();
            String queryForEmpty = 
                "select ... query here"; 
            projectDetailsStatment = 
                    conn.prepareStatement(queryForEmpty);
            projectDetailsStatment.setString(1,sprojectid);
            ResultSet rs = projectDetailsStatment.executeQuery();
    
        }
        catch(SQLException e)
       {
           String sqlErrMsg = e.getMessage();
           throw new OAException((new StringBuilder()).append("Exception in Finding Project Type:").append(sqlErrMsg).toString(), (byte)0);
       }
       finally
       {
          try{
          conn.close(); // throws exception here
          }
            catch(SQLException e)
          {
            //your code here
          }
       }