简体   繁体   中英

Where is the bug in this code?

In the following code when if condition fails ie when else block is reached the procedure runs successfully else throws Closed Statement Exception:

 public synchronized void create(Map formMap) throws SQLException, Exception
  { 
    CallableStatement callStmt=null;    
    try
    {    
      String sql= "BEGIN UP_I_51(?, ?, ?, ?, ?, ?, ?, ?, ?); END;";   
      callStmt=getConnection().prepareCall(sql);
      callStmt.setString(1,formMap.get("periodFrom").toString());   
      callStmt.setString(2, formMap.get("periodTo").toString());
      String srcType=formMap.get("srcType").toString();
      callStmt.setString(3, srcType);
      callStmt.setString(4, formMap.get("collabIdMod").toString());
      callStmt.setString(5, formMap.get("name").toString());
      callStmt.setString(6, formMap.get("address").toString());
      callStmt.setString(7, " ");
      String parCollabId=formMap.get("pCollabId").toString();
      if(StringUtils.exists(parCollabId))
      {
        callStmt.setString(8, parCollabId);
        String parSrcType=getParentSrcType(srcType);
        callStmt.setString(9, parSrcType);
      }
      else
      {
        callStmt.setString(8, " ");
        callStmt.setString(9, " ");
      }   
      callStmt.executeUpdate(); 
    }
    finally
    {     
      callStmt.close();       
    }
  }

I replaced the code with below code and it is working now. I have used conditions when setting strings before but don't know why exception comes now.

I still don't understand why this exception comes. Could anyone find it?

 public synchronized void create(Map formMap) throws SQLException, Exception
  { 
        CallableStatement callStmt=null;    
        String srcType=formMap.get("srcType").toString();

        String parCollabId=formMap.get("pCollabId").toString();
        String parSrcType=getParentSrcType(srcType);
        try
        {    
            srcType=formMap.get("srcType").toString();
            parCollabId=formMap.get("pCollabId").toString();
            parSrcType=getParentSrcType(srcType);

            parCollabId = StringUtils.exists(parCollabId) ? parCollabId : " ";
            parSrcType = StringUtils.exists(parCollabId) ? parSrcType : " ";

            String sql= "{ call UP_I_51(?, ?, ?, ?, ?, ?, ?, ?, ?) }";  

          callStmt=getConnection().prepareCall(sql);
          callStmt.setString(1,formMap.get("periodFrom").toString());   
          callStmt.setString(2, formMap.get("periodTo").toString());

          callStmt.setString(3, srcType);
          callStmt.setString(4, formMap.get("collabIdMod").toString());
          callStmt.setString(5, formMap.get("name").toString());
          callStmt.setString(6, formMap.get("address").toString());
          callStmt.setString(7, " ");

          callStmt.setString(8, parCollabId);
          callStmt.setString(9, parSrcType);
          callStmt.executeUpdate(); 
        }catch(Exception e)
        {
            e.printStackTrace();
            throw e;
        }
        finally
        {     
          callStmt.close();       
        }
  }

There is so much missing it is hard to tell how you are using this, but, if you are closing the connection, so getConnection() returns something that isn't open, that could be your problem.

You should write a unit test, and call it two or three times in a row, from within the same test, to ensure that it is working properly.

If it doesn't you may want to have getConnection() ensure that the connection is open before returning the connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM