Program: 105265
Project: 105265
Buggy source:
private void setupPhysicalNetworks(Connection conn){
  PreparedStatement pstmt=null;
  ResultSet rs=null;
  List<PreparedStatement> pstmt2Close=new ArrayList<PreparedStatement>();
  PreparedStatement pstmtUpdate=null;
  try {
    String xenPublicLabel=getNetworkLabelFromConfig(conn,"String_Node_Str");
    String xenPrivateLabel=getNetworkLabelFromConfig(conn,"String_Node_Str");
    String xenStorageLabel=getNetworkLabelFromConfig(conn,"String_Node_Str");
    String xenGuestLabel=getNetworkLabelFromConfig(conn,"String_Node_Str");
    String kvmPublicLabel=getNetworkLabelFromConfig(conn,"String_Node_Str");
    String kvmPrivateLabel=getNetworkLabelFromConfig(conn,"String_Node_Str");
    String kvmGuestLabel=getNetworkLabelFromConfig(conn,"String_Node_Str");
    String vmwarePublicLabel=getNetworkLabelFromConfig(conn,"String_Node_Str");
    String vmwarePrivateLabel=getNetworkLabelFromConfig(conn,"String_Node_Str");
    String vmwareGuestLabel=getNetworkLabelFromConfig(conn,"String_Node_Str");
    pstmt=conn.prepareStatement("String_Node_Str");
    pstmt2Close.add(pstmt);
    rs=pstmt.executeQuery();
    while (rs.next()) {
      long zoneId=rs.getLong(1);
      Long domainId=rs.getLong(2);
      String networkType=rs.getString(3);
      String vnet=rs.getString(4);
      String zoneName=rs.getString(5);
      String removed=rs.getString(6);
      String uuid=UUID.randomUUID().toString();
      String updateUuid="String_Node_Str";
      pstmtUpdate=conn.prepareStatement(updateUuid);
      pstmtUpdate.setString(1,uuid);
      pstmtUpdate.setLong(2,zoneId);
      pstmtUpdate.executeUpdate();
      pstmtUpdate.close();
      boolean crtPbNtwk=false;
      pstmt=conn.prepareStatement("String_Node_Str");
      pstmt2Close.add(pstmt);
      pstmt.setLong(1,zoneId);
      ResultSet rs1=pstmt.executeQuery();
      if (rs1.next()) {
        crtPbNtwk=true;
      }
      PreparedStatement pstmt2=conn.prepareStatement("String_Node_Str");
      pstmt2Close.add(pstmt2);
      pstmt2.setLong(1,zoneId);
      ResultSet rsTags=pstmt2.executeQuery();
      if (rsTags.next()) {
        s_logger.debug("String_Node_Str");
        if (vnet != null) {
          PreparedStatement pstmt4=conn.prepareStatement("String_Node_Str" + "String_Node_Str");
          pstmt2Close.add(pstmt4);
          pstmt4.setLong(1,zoneId);
          ResultSet rsVNet=pstmt4.executeQuery();
          if (rsVNet.next()) {
            String message="String_Node_Str" + "String_Node_Str" + "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str";
            s_logger.error(message);
            throw new CloudRuntimeException("String_Node_Str");
          }
          rsVNet.close();
          pstmt4=conn.prepareStatement("String_Node_Str");
          pstmt2Close.add(pstmt4);
          pstmt4.setLong(1,zoneId);
          rsVNet=pstmt4.executeQuery();
          while (rsVNet.next()) {
            Long vnet_id=rsVNet.getLong(1);
            String vnetValue=rsVNet.getString(2);
            String reservationId=rsVNet.getString(3);
            PreparedStatement pstmt5=conn.prepareStatement("String_Node_Str");
            pstmt2Close.add(pstmt5);
            String uri="String_Node_Str" + vnetValue;
            pstmt5.setString(1,uri);
            ResultSet rsNic=pstmt5.executeQuery();
            Long nic_id=rsNic.getLong(1);
            Long instance_id=rsNic.getLong(2);
            if (rsNic.next()) {
              throw new CloudRuntimeException("String_Node_Str" + vnetValue + "String_Node_Str"+ nic_id+ "String_Node_Str"+ instance_id);
            }
            String freeVnet="String_Node_Str";
            pstmtUpdate=conn.prepareStatement(freeVnet);
            pstmtUpdate.setLong(1,vnet_id);
            pstmtUpdate.executeUpdate();
            pstmtUpdate.close();
          }
        }
        boolean isFirstPhysicalNtwk=true;
        do {
          String guestNetworkTag=rsTags.getString(1);
          long physicalNetworkId=addPhysicalNetworkToZone(conn,zoneId,zoneName,networkType,(isFirstPhysicalNtwk) ? vnet : null,domainId);
          if (isFirstPhysicalNtwk) {
            if (crtPbNtwk) {
              addTrafficType(conn,physicalNetworkId,"String_Node_Str",xenPublicLabel,kvmPublicLabel,vmwarePublicLabel);
            }
 else {
              s_logger.debug("String_Node_Str" + zoneId);
            }
            addTrafficType(conn,physicalNetworkId,"String_Node_Str",xenPrivateLabel,kvmPrivateLabel,vmwarePrivateLabel);
            addTrafficType(conn,physicalNetworkId,"String_Node_Str",xenStorageLabel,null,null);
          }
          addTrafficType(conn,physicalNetworkId,"String_Node_Str",guestNetworkTag,kvmGuestLabel,vmwareGuestLabel);
          addDefaultVRProvider(conn,physicalNetworkId,zoneId);
          addDefaultSGProvider(conn,physicalNetworkId,zoneId,networkType,false);
          PreparedStatement pstmt3=conn.prepareStatement("String_Node_Str" + guestNetworkTag + "String_Node_Str");
          ResultSet rsNet=pstmt3.executeQuery();
          s_logger.debug("String_Node_Str");
          s_logger.debug("String_Node_Str");
          s_logger.debug("String_Node_Str");
          while (rsNet.next()) {
            Long networkId=rsNet.getLong(1);
            addPhysicalNtwk_To_Ntwk_IP_Vlan(conn,physicalNetworkId,networkId);
          }
          pstmt3.close();
          if (isFirstPhysicalNtwk) {
            s_logger.debug("String_Node_Str");
            pstmt3=conn.prepareStatement("String_Node_Str" + zoneId);
            ResultSet rsPubNet=pstmt3.executeQuery();
            if (rsPubNet.next()) {
              Long publicNetworkId=rsPubNet.getLong(1);
              addPhysicalNtwk_To_Ntwk_IP_Vlan(conn,physicalNetworkId,publicNetworkId);
            }
            pstmt3.close();
            s_logger.debug("String_Node_Str");
            String updateVnet="String_Node_Str" + physicalNetworkId + "String_Node_Str"+ zoneId;
            pstmtUpdate=conn.prepareStatement(updateVnet);
            pstmtUpdate.executeUpdate();
            pstmtUpdate.close();
          }
          isFirstPhysicalNtwk=false;
        }
 while (rsTags.next());
        pstmt2.close();
      }
 else {
        long physicalNetworkId=addPhysicalNetworkToZone(conn,zoneId,zoneName,networkType,vnet,domainId);
        if (crtPbNtwk) {
          addTrafficType(conn,physicalNetworkId,"String_Node_Str",xenPublicLabel,kvmPublicLabel,vmwarePublicLabel);
        }
 else {
          s_logger.debug("String_Node_Str" + zoneId);
        }
        addTrafficType(conn,physicalNetworkId,"String_Node_Str",xenPrivateLabel,kvmPrivateLabel,vmwarePrivateLabel);
        addTrafficType(conn,physicalNetworkId,"String_Node_Str",xenStorageLabel,null,null);
        addTrafficType(conn,physicalNetworkId,"String_Node_Str",xenGuestLabel,kvmGuestLabel,vmwareGuestLabel);
        addDefaultVRProvider(conn,physicalNetworkId,zoneId);
        addDefaultSGProvider(conn,physicalNetworkId,zoneId,networkType,false);
        s_logger.debug("String_Node_Str");
        String updateVnet="String_Node_Str" + physicalNetworkId + "String_Node_Str"+ zoneId;
        pstmtUpdate=conn.prepareStatement(updateVnet);
        pstmtUpdate.executeUpdate();
        pstmtUpdate.close();
        s_logger.debug("String_Node_Str");
        String updateVLAN="String_Node_Str" + physicalNetworkId + "String_Node_Str"+ zoneId;
        pstmtUpdate=conn.prepareStatement(updateVLAN);
        pstmtUpdate.executeUpdate();
        pstmtUpdate.close();
        s_logger.debug("String_Node_Str");
        String updateUsrIp="String_Node_Str" + physicalNetworkId + "String_Node_Str"+ zoneId;
        pstmtUpdate=conn.prepareStatement(updateUsrIp);
        pstmtUpdate.executeUpdate();
        pstmtUpdate.close();
        s_logger.debug("String_Node_Str");
        String updateNet="String_Node_Str" + physicalNetworkId + "String_Node_Str"+ zoneId+ "String_Node_Str";
        pstmtUpdate=conn.prepareStatement(updateNet);
        pstmtUpdate.executeUpdate();
        pstmtUpdate.close();
        if (removed != null) {
          pstmtUpdate=conn.prepareStatement("String_Node_Str");
          pstmtUpdate.setLong(1,physicalNetworkId);
          pstmtUpdate.executeUpdate();
          pstmtUpdate.close();
        }
      }
    }
  }
 catch (  SQLException e) {
    throw new CloudRuntimeException("String_Node_Str",e);
  }
 finally {
    TransactionLegacy.closePstmts(pstmt2Close);
  }
}

Fixed version (ground truth):
private void setupPhysicalNetworks(Connection conn){
  PreparedStatement pstmt=null;
  ResultSet rs=null;
  List<PreparedStatement> pstmt2Close=new ArrayList<PreparedStatement>();
  PreparedStatement pstmtUpdate=null;
  try {
    String xenPublicLabel=getNetworkLabelFromConfig(conn,"String_Node_Str");
    String xenPrivateLabel=getNetworkLabelFromConfig(conn,"String_Node_Str");
    String xenStorageLabel=getNetworkLabelFromConfig(conn,"String_Node_Str");
    String xenGuestLabel=getNetworkLabelFromConfig(conn,"String_Node_Str");
    String kvmPublicLabel=getNetworkLabelFromConfig(conn,"String_Node_Str");
    String kvmPrivateLabel=getNetworkLabelFromConfig(conn,"String_Node_Str");
    String kvmGuestLabel=getNetworkLabelFromConfig(conn,"String_Node_Str");
    String vmwarePublicLabel=getNetworkLabelFromConfig(conn,"String_Node_Str");
    String vmwarePrivateLabel=getNetworkLabelFromConfig(conn,"String_Node_Str");
    String vmwareGuestLabel=getNetworkLabelFromConfig(conn,"String_Node_Str");
    pstmt=conn.prepareStatement("String_Node_Str");
    pstmt2Close.add(pstmt);
    rs=pstmt.executeQuery();
    while (rs.next()) {
      long zoneId=rs.getLong(1);
      Long domainId=rs.getLong(2);
      String networkType=rs.getString(3);
      String vnet=rs.getString(4);
      String zoneName=rs.getString(5);
      String removed=rs.getString(6);
      String uuid=UUID.randomUUID().toString();
      String updateUuid="String_Node_Str";
      pstmtUpdate=conn.prepareStatement(updateUuid);
      pstmtUpdate.setString(1,uuid);
      pstmtUpdate.setLong(2,zoneId);
      pstmtUpdate.executeUpdate();
      pstmtUpdate.close();
      boolean crtPbNtwk=false;
      pstmt=conn.prepareStatement("String_Node_Str");
      pstmt2Close.add(pstmt);
      pstmt.setLong(1,zoneId);
      ResultSet rs1=pstmt.executeQuery();
      if (rs1.next()) {
        crtPbNtwk=true;
      }
      PreparedStatement pstmt2=conn.prepareStatement("String_Node_Str");
      pstmt2Close.add(pstmt2);
      pstmt2.setLong(1,zoneId);
      ResultSet rsTags=pstmt2.executeQuery();
      if (rsTags.next()) {
        s_logger.debug("String_Node_Str");
        if (vnet != null) {
          PreparedStatement pstmt4=conn.prepareStatement("String_Node_Str" + "String_Node_Str");
          pstmt2Close.add(pstmt4);
          pstmt4.setLong(1,zoneId);
          ResultSet rsVNet=pstmt4.executeQuery();
          if (rsVNet.next()) {
            String message="String_Node_Str" + "String_Node_Str" + "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str";
            s_logger.error(message);
            throw new CloudRuntimeException("String_Node_Str");
          }
          rsVNet.close();
          pstmt4=conn.prepareStatement("String_Node_Str");
          pstmt2Close.add(pstmt4);
          pstmt4.setLong(1,zoneId);
          rsVNet=pstmt4.executeQuery();
          while (rsVNet.next()) {
            Long vnet_id=rsVNet.getLong(1);
            String vnetValue=rsVNet.getString(2);
            String reservationId=rsVNet.getString(3);
            PreparedStatement pstmt5=conn.prepareStatement("String_Node_Str");
            pstmt2Close.add(pstmt5);
            String uri="String_Node_Str" + vnetValue;
            pstmt5.setString(1,uri);
            ResultSet rsNic=pstmt5.executeQuery();
            Long nic_id=rsNic.getLong(1);
            Long instance_id=rsNic.getLong(2);
            if (rsNic.next()) {
              throw new CloudRuntimeException("String_Node_Str" + vnetValue + "String_Node_Str"+ nic_id+ "String_Node_Str"+ instance_id);
            }
            String freeVnet="String_Node_Str";
            pstmtUpdate=conn.prepareStatement(freeVnet);
            pstmtUpdate.setLong(1,vnet_id);
            pstmtUpdate.executeUpdate();
            pstmtUpdate.close();
          }
        }
        boolean isFirstPhysicalNtwk=true;
        do {
          String guestNetworkTag=rsTags.getString(1);
          long physicalNetworkId=addPhysicalNetworkToZone(conn,zoneId,zoneName,networkType,(isFirstPhysicalNtwk) ? vnet : null,domainId);
          if (isFirstPhysicalNtwk) {
            if (crtPbNtwk) {
              addTrafficType(conn,physicalNetworkId,"String_Node_Str",xenPublicLabel,kvmPublicLabel,vmwarePublicLabel);
            }
 else {
              s_logger.debug("String_Node_Str" + zoneId);
            }
            addTrafficType(conn,physicalNetworkId,"String_Node_Str",xenPrivateLabel,kvmPrivateLabel,vmwarePrivateLabel);
            addTrafficType(conn,physicalNetworkId,"String_Node_Str",xenStorageLabel,null,null);
          }
          addTrafficType(conn,physicalNetworkId,"String_Node_Str",guestNetworkTag,kvmGuestLabel,vmwareGuestLabel);
          addDefaultVRProvider(conn,physicalNetworkId,zoneId);
          addDefaultSGProvider(conn,physicalNetworkId,zoneId,networkType,false);
          PreparedStatement pstmt3=conn.prepareStatement("String_Node_Str");
          pstmt3.setString(1,guestNetworkTag);
          ResultSet rsNet=pstmt3.executeQuery();
          s_logger.debug("String_Node_Str");
          s_logger.debug("String_Node_Str");
          s_logger.debug("String_Node_Str");
          while (rsNet.next()) {
            Long networkId=rsNet.getLong(1);
            addPhysicalNtwk_To_Ntwk_IP_Vlan(conn,physicalNetworkId,networkId);
          }
          pstmt3.close();
          if (isFirstPhysicalNtwk) {
            s_logger.debug("String_Node_Str");
            pstmt3=conn.prepareStatement("String_Node_Str" + zoneId);
            ResultSet rsPubNet=pstmt3.executeQuery();
            if (rsPubNet.next()) {
              Long publicNetworkId=rsPubNet.getLong(1);
              addPhysicalNtwk_To_Ntwk_IP_Vlan(conn,physicalNetworkId,publicNetworkId);
            }
            pstmt3.close();
            s_logger.debug("String_Node_Str");
            String updateVnet="String_Node_Str" + physicalNetworkId + "String_Node_Str"+ zoneId;
            pstmtUpdate=conn.prepareStatement(updateVnet);
            pstmtUpdate.executeUpdate();
            pstmtUpdate.close();
          }
          isFirstPhysicalNtwk=false;
        }
 while (rsTags.next());
        pstmt2.close();
      }
 else {
        long physicalNetworkId=addPhysicalNetworkToZone(conn,zoneId,zoneName,networkType,vnet,domainId);
        if (crtPbNtwk) {
          addTrafficType(conn,physicalNetworkId,"String_Node_Str",xenPublicLabel,kvmPublicLabel,vmwarePublicLabel);
        }
 else {
          s_logger.debug("String_Node_Str" + zoneId);
        }
        addTrafficType(conn,physicalNetworkId,"String_Node_Str",xenPrivateLabel,kvmPrivateLabel,vmwarePrivateLabel);
        addTrafficType(conn,physicalNetworkId,"String_Node_Str",xenStorageLabel,null,null);
        addTrafficType(conn,physicalNetworkId,"String_Node_Str",xenGuestLabel,kvmGuestLabel,vmwareGuestLabel);
        addDefaultVRProvider(conn,physicalNetworkId,zoneId);
        addDefaultSGProvider(conn,physicalNetworkId,zoneId,networkType,false);
        s_logger.debug("String_Node_Str");
        String updateVnet="String_Node_Str" + physicalNetworkId + "String_Node_Str"+ zoneId;
        pstmtUpdate=conn.prepareStatement(updateVnet);
        pstmtUpdate.executeUpdate();
        pstmtUpdate.close();
        s_logger.debug("String_Node_Str");
        String updateVLAN="String_Node_Str" + physicalNetworkId + "String_Node_Str"+ zoneId;
        pstmtUpdate=conn.prepareStatement(updateVLAN);
        pstmtUpdate.executeUpdate();
        pstmtUpdate.close();
        s_logger.debug("String_Node_Str");
        String updateUsrIp="String_Node_Str" + physicalNetworkId + "String_Node_Str"+ zoneId;
        pstmtUpdate=conn.prepareStatement(updateUsrIp);
        pstmtUpdate.executeUpdate();
        pstmtUpdate.close();
        s_logger.debug("String_Node_Str");
        String updateNet="String_Node_Str" + physicalNetworkId + "String_Node_Str"+ zoneId+ "String_Node_Str";
        pstmtUpdate=conn.prepareStatement(updateNet);
        pstmtUpdate.executeUpdate();
        pstmtUpdate.close();
        if (removed != null) {
          pstmtUpdate=conn.prepareStatement("String_Node_Str");
          pstmtUpdate.setLong(1,physicalNetworkId);
          pstmtUpdate.executeUpdate();
          pstmtUpdate.close();
        }
      }
    }
  }
 catch (  SQLException e) {
    throw new CloudRuntimeException("String_Node_Str",e);
  }
 finally {
    TransactionLegacy.closePstmts(pstmt2Close);
  }
}