Sample 657: Distributed Transaction Management
Note that WSO2 EI is shipped with the following changes to what is mentioned in this documentation:
<PRODUCT_HOME>/
repository/samples/
directory that includes all Integration profile samples is changed to<EI_HOME>/
samples/service-bus/
.<PRODUCT_HOME>/
repository/samples/resources/
directory that includes all artifacts related to the Integration profile samples is changed to<EI_HOME>/
samples/service-bus/resources/
.
Introduction
This sample demonstrates the functionality of the /wiki/spaces/EI6xx/pages/49612647 using a sample distributed transaction. In this sample, a record is deleted from one database and added to a second database. If either of the operations (deleting from the first database and adding to the other) fails, all operations rollback, and the records do not change.
Prerequisites
- For a list of general prerequisites, see Setting Up the ESB Samples.
- The sample configuration uses two datasources and database instances to point to the sample databases. You have to manually create these in your environment for the sample to work.
Setup two distributed Derby databases esbdb and esbdb1.  For instructions on setting up the Derby databases, see Setting up Remote Derby.
Create a table in esbdb by executing the following statement.
CREATE table company(name varchar(10) primary key, id varchar(10), price double);
Create a table in esbdb1 by executing the following statement.
CREATE table company(name varchar(10) primary key, id varchar(10), price double);
Insert records into the two tables that you created by executing the following statements.
To insert records into the table in esbdb
INSERT into company values ('IBM','c1',0.0); INSERT into company values ('SUN','c2',0.0);
To insert records into the table in esbdb1
INSERT into company values ('SUN','c2',0.0); INSERT into company values ('MSFT','c3',0.0);
Note
When inserting records into the tables, the order of the record matters.
Add the required datasource via the Management Console or create datasource declarations for the distributed databases In theÂ
master-datasources.xmlÂ
file. For information on how you can add datasources via the management console, see Adding Datasources. To create datasource declarations In theÂmaster-datasources.xml
 file, add the following code segments in theÂmaster-datasources.xml
 file located in the <ESB_HOME>/repository/conf/datasourcesÂ
directory, ensuring that the datasource file names areÂ*-xa-ds.xml
:
Datasource1: esb-derby-xa-ds.xml<datasources> <xa-datasource> <jndi-name>jdbc/XADerbyDS</jndi-name> <isSameRM-override-value>false</isSameRM-override-value> <xa-datasource-class>org.apache.derby.jdbc.ClientXADataSource</xa-datasource-class> <xa-datasource-property name="portNumber">1527</xa-datasource-property> <xa-datasource-property name="DatabaseName">esbdb</xa-datasource-property> <xa-datasource-property name="user">esb</xa-datasource-property> <xa-datasource-property name="password">esb</xa-datasource-property> <metadata> <type-mapping>Derby</type-mapping> </metadata> </xa-datasource> </datasources>
Datasource2: esb-derby1-xa-ds.xml
<datasources> <xa-datasource> <jndi-name>jdbc/XADerbyDS1</jndi-name> <isSameRM-override-value>false</isSameRM-override-value> <xa-datasource-class>org.apache.derby.jdbc.ClientXADataSource</xa-datasource-class> <xa-datasource-property name="portNumber">1527</xa-datasource-property> <xa-datasource-property name="DatabaseName">esbdb1</xa-datasource-property> <xa-datasource-property name="user">esb</xa-datasource-property> <xa-datasource-property name="password">esb</xa-datasource-property> <metadata> <type-mapping>Derby</type-mapping> </metadata> </xa-datasource> </datasources>
Building the sample
The XML configuration for this sample is as follows:Â
<definitions xmlns="http://ws.apache.org/ns/synapse" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ws.apache.org/ns/synapse http://synapse.apache.org/ns/2010/04/configuration/synapse_config.xsd"> <sequence name="myFaultHandler"> <log level="custom"> <property name="text" value="** Rollback Transaction**"/> </log> <transaction action="rollback"/> <send/> </sequence> <sequence name="main" onError="myFaultHandler"> <in> <send> <endpoint> <address uri="http://localhost:9000/services/SimpleStockQuoteService"/> </endpoint> </send> </in> <out> <transaction action="new"/> <log level="custom"> <property name="text" value="** Reporting to the Database esbdb**"/> </log> <dbreport useTransaction="true" xmlns="http://ws.apache.org/ns/synapse"> <connection> <pool> <dsName>jdbc/XADerbyDS</dsName> <user>esb</user> <password>esb</password> </pool> </connection> <statement> <sql>delete from company where name =?</sql> <parameter expression="//m0:return/m1:symbol/child::text()" xmlns:m0="http://services.samples" xmlns:m1="http://services.samples/xsd" type="VARCHAR"/> </statement> </dbreport> <log level="custom"> <property name="text" value="** Reporting to the Database esbdb1**"/> </log> <dbreport useTransaction="true" xmlns="http://ws.apache.org/ns/synapse"> <connection> <pool> <dsName>jdbc/XADerbyDS1</dsName> <user>esb</user> <password>esb</password> </pool> </connection> <statement> <sql>INSERT into company values ('IBM','c4',12.0)</sql> </statement> </dbreport> <transaction action="commit"/> <send/> </out> </sequence> </definitions>
This configuration file synapse_sample_657.xml
is available in the <ESB_HOME>/repository/samples
directory.
To build the sample
Start the ESB with the sample 657 configuration. For instructions on starting a sample ESB configuration, see Starting the ESB with a sample configuration.
The operation log keeps running until the server starts, which usually takes several seconds. Wait until the server has fully booted up and displays a message similar to "WSO2 Carbon started in n seconds."
Start the Axis2 server. For instructions on starting the Axis2 server, see Starting the Axis2 server.
Deploy the back-end service SimpleStockQuoteService. For instructions on deploying sample back-end services, see Deploying sample back-end services.
Infor
WSO2 ESB comes with a default JTA transaction manager (Atomikos), which allows you to run distributed transactions without deploying the ESB on an external application server.
You now have a running ESB instance, databases and a back-end service deployed in your environment.
Executing the sample
The sample client used here is the Stock Quote Client, which can operate in several modes. For further details on this sample client and its operation modes, see Stock Quote Client.
To execute the sample client
Execute the following command from theÂ
<ESB_HOME>/samples/axis2Client
 directory to test the successful scenario.ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=IBM
Execute the following command from theÂ
<ESB_HOME>/samples/axis2Client
 directory to test the failure scenario.ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=SUN
Analyzing the output
When you run the client to test the successful scenario, you will see that the IBM record is removed from the first database and added to the second database.
When you run the client to test the failure scenario, you will see that a record is neither deleted from the first database nor added into the second database. That is because executing the command attempts to add an already existing record again to the second database, which results in the fault sequence being executed. Therefore, an exception raised for duplicate entries and results in an entire transaction rollback.