Please ensure Javascript is enabled for purposes of website accessibility
Powered by Zoomin Software. For more details please contactZoomin

AVEVA Enterprise SCADA High Performance Database (HPDB) API Reference

Convert a lock on a record with a parent-child relationship

Convert a lock on a record with a parent-child relationship

Advanced knowledge is required to properly perform this scenario. Use caution and ensure you know how to use these functions properly. For more information, see Use AttachInsteadOfLock and DetachInsteadOfUnlock methods.

This scenario guides you through the steps required to convert a lock on a record with a parent-child relationship.

Typically, a parent-child relationship is created when you want both records locked at the same time because both records are going to be used during a read-and-write operation. This capability enables you to switch between the read-and-write modes of both records safely, without unlocking either of them.

For more information on records with parent-child relationships, see Read records with a parent-child relationship.

To identify the code associated with each step, see the code comments in the Example code section.

To convert a lock on a record with a parent-child relationship

  1. Open the Program.cs file from the created project.
  2. Open the RealTime databse (RTDB).
  3. Lock the child table for reading.
  4. Locate the child record.
  5. Lock the child record for reserved read and write.
    This lock type also locks the parent record.
  6. Locate the parent record.
  7. Read the parent field that links both tables (slot number).
  8. Attach to the parent record instead of lock for reading by using the AttachInsteadOfLock method. 
  9. Read a field from the parent record.
  10. Detach the parent record instead of unlock by using the DetachInsteadOfUnlock method.
  11. Switch to write.
  12. Attach to the parent record instead of lock for writing by using the AttachInsteadOfLock method.
  13. Update a parent record field.    
  14. Unlock the child record.
    The parent record also unlocks. 
  15. Unlock the child and parent tables.

Example code 

IRealTimeDatabase database = new RealTimeDatabase();
try
{
    // Open the RTDB.
    if (!database.IsOpen())
    {
        database.Open();
    }
    // Tables with a parent-child relationship.
    var parentTableName = "pipeline";
    var childTableName = "pipe_segment";               
    // Lock types.
    IRecordLockRequestType recordLockRequest = new RecordLockRequestType();               
    ITableLockRequestType tableLockRequest = new TableLockRequestType();
    using (IRealTimeTable childTable = new RealTimeTable(childTableName))
    using (IRealTimeTable parentTable = new RealTimeTable(parentTableName))
    {
        // Lock the child table for reading.
        childTable.Lock(tableLockRequest.SAFE_READ);
        var nameField = new RealTimeStringField(childTable, "name");
        // Locate the child record.
        using (var childRecord = childTable.FindRecord(nameField, "ATBT->Segment01"))
        {
            // Lock the child record for writing.
            childRecord.Lock(recordLockRequest.RESERVE_READ_WRITE);
            // Read the parent slot number.
            var parentSlot = childRecord.ReadData<Int32>("pipeline");
            // Locate the parent record.
            using (var parentRecord = parentTable.FindRecord(parentSlot))
            {
                // Using AttachInsteadOfLock instead of Lock for reading.
                parentRecord.AttachInsteadOfLock(recordLockRequest.RESERVE_READ_WRITE);                           
                var parentRecordName = parentRecord.ReadString("name");
                // Detach the parent record instead of unlock by using the DetachInsteadOfUnlock method.
                parentRecord.DetachInsteadOfUnLock();                           
                // Switch to write.
                childRecord.Lock(recordLockRequest.CONVERT_RESRW_SWRITE);
                // Using AttachInsteadOfLock instead of Lock for writing.
                parentRecord.AttachInsteadOfLock(recordLockRequest.SAFE_WRITE);
                parentRecord.WriteString("description", "New description");
            } // Leaving scope releases the parent record.
        } // Leaving scope releases the child record.
    } // Leaving scope releases child and parent tables.
}
catch (Exception ex)
{
    Console.WriteLine(
quot;An error occurred while converting read\\write locks for parent - child records:
{ ex.Message}"); }
TitleResults for “How to create a CRG?”Also Available in