The first example of using the transacted variable part will be to make the TstDepartmentView transaction safe, that is, it will work in the correct transaction regardless of whatever else is going on around it.
Making TstDepartmentView transaction safe
We will change the TstDepartmentView created earlier such that it shows details of the department in both the top-level transaction and also the shared transaction.
The part TransactedVariable is a special type of variable part. It has all the behavior of a variable except that it has an additional attribute transaction. When a variable is given a transaction it will ensure that all actions, events and attributes performed on the variable are within that transaction. For the TstDepartmentView we need two transacted variables, one that has the department within the shared transaction, and one that has the department within the top-level transaction.
This will ensure that the variable TstDepartment1 always sets the attribute values of its contents, the transaction, in the top-level transaction.
Now, test the view. Switch to the Transcript and get the shared transaction to resume itself as described earlier. When you go back to the view and edit some attributes of the selected department, the earlier walkback, caused by attempting to update the shared transaction, no longer occurs. This is because of the connection from the TopLevelTransaction variable to the transaction attribute of the TstDepartment variable. This ensures that access to the variable's value is always done in the context of the specified transaction.
Go back to the System Transcript and look at the current transaction using the method
Transaction current.
Note that it will be the top-level transaction (if you took the step to resume the shared transaction earlier). The transacted variable part will ensure its contents are always accessed in the context of its specified transaction but it will not change the current transaction.
In addition to specifying the transaction in which the TstDepartment1 variable should operate, we should also specify the same for the department home collection. When the New is clicked, it performs the create action, thus the new department should be created in the top-level transaction.
To create a transacted variable that contains the TstDepartmentHome1 part
The transacted variable is then added for you and the connection from the self attribute of the TstDepartmentHome1 part to the new variable that will be named transacted TstDepartmentHome1.
As was the case earlier, make sure that the ordering of the committed events from the TopLevelTransaction are such that the connection to the SharedTransaction part is first.
This view now ensures that the TstDepartment1 variable and the transacted TstDepartment1 variable are always accessed within the context of the transaction within the TopLevelTransaction1 variable, regardless of whatever else is going on around the view in terms of which is the current transaction.