Transactional Scope in Mule | Handling Transactions in Mule

  Transactional Scope in Mule | Handling Transactions in Mule




Mule applies the concept of transactions to operations in application for which the result cannot remain indeterminate. In other words, where a series of steps in a flow must succeed or fail as one unit, Mule uses a transaction to demarcate such a unit. For example, you might use a transaction to encapsulate several steps in a flow for which the end result involves committing information to a database. In this type of scenario, the commit is either entirely complete and succeeds, or is incomplete and it fails. Even if partially complete, the commit – or transaction – fails. Where a transaction fails, Mule rolls back the operations within the transaction so that no one part results in partial completion.

You can demarcate a transaction by applying a transaction to a connector. If a Mule flow begins with a transactional resource (such as an inbound connector), Mule can start a new transaction and manage the entire flow as a transaction. If your flow includes a transactional outbound connector, Mule manages the outgoing operation as a transaction. With both a transactional inbound and outbound connector, Mule executes the outgoing operation as part of the transaction initiated by the inbound connector.


Transactional scope in mule is used to handle transactions.

In below example we have two database connectors configured with two different tables.In transactional scope, if we are inserting records in both the tables so either they will insert in both or none of the two tables that means either data will commit or rollback.


flow of transactional scope :



Transactional scope configuration :





URL : http://localhost:8085/api/transactional
Method : POST

Input 1: 


Since we are not giving address attribute in input below so transaction will not be committed because address attribute is present in table info4 and is mandatory attribute.So if insertion in info4 fails then insertion in first table i.e., info will also be fail.Hence transaction will be rolled back.



Input 2 : 


Here we are address attribute also in input so transaction will be committed.



Output for Input 2 :




XML project code :



<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8085" basePath="/api" doc:name="HTTP Listener Configuration"/>
    <db:mysql-config name="MySQL_Configuration" host="localhost" port="3306" user="****" password="****" database="demodata" doc:name="MySQL Configuration"/>
    <flow name="test_transactionalFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/transactional" doc:name="HTTP"/>
        <ee:multi-transactional action="ALWAYS_BEGIN" doc:name="Transactional">
            <byte-array-to-string-transformer doc:name="Byte Array to String"/>
            <json:json-to-object-transformer returnClass="java.util.Map" doc:name="JSON to Object"/>
            <set-variable variableName="varpayload" value="#[payload]" doc:name="Variable"/>
            <db:insert config-ref="MySQL_Configuration" doc:name="Database">
                <db:parameterized-query><![CDATA[insert into info (ID,NAME,AGE) values (#[payload.id],#[payload.name],#[payload.age]);]]></db:parameterized-query>
            </db:insert>
            <set-payload value="#[flowVars.varpayload]" doc:name="Set Payload"/>
            <db:insert config-ref="MySQL_Configuration" doc:name="Database">
                <db:parameterized-query><![CDATA[insert into info4 (ID,NAME,AGE,ADDRESS) values (#[payload.id],#[payload.name],#[payload.age],#[payload.address]);]]></db:parameterized-query>
            </db:insert>
            <object-to-string-transformer doc:name="Object to String"/>
        </ee:multi-transactional>
    </flow>
</mule>




No comments:

Post a Comment



Please go through below tutorials:


Mule 4 Tutorials

DEPLOY TO CLOUDHUB C4E CLIENT ID ENFORCEMENT CUSTOM POLICY RABBIT MQ INTEGRATION
XML TO JSON WEBSERVICE CONSUMER VM CONNECTOR VALIDATION UNTIL SUCCESSFUL
SUB FLOW SET & REMOVE VARIABLE TRANSACTION ID SCATTER GATHER ROUND ROBIN
CONSUME REST WEBSERVICE CRUD OPERATIONS PARSE TEMPLATE OBJECT TO JSON LOAD STATIC RESOURCE
JSON TO XML INVOKE IDEMPOTENT FILTER FOR EACH FLAT TO JSON
FIXWIDTH TO JSON FIRST SUCCESSFUL FILE OPERATIONS EXECUTE ERROR HANDLING
EMAIL FUNCTIONALITY DYNAMIC EVALUATE CUSTOM BUSINESS EVENT CSV TO JSON COPYBOOK TO JSON
CHOICE ASYNC

Widely used Connectors in Mule 3

CMIS JETTY VM CONNECTOR SALESFORCE POP3
JMS TCP/IP WEBSERVICE CONSUMER QUARTZ MONGO DB
FILE CONNECTOR DATABASE CONNECTOR


Widely used Scopes in Mule 3

SUB FLOW REQUEST REPLY PROCESSOR CHAIN FOR EACH CACHE
ASYNC TCP/IP COMPOSITE SOURCE POLL UNTIL SUCCESSFUL
TRANSACTIONAL FLOW

Widely used Components in Mule 3

EXPRESSION CXF SCRIPT RUBY PYTHON
JAVASCRIPT JAVA INVOKE CUSTOM BUSINESS EVENT GROOVY
ECHO LOGGER


Widely used Transformers in Mule 3

MONGO DB XSLT TRANSFORMER REFERENCE SCRIPT RUBY
PYTHON MESSAGE PROPERTIES JAVA TRANSFORMER GZIP COMPRESS/UNCOMPRESS GROOVY
EXPRESSION DOM TO XML STRING VALIDATION COMBINE COLLECTIONS BYTE ARRAY TO STRING
ATTACHMENT TRANSFORMER FILE TO STRING XML TO DOM APPEND STRING JAVASCRIPT
JSON TO JAVA COPYBOOK TO JSON MAP TO JSON JSON TO XML FLATFILE TO JSON
FIXWIDTH TO JSON CSV TO JSON


Widely used Filters in Mule 3

WILDCARD SCHEMA VALIDATION REGEX PAYLOAD OR
NOT MESSAGE PROPERTY MESSAGE IDEMPOTENT FILTER REFERNCE
EXPRESSION EXCEPTION CUSTOM AND


Exception Strategy in Mule 3

REFERENCE EXCEPTION STRATEGY CUSTOM EXCEPTION STRATEGY CHOICE EXCEPTION STRATEGY CATCH EXCEPTION STRATEGY GLOBAL EXCEPTION STRATEGY


Flow Control in Mule 3

CHOICE COLLECTION AGGREGATOR COLLECTION SPLITTER CUSTOM AGGREGATOR FIRST SUCCESSFUL
MESSAGE CHUNK AGGREGATOR MESSAGE CHUNK SPLITTER RESEQUENCER ROUND ROBIN SOAP ROUTER