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>