Sunday, July 17, 2016

Configuring XA transactions with mysql database in ESB

In this example scenario I'm showing you how to configure XA-Transaction with ESB using mysql database. First you need to login to the mysql database and create a database call "test". Inside that database execute the following command.

PersonID int,
Name varchar(255)

Use case:
  1. create new transaction
  2. insert into database
  3. Call a rest endpoint. 
  4. When response is received commit the  transaction.
When configuring XA transactions the best practice is to use datasource class name instead of using the driver class name. Please follow the below instructions to configure master-datasources.xml with datasource class name.
  • Open <ESB_HOME>/repository/conf/datasources/master-datasources.xml.
  • Add the below configuration under <datasources> tag. (You can change the datasource name to any name you like.)
    <definition type="RDBMS">
            <validationQuery>select 1</validationQuery>
                <property name="serverName">localhost</property>
                <property name="portNumber">3306</property>
                <property name="databaseName">test</property>
                <property name="user">root</property>
                <property name="password">root</property>

  • Copy mysql driver library to <ESB_HOME>/repository/components/lib folder. 
  • Re-start the ESB server. 

Below is the ESB API configuration for this use case.

 <api context="/bpmn" name="xa_transaction_example">
        <resource faultSequence="faultSequenceREST" methods="POST" uri-template="/test">
                <log level="custom">
                    <property name="text" value="** In Sequencce started**"/>
                <transaction action="new"/>
                <dbreport useTransaction="true">
                        <sql><![CDATA[INSERT INTO Persons (PersonID,Name) VALUES ('1234','Tom')]]></sql>
                <call blocking="true">
                        <address uri=""/>
                <log level="full"/>
                <transaction action="commit"/>
                <log level="custom">
                    <property name="text" value="**Transaction commited**"/>

Get Redirection URL from ESB

In this article I will show you how to get redirection URL from ESB. Assume that you call a back-end service from ESB proxy service and that back-end service re-direct to another service. In that case you will get a response from the server something like below.

HTTP/1.1 308 Redirection (308)[\r][\n]"
Server: Cowboy[\r][\n]
Connection: close[\r][\n]
Content-Type: application/xml; charset=utf-8[\r][\n]
Date: Mon, 18 Jul 2016 05:40:21 GMT[\r][\n]
Via: 1.1 vegur[\r][\n]

If you want to get the re-direction url which is with the "Location" header,  you can use the the property mediator as below. 

 <property xmlns:ns="http://org.apache.synapse/xsd"