Introduction
Distributed transactions are important when need to handle multiple transactions across multiple databases in a coordinated way. This sample demonstrates the distributed transaction support in WSO2 Data Services Server.
Building the sample
The sample data service DTPSampleService should be deployed using the instructions in Samples Setup section.
Running the sample
The sample service can be run using the TryIt tool, which is bundled with the WSO2 Data Services Server, or a code-generated java client sample as discussed in the Data Services Clients section.
Service description
The scenario is based on two banks using separate databases. Distributed transactions are involved in their banking transactions (transfer of money) between two accounts of different banks. This service uses distributed transactions with multiple data sources.
Sample run
Sample run of the service using the TryIt tool is shown below.
1. Run addAccountToBank1 operation with balance 1000. This creates a new account in bank1 and returns the bank id. Output is as follows:
2. Run addAccountToBank2 operation with balance 2000. This creates a new account in bank2 and returns the bank id. Output is as follows:
3. Run the begin_boxcar operation. 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, the whole transaction (including the previous operation done on addToAccountBalanceInBank1) should be aborted eventhough the transaction involves two different databases.
6. Run the end_boxcar operation.
7. We can run getAccountBalanceFromBank1 and getAccountBalanceFromBank2 operations to verify transactions have aborted and distributed transaction works properly.
8. Run the getAccountBalanceFromBank1 operation. The Output is as follows.
9. Run the getAccountBalanceFromBank2 operation. The output is as follows:
As we can see, the addAccountToBank1 service call also rolled back because of the error in addAccountToBank2 operation.