[Going deeper into WF] -- Compensation)

Source: Internet
Author: User

Basic Concepts
First, let's review the locking mechanism of the transaction. A transaction locks a record before it is committed to prevent queries from other execution processes. However, a long-term locking transaction not only compromises the scalability of the program, but may even cause deadlocks.
The so-called compensation, simply put, is the time-space tunnel, and re-do the correct things that have been done before. It puts aside the lock mechanism to commit transactions as quickly as possible and continue execution. if an error occurs at a later point, we can make up for the previous transaction. This may not reverse the transaction, but we can take other measures (such as canceling) to make up for the impact of this transaction failure.
In WF3.5, we can only compensate for activities that implement the ICompensatableActivity interface. In the basic activity library, CompensatableSequenceActivity and CompensatableTransactionScopeActivity both implement this interface .. (To be verified)
1.1 CompensatableSequenceActivity
The function of the CompensatableSequenc activity is equivalent to the Sequence activity of the Compensation Handler. You can View the Compensation processor View by right-clicking the View Compensation Handler in the menu.
In the compensation handler view, we can add an activity to compensate its subactivities.
1.2 CompensatableTransactionScopeActivity
The CompensatableTransactionScope activity function is equivalent to attaching the TransacionScope of the compensation handler. Therefore, the CompensatableTransactionScope activity cannot contain the CompensatableTransactionScope activity as its subactivity.
In the compensation handler view of the CompensatableTransactionScope activity, we can use the activity to define the compensation logic.
Remember, compensation takes effect only when the compensation activity is successfully completed.

1.3 CompensateActivity
We can only compensate for the activities that implement the ICompensatableActivity interface. In addition to the activities in the two basic activity libraries mentioned earlier, we can also create custom activities that implement the ICompensatableActivity interface.
Specify the activity to be compensated for the TargetActivityName attribute of the Compensate activity, and the Runtime will execute the compensation processing program for the target activity. the Compensate activity can only exist in the error handler or compensation handler. when this activity is in the compensation handler, it can also trigger compensation for its subactivity transactions. it may be hard to understand. Let me explain it again:
The Compensate activity can be regarded as a trigger, which can trigger the compensation handler of the target activity to make compensation.
The ICompensatableActivity interface must be implemented for the target activity that uses Compensate for compensation, and must be completed smoothly. As we mentioned earlier, the so-called compensation is a process of making up for it, the error is generated and discovered after the transaction is committed. At this time, the Compensate activity must be placed in the error handler.
If a compensated activity also contains a compensated subactivity (such as a nested CompensatableSequence activity) and its subactivities have an error at a certain step after the transaction is committed, therefore, adding a Compensate activity to the compensation handler of this activity (rather than its subactivity) can Compensate for this error.
Define Compensation
Brief Introduction
The Compensate activity is used to Compensate for previously completed activities that require compensation. Therefore
CompenationHandler is executed only when the workflow has an exception or the activity execution is canceled;
ConfirmationHandler is executed after the workflow is executed normally;
CancellationHandler is executed when the activity is canceled.
Result: The parameter of the activity execution Result.
Code example:
New CompensableActivity
{
Body = new WriteLine {Text = "CompensableActivity: Do First Action "},

ConfirmationHandler = new WriteLine {Text = "CompensableActivity: Confirm First Action "},
CompensationHandler = new WriteLine {Text = "CompensableActivity: Compensate First Action "},
CancellationHandler = new WriteLine {Text = "CompensableActivity: Cancel First Action "},
Result = tokenA
},
Declared instance:
Xml is a type of Declarative Programming because it is defined using xaml.
Show call compensation Activity
Use Confirm and Compensate to display the call.
Use
Official instances
Eg1 simple compensation activities
Introduction: In the normal execution order, the compensation activity is used to define the work to be done. It is necessary to compensate for this operation later.
Related Knowledge points:
Use ManualResetEvent to create a thread for the application to run.
// Executing the sequence of two actions that complete with success
Console. WriteLine ("Execution with success :");
Application = new WorkflowApplication (BuildASequenceofCompensableActions ());
ManualResetEvent resetEvent = new ManualResetEvent (false );
Application. Completed = delegate (WorkflowApplicationCompletedEventArgs e)
{
ResetEvent. Set ();
};
Application. Run ();
ResetEvent. WaitOne ();

Compensation activities triggered by cancellation of eg2
Introduction: Use a compensation range to group A series of compensatory activity sets so that they are isolated from other compensation ranges.
Related Knowledge points
CompensationToken, used to arrange the execution sequence of compensation activities by recording the issuing token.
Eg3 customized Compensation
Introduction: the compensation and compensation processor is used to define the customized compensation logic.
Eg4 distributed compensation
Introduction: compensation is used to achieve parallel operation of the workflow service host.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.