Objective: Demonstrate the use of the Transaction Mediator in a distributed transaction.
Prerequisites
...
Table of Contents |
---|
Introduction
This sample demonstrates the Transaction Mediator using a sample distributed transaction. In this sample, a record is delete 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 will roll back, and the records will be untouched.
Also, see Transaction Mediator.
Prerequisites
For a list of prerequisites, refer to Prerequisites section in ESB samples setup page.
Building the sample
1. First, let's take a look at the synapse configuration used for this sample.
Code Block | ||||
---|---|---|---|---|
| ||||
<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>java:jdbc<dsName>jdbc/XADerbyDS</dsName> <icClass>org.jnp.interfaces.NamingContextFactory</icClass> <url>localhost:1099</url> <user>synapse</user> <password>synapse</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>java:jdbc<dsName>jdbc/XADerbyDS1</dsName> <icClass>org.jnp.interfaces.NamingContextFactory</icClass> <url>localhost:1099</url> <user>synapse</user> <password>synapse</password> </pool> </connection> <statement> <sql>INSERT into company values ('IBM','c4',12.0)</sql> </statement> </dbreport> <transaction action="commit"/> <send/> </out> </sequence> </definitions> |
1. To run this sample it is required to deploy synpase on JBoss application server (This is only tested with JBoss application sever), you can use the war distribution to deploy synapse on JBoss application server.
2. Use the synpase_sample_652.xml
as the synapse confiuration file and start JBoss with that synpase configuration file.
3. Define two XA datasources for above two datasources.
Info | ||
---|---|---|
| ||
Refer JBoss documentation to see how to do this. |
4. Create two database instances, this will be used by the two XA data sources.
...
title | Tip |
---|
...
2. The above sample configuration uses two data sources and database instances to point to the sample databases. You have to manually create these in your environment for the sample to work.
- For information on creating database instances, refer to Database Instances. Refer the Sample Setup Guide to see how you can set up the derby database server.
In this sample a record is delete from one database and it is added into the second database. If either of the operations(deleting from the first database and adding into the second database) fails everything will be roll backed. The records will be untouched.
...
- For information on creating the two sample XA data sources, refer to section Data Sources.
3. Start the ESB server and log into its management console UI (https:
//localhost:9443/carbon
). In the management console, navigate to Main Menu, click Service Bus and then Source View. Next, copy and paste the above synapse configuration, which helps you explore the example scenario, to the source view
4. Deploy the back-end service SimpleStockQuoteService, and start the Axis2 server using the instructions given in section Starting Sample Back-End Services.
Info | ||
---|---|---|
| ||
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
We run this setup using the sample Stock Quote Client, which can operate in several modes. For information on this sample client, refer to section Stock Quote Client.
Execute the following ant command to invoke the sample client.
Code Block | ||
---|---|---|
ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=SUN/ | ||
Excerpt | ||
|
You can try deleting a record from one database and adding to a second database. Simulate a failure scenario and notice how all operations roll back, and none of the records gets affected.