What is Compensation?
Most of us are aware of an Atomic transaction, which can either be committed or rolled back. A Long Running (L-R) transaction is one which executes for several hours, days or even weeks and contains several atomic transactions. These smaller atomic transactions which are completed/committed cannot be rolled back and hence need to be compensated. This undo operation which consists of deleting a record, in the case of an insert operation is known as Compensation and such a transaction is known as a Compensatory Transaction. BizTalk Server and Compensation!
BizTalk provides the capabilities and facilities for the compensation to happen, but the developer is responsible for keeping track of all database changes and performing the appropriate undo operations. NOTE: BizTalk does NOT automatically perform compensation. BizTalk Server 2006 and Visual Studio 2005 have been used in this article.Compensation - Points to Remember...
- A
Compensation sectionis generally written, in order to UNDO the effect of a transaction. - The Compensation section for a scope gets
activatedonly when the scope terminates normally or the scope completes its execution. - Unlike exceptions, a compensation section can be written for a "Atomic" as well as a "L-R" transaction.
- A Compensation section needs to be explicitly invoked, using the
Compensation Shapein order for it to execute. - A Compensation section for a scope does NOT automatically undo the effect of a transaction. The compensation section must have the undo logic in place in order to have such an effect.
- A
Compensation shapecan be used to invoke/execute a compensation section. The control returns to the next operation after compensation shape, once the execution of the compensation section is complete.
Scenario 1 : Employee Leave Request Processing...
Consider a scenario where an Employee leave request needs to be processed. The following are the steps which describe it in more detail...- An
EmployeeLeaveHistoryrecord is inserted into the database as soon as a request arrives in BizTalk.
- The number of leaves requested is calculated from the
EmployeeLeaveHistoryrecord and the columnsSickLeaveBalance and EarnedLeaveBalancein theEmployeetable are updated. - The
Employeetable update can fail, if the number of leaves in the columnsSickLeaveBalance or EarnedLeaveBalanceremaining for an employee is zero or less than the requested leaves. - Once an exception occurs, the
EmployeeLeaveHistoryrecord created in the first step needs to be deleted/removed. This is the compensatory transaction. - The following is the list of
Stored Proceduresused in this scenario.
- The Stored Procedures are called from a
.NET ComponentcalledDBLib, which uses a data class known asLeaveHistoryData
- The Leave Application Schema - Observe the structure and the elements of the
LeaveApplicationSchema
Leave Compensation Orchestration:
In the
Leave Compensation orchestration, there are three scopes being used...- Main Long Running Transaction Scope - Parent Scope
- Leave History Record Insertion Atomic Scope - Child Scope
- Employee Record Updation Atomic Scope - Child Scope
EmployeeLeaveHistory record. The updation scope updates the columns SickLeaveBalance or EarnedLeaveBalance in the Employee table. The Stored procedure raises an exception when the number of leaves remaining is zero or less than the requested number of leaves. Observe that the exception is caught by the Parent scope and the Compensation Shape invokes the Compensation section of the Leave History Record Insertion Atomic Scope and this section deletes the record or in other words performs an undo operation (Compensatory Transaction). The
RED arrow indicates the flow-of-control from the Compensation Shape to the Compensatory Section. Event Log Messages...
Observe the event log messages...Sample Example using a Parallel Shape
Updation Atomic Scope branch does NOT interrupt the Insertion Atomic Scope branch. The exception is raised only after the Insertion branch completes its execution.The
RED arrow indicates the flow-of-control from the Compensation Shape to the Compensatory Section. Some Takeaways
- Set the
Synchronizedproperty on theScopeshape totrue, in case you want to handleexceptions. - Compensation is an excellent mechanism to achieve database consistency during transactional failures.
- Compensatory transactions must be widely used in all the transactions involving disparate databases and integration projects.
No comments:
Post a Comment