This documentation is for WSO2 Enterprise Service Bus version 4.8.1 . View documentation for the latest release.

All docs This doc
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 8 Next »

Introduction

This sample demonstrates the functionality of the  Transaction Mediator 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 will roll back, and the records will not change.

Prerequisites

For a list of general prerequisites, see Setting Up the ESB Samples.

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>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>jdbc/XADerbyDS1</dsName>
                        <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>

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.

To build the sample

  1. Create the required database instances, see Setting up the Physical Database

  2. Set up the Derby database server, see Setting Up the Derby Database.
  3. Create the two sample XA data sources, see Adding Datasources and Transaction Examples.
  4. Start the Axis2 server. For instructions on starting the Axis2 server, see Starting the Axis2 server.

  5. 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 all operations roll back and none of the records are affected.

  • No labels