Scenario:
when working with multiple Component Interfaces in Application Engine Program if one fails then it Rollbacks the changes made by other CIs'
Solution:
Local ApiObject &SESSION, &STUCI, &EDUCI; //declaring Objects
&SESSION= %Session; //initializing Object
&STUCI = &SESSION.GetCompIntfc(COMPINTFC.ADD_STUDENT); /*Where ADD_STUDENT is the Component Interface based on ADD_Stu_Info Component.*/
StartWork();
If &STUCI.Save() Then
&EDUCI = &SESSION.GetCompIntfc(COMPINTFC.ADD_EDUCATION);
/*Where ADD_EDUCATION is the Component Interface based on ADD_EDU_Info Component.*/
// Enter Your Custom Code
If &EDUCI.Save() Then
CommitWork();
else
SQLExec("ROLLBACK");
end-if;
else
SQLExec("ROLLBACK");
end-if;
Methods Used:
StartWork();
StartWork function is used to mark the start of a unit of work. Once this function is executed, no updates to the database are allowed until a unit of work is completed. A unit of work is completed by an event completing (such as a FieldChange event) in which case all the Updates are saved.
A unit of work can also be completed using the CommitWork built-in function.
If a SQL failure occurs anytime during the unit of work, after the StartWork function has been called and before the unit of work completes, all updates are rolled back, up to when the StartWork function was executed.
This function can be used for nested component interface calls, such that if the lower level component interface fails, any database changes made by the calling component interface can be rolled back.
CommitWork();
Will this work even when the first and second CI are inter related? For instance, second CI requires that the first CI is saved before second CI can trigger functionality. Example: First CI is to add a student record and second CI is to add the subjects for the student record. So when the first CI is successfully saved, the student record is saved. Then the second CI can use the student record as keys and then add the subject details. In this scenario , the rollback wouldn't work in case the second CI fails, because the data of the first CI is saved already.
ReplyDelete