[Download] | [Documentation Home] | [Release Note]

Distributed Transactions Sample

Introduction

Distributed transactions are important when need to handle multiple transactions across multiple databases in a coordinated way. In this sample, We demonstrate the distributed transactions support in WSO2 Data Services Server.

Prerequisites

The sample data services should be deployed as per the instructions mentioned in Deploying Samples section.

Running the Sample

The sample service "DTPSampleService", can be run using the "tryit" tool, which is bundled with WSO2 Data Services Server, or a code-generated java client sample using this service is demonstrated in the Data Services Clients Sample section.

Service Definition

The scenario is based on a two separate banks which use two separate databases where distributed transactions get involves when doing bank transactions(transfer of money) between two accounts of different banks.

This service has used "distributed transactions with multiple data source" support. The usage of distributed transactions can be demonstrated by executing the following operations using the "tryit" tool.

  1. Run "addAccountToBank1" operation with the "balance" 1000. This will create a new account in bank1 and return the bank id. We will see an output as seen below,


    Figure 1: Add account to bank1.


  2. Run "addAccountToBank2" operation with the "balance" 2000. This will create a new account in bank2 and return the bank id. We will see an output as seen below,


    Figure 2: Add account to bank2.


  3. Run the "begin_boxcar" operation, now the service will switch to a boxcarring session.
  4. Run the "addToAccountBalanceInBank1" operation and give the values "1" for "accountId" and "-100" for the "value" fields.
  5. Run the "addToAccountBalanceInBank2" operation and give the values "1" for "accountId" field and "h200" for the "value" field. Here we are giving incorrect value to the "value" field. So if distributed transaction works correctly whole transaction(including the previous operation done on addToAccountBalanceInBank1) should be aborted though transaction involves two different databases.
  6. Run the "end_boxcar" operation.
  7. We can run "getAccountBalanceFromBank1" and "getAccountBalanceFromBank2" operations to verify transactions has aborted and distributed transaction works properly.
  8. Run the "getAccountBalanceFromBank1" operation. We will see an output as seen below,


    Figure 3: Get account balance from bank1.


  9. Run the "getAccountBalanceFromBank2" operation. We will see an output as seen below,


    Figure 4: Get account balance from bank2.


    As we can see, "addAccountToBank1" service call also rolled back because of the error in "addAccountToBank2" operation.