Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Handle Transaction Scope in Mule

DZone's Guide to

How to Handle Transaction Scope in Mule

In this article, we'll learn how to roll back the clock (so to speak) using transaction scope to fix any files giving our application trouble.

· Integration Zone ·
Free Resource

The State of API Integration 2018: Get Cloud Elements’ report for the most comprehensive breakdown of the API integration industry’s past, present, and future.

Hi Mulies! In this tutorial, we are going to handle Transaction Scope (Roll Back Mechanism). 

Here is a small piece of a Transaction Scope application which I am going to demonstrate below.

Here Is the Flow:

Image title

Here, my use case is to insert a new employee record in two database tables. In Table 1, the employee Education Details are inserted and in Table 2 employee personal data is inserted. So while I am inserting the data of new employees, if I get any errors I need to roll back the information that I had previously inserted in Table 1.

Database 1:

In Database 1, I am inserting employee education details like Name, Graduation Date, Previous Experience, etc.

Logger:

I'm using a logger to print the number of records inserted in the Database Table.

Groovy:

Here I am using Groovy's sleep function to cause my flow to wait for 20 seconds to check whether the first database is executed correctly or not.

Image title


Database 2:

In Database2, I am inserting employee personal details like Name, Contact Details, etc.

But, while inserting the details, I missed a value, which causes the applicaiton to throw an error like ORA-00947: not enough values. Now I need to roll back the data which had previously been inserted in Database 1.

Application Testing:

Right click on the application and run the project so that if there are no errors in the application it will be deployed successfully.

Image title

You can see above that when the employee's education details are inserted in Table 1 the logger printed that one record had been successfully inserted.

So now the flow is made to sleep for 20 seconds. Later it throws an error due to the reasons I explained above. So now, due to transaction scope in Mule, you can go and check Table 1 for the record that is causing the application to not function and remove it. This is how you can use and work with transaction scope in Mule!

Hope this helps!

Suggest your Feedback 

Thank You!

Your API is not enough. Learn why (and how) leading SaaS providers are turning their products into platforms with API integration in the ebook, Build Platforms, Not Products from Cloud Elements.

Topics:
mulesoft ,mule 3.8 ,mule flow ,integration ,transaction scope

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}