Distributed transactions are important when we coordinate multiple transactions across multiple databases. This sample demonstrates the support for distributed transactions in WSO2 Data Services Server.
About the sample
The scenario used in this sample data service (DTPSampleService) is based on two banks using separate databases. This data service uses distributed transactions with multiple data sources.
Building the sample
The sample data service DTPSampleService should be deployed using the instructions in 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 section.
Follow the steps below to demonstrate this functionality using the TryIt tool:
- Log in to the management console of your server and click List under Services in the navigator. The DTPSampleService will be listed here.
- Click Try this service to open the TryIt tool. Execute the following operations:
- Run addAccountToBank1 operation with balance 1000. This creates a new account in bank1 and returns the bank id. Output is as follows:
- Run addAccountToBank2 operation with balance 2000. This creates a new account in bank2 and returns the bank id. Output is as follows:
- Run the begin_boxcar operation. The service will switch to a boxcarring session.
- Run the addToAccountBalanceInBank1 operation and give the values "1" for "accountId" field and "-100" for the "value" field.
- Run the addToAccountBalanceInBank2 operation and give the values "1" for "accountId" field and "h200" for the "value" field. Here we are giving an 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 even though the transaction involves two different databases.
- Run the end_boxcar operation.
- We can run getAccountBalanceFromBank1 and getAccountBalanceFromBank2 operations to verify that the transactions have aborted and that distributed transaction works properly.
- Run the getAccountBalanceFromBank1 operation. The output is as follows:
- 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.