5,401,186 members and growing! (17,756 online)
Email Password   helpLost your password?
Enterprise Systems » Microsoft BizTalk Server » General     Advanced

HOW To Compensate a Transaction in a BizTalk Orchestration

By Naveen Karamchetti

This article explains the use of Compensatory Transactions in Orchestrations in BizTalk Server 2006
C#, Windows, .NET, Visual Studio, Dev

Posted: 10 Apr 2006
Updated: 12 May 2006
Views: 44,389
Bookmarked: 21 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
8 votes for this Article.
Popularity: 3.20 Rating: 3.54 out of 5
1 vote, 12.5%
1
1 vote, 12.5%
2
0 votes, 0.0%
3
1 vote, 12.5%
4
5 votes, 62.5%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

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 has been used in this article.

Compensation - Points to remember...

  • A Compensation section is generally written, in order to UNDO the effect of a transaction.
  • The Compensation section for a scope gets activated only 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 Shape in 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 shape can 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 a Employee leave request needs to be processed. The following are the steps which describe it in more detail...

  • An EmployeeLeaveHistory record is inserted into the database as soon as a request arrives in BizTalk. DD
  • The number of leaves requested is calculated from the EmployeeLeaveHistory record and the columns SickLeaveBalance and EarnedLeaveBalance in the Employee table are updated.
  • The Employee table update can fail, if the number of leaves in the columns SickLeaveBalance or EarnedLeaveBalance remaining for an employee is zero or less than the requested leaves.
  • Once an exception occurs, the EmployeeLeaveHistory record created in the first step needs to be deleted/removed. This is the compensatory transaction.
  • The following is the list of Stored Procedures used in this scenario.

      SPList

  • The Stored Procedures are called from a .NET Component called DBLib, which uses a data class known as LeaveHistoryData

      CD

  • The Leave Application Schema - Observe the structure and the elements of the LeaveApplicationSchema

      LeaveAppSchema

Leave Compensation Orchestration

LeaveCompensate OrchestrationView SolutionExplorer

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

The Parent scope is a long running transaction which consists of two Atomic transactions. The First atomic transaction creates a new 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...

EventViewer

Sample Example using a Parallel Shape

ParallelCompensation

The point to note in this example is that the exception raised in the Updation Atomic Scope branch does NOT interrupt the Insertion Atomic Scope branch. The exception is raised only afer the Insertion branch completes its execution.

The RED arrow indicates the flow-of-control from the Compensation Shape to the Compensatory Section.

BizTalkExplorer

About the Downloadable Code

  • Unzip and Restore the database backup file EmployeeDB.zip into the SQL 2005 Database Server on your box. 
  • Unzip the BizTalk project zip file with the folder names in the C:\ drive.
  • Add the User Environment variable "DBLIB.EMPLOYEEUTIL" with the value as the Database Connection String.
  • Place the LeaveRequestMessage in the In folder and check the Event Viewer.

Some Takeaways

  1. Set the Synchronized property on the Scope shape to true, in the case you want to handle exceptions.
  2. Compensation is an excellent mechanism to achieve database consistency during transactional failures.
  3. Compensatory transactions must be widely used in all the transactions involving disparate databases and integration projects.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Naveen Karamchetti


Naveen Karamchetti has done his Masters (M.S.) in Software Engineering from B.I.T.S, Pilani and is based out of Fremont, CA.
The author has more than 8.5 yrs of experience in the IT industry, has started his career
starting from the good old days of using a Unix, Tcl/tk. The author has been associated with
several companies based out of Silicon Valley, California.

The author has won several programming awards within the organizations worked and is well-recognized. The author has also worked for Microsoft based at Redmond.

Hobbies include training, mentoring and research. Spends his free time travelling in the BART (Bay Area Rapid Transit) in Fremont, CA.
Occupation: Software Developer (Senior)
Location: United States United States

Other popular Microsoft BizTalk Server articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 21 of 21 (Total in Forum: 21) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralNot related to this articlememberamitparva5:47 19 Mar '07  
GeneralRe: Not related to this articlememberNaveen Karamchetti5:54 19 Mar '07  
QuestionRe: Not related to this articlememberamitparva7:31 19 Mar '07  
QuestionDeployment Errormemberamitparva13:13 14 Mar '07  
AnswerRe: Deployment ErrormemberNaveen Karamchetti4:09 15 Mar '07  
GeneralRe: Deployment Errormemberamitparva15:31 18 Mar '07  
GeneralGood article...membereladros19:11 2 Jul '06  
QuestionWhat was updated?staffNishant Sivakumar3:41 12 May '06  
AnswerRe: What was updated?memberNaveen Karamchetti18:34 14 May '06  
GeneralRe: What was updated?staffSmitha Vijayan5:29 18 May '06  
Generallog ?memberBogdan Nedelcu21:34 17 Apr '06  
GeneralRe: log ?memberNaveen Karamchetti22:25 17 Apr '06  
GeneralRe: log ?memberBogdan Nedelcu22:28 17 Apr '06  
GeneralRe: log ?memberNaveen Karamchetti23:51 18 Apr '06  
GeneralMore About Synchronization in ScopesmemberKarthikcse20:35 12 Apr '06  
AnswerRe: More About Synchronization in ScopesmemberNaveen Karamchetti21:58 12 Apr '06  
GeneralNot Related to this Article..memberSendilkumar.M23:09 10 Apr '06  
JokeRe: Not Related to this Article..memberNaveen Karamchetti1:20 11 Apr '06  
GeneralRe: Not Related to this Article..memberSendilkumar.M19:07 11 Apr '06  
GeneralRe: Not Related to this Article..memberNaveen Karamchetti20:37 11 Apr '06  
GeneralRe: Not Related to this Article..memberBogdan Nedelcu21:13 19 Apr '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 May 2006
Editor:
Copyright 2006 by Naveen Karamchetti
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project