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="" xmlns:db="" xmlns:json="" xmlns:http="" xmlns="" xmlns:doc=""
    <http:listener-config name="HTTP_Listener_Configuration" host="" 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.age]);]]></db:parameterized-query>
            <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.age],#[payload.address]);]]></db:parameterized-query>
            <object-to-string-transformer doc:name="Object to String"/>

© 2020

Share on :

Mule 4:

XML to JSON in mule 4 Web service consumer VM Validation in Mule 4 Until Successful
Sub flow Set Variable & Remove Variable Set Transaction ID Scatter Gather Round Robin
Consume Restful Service CRUD in Mule 4 Parse Template Object to JSON Load Static Resource
JSON to XML Invoke Idempotent Filter ForEach Flat file to JSON
Fixwidth to JSON First Successful File Execute Error Handling
Email Dynamic Evaluate Custom Business Event CSV to JSON Copybook to JSON
Choice Router Async RabbitMQ

Mule 3:

Database Connector Async CXF Attachement XSLT
Mongo DB Cache scope Custom Business Event Gzip Compress Expression
SMTP For Each Expression Javascript Filter Reference
Salesforce Message Enricher Groovy Parse Template Idempotent
VM Poll Invoke Ruby Message
Webservice consumer Python Transformer reference Message Property Not
Collection Aggregator First Sucessful Catch Exception Strategy Reference Exception Strategy Or
Choice Message Chunk Aggregator Choice Exception Strategy Global Exception Strategy Schema Validation
Custom Aggregator Scatter Gather Custom Exception Strategy Batch Processing MEL
M Unit RAML Map Operator API Mocking Dataweave