2 Replies Latest reply on Jul 6, 2015 7:14 PM by SvenBatalla

    How can I rollback a tag creation using AFSDK?

    SvenBatalla

      We currently have an application that allows users to create new elements in PIAF.  Naturally, this is done using the AFSDK.  During the process, if an exception occurs, the element can be rolled back by simply undoing the checkout.  However, if tags were created in PI during the process (and this is still done using the AFSDK), the tags themselves are not removed.  So the question is:  how can I "undo" the tag creation?

       

      Here is some sample code to show what we're talking about (shortened for brevity and clarity):

       

      try
      {
         // Create the new element
         AFElement element = new AFElement("My Element", db.ElementTemplates["My Template"]);
      
         // Create the needed PI tag attribute
         element.Attributes["My Attribute"].DataReferencePlugin = AFDataReference.GetPIPointDataReference(pisystem);
         element.Attributes["My Attribute"].DataReference.ConfigString = "...";
         element.Attributes["My Attribute"].DataReference.CreateConfig();
      
         // Commit the work
         // - At this point, all is well.  The element is created and the tag is created too.
         db.CheckIn();
      }
      catch(Exception ex)
      {
         // An error has occurred, so undo the element creation and let the error bubble up to the next layer
         // - At this point, the element rollback occurs, but the tag remains in existence
         db.UndoCheckOut(true);
         throw;
      }
      

       

      There is obviously code that we could write to maintain the list of tags we've created up to that point and then manually delete them within the catch, but this seems like extra effort that I shouldn't need to do.  If I have to, that's fine, but with the element rollback possible, I hoped something similar would exist for PI.  Can anyone help with this?

        • Re: How can I rollback a tag creation using AFSDK?
          dng

          Hi Sven,


          I don't think there is a simple "rollback" operation for tag creation already completed on the PI Data Archive. In the PI AF server, changes made to checked out elements are recorded in a sandbox, and only get committed to the database during a check in. However, on the other hand, once a command is initiated to create tags in the PI Data Archive, the tags are already created.

           

          My suggestion would be to first check in your changes to the element (during element creation) to make sure all goes well. If there is no exception, you can then call AFDataReference.CreateConfig to create the PI tags. Treat them as separate operations so you can avoid tag creation if things go wrong during the AF element creation.