Click here to Skip to main content
15,881,872 members
Articles / Programming Languages / C#

HOW to Compensate a Transaction in a BizTalk Orchestration

Rate me:
Please Sign up or sign in to vote.
4.00/5 (13 votes)
12 May 2006CPOL4 min read 142.4K   1.6K   39   27
This article explains the use of Compensatory Transactions in Orchestrations in BizTalk Server 2006

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 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 an 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.
<p<img src="/KB/biztalk/BizTalkCompensation/SPList.PNG" alt="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 - Click to enlarge image

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 after 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 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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect AT&T Wi-Fi Services
United States United States
Naveen has done his Masters (M.S.) in Computer science, has started his career programming the mainframes and now has more than a decade of programming, development and design experience. Naveen has a sharp eye and keen observation skills. Naveen has worked for several companies and strived hard to build large scale business applications and bringing better solutions to the table.
Quite recently Naveen has built a fairly complex integration platform for a large bank. His hobbies include training, mentoring and research. Naveen spends his free time visiting National Parks nationwide.

Naveen has developed the BizTalk Control Center (BCC)
http://biztalkcontrolcenter.codeplex.com

Comments and Discussions

 
QuestionDoes BizTalk compensate automatically? Pin
Krishan Aggarwal31-Jan-16 23:39
Krishan Aggarwal31-Jan-16 23:39 
Questionwhy not the same with Log running Transaction ? Pin
Shrilata Ellaboina1-Nov-15 19:39
Shrilata Ellaboina1-Nov-15 19:39 
GeneralNice article Pin
Petr Abdulin5-Jun-11 18:50
Petr Abdulin5-Jun-11 18:50 
GeneralMy vote of 5 Pin
Petr Abdulin5-Jun-11 18:44
Petr Abdulin5-Jun-11 18:44 
GeneralEvent viewer does not show messages of the application working. Pin
ishedge14-Sep-09 19:37
ishedge14-Sep-09 19:37 
Questionbinding file missing? Pin
senkiner21-Mar-09 18:53
senkiner21-Mar-09 18:53 
GeneralNot related to this article Pin
amitparva19-Mar-07 4:47
amitparva19-Mar-07 4:47 
GeneralRe: Not related to this article Pin
Naveen Karamchetti19-Mar-07 4:54
professionalNaveen Karamchetti19-Mar-07 4:54 
QuestionRe: Not related to this article Pin
amitparva19-Mar-07 6:31
amitparva19-Mar-07 6:31 
QuestionDeployment Error Pin
amitparva14-Mar-07 12:13
amitparva14-Mar-07 12:13 
When I try to deploy the application, I get following error:

"An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) "

Can someone please provide me with an insight on the solution. I have enabled remote connections for both TCP/IP and named pipes.

Thanks



AnswerRe: Deployment Error Pin
Naveen Karamchetti15-Mar-07 3:09
professionalNaveen Karamchetti15-Mar-07 3:09 
GeneralRe: Deployment Error Pin
amitparva18-Mar-07 14:31
amitparva18-Mar-07 14:31 
GeneralGood article... Pin
eladros2-Jul-06 18:11
eladros2-Jul-06 18:11 
QuestionWhat was updated? Pin
Nish Nishant12-May-06 2:41
sitebuilderNish Nishant12-May-06 2:41 
AnswerRe: What was updated? Pin
Naveen Karamchetti14-May-06 17:34
professionalNaveen Karamchetti14-May-06 17:34 
GeneralRe: What was updated? Pin
Smitha Nishant18-May-06 4:29
protectorSmitha Nishant18-May-06 4:29 
Questionlog ? Pin
Bogdan Nedelcu17-Apr-06 20:34
Bogdan Nedelcu17-Apr-06 20:34 
AnswerRe: log ? Pin
Naveen Karamchetti17-Apr-06 21:25
professionalNaveen Karamchetti17-Apr-06 21:25 
GeneralRe: log ? Pin
Bogdan Nedelcu17-Apr-06 21:28
Bogdan Nedelcu17-Apr-06 21:28 
GeneralRe: log ? Pin
Naveen Karamchetti18-Apr-06 22:51
professionalNaveen Karamchetti18-Apr-06 22:51 
GeneralMore About Synchronization in Scopes Pin
Karthikcse12-Apr-06 19:35
Karthikcse12-Apr-06 19:35 
AnswerRe: More About Synchronization in Scopes Pin
Naveen Karamchetti12-Apr-06 20:58
professionalNaveen Karamchetti12-Apr-06 20:58 
GeneralNot Related to this Article.. Pin
Sendilkumar.M10-Apr-06 22:09
Sendilkumar.M10-Apr-06 22:09 
JokeRe: Not Related to this Article.. Pin
Naveen Karamchetti11-Apr-06 0:20
professionalNaveen Karamchetti11-Apr-06 0:20 
GeneralRe: Not Related to this Article.. Pin
Sendilkumar.M11-Apr-06 18:07
Sendilkumar.M11-Apr-06 18:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.