Tuesday, 23 January 2018

A Complete Guide to Spring Transaction Management

           In the previous post, we learned how to perform CRUD operations using Spring and Hibernate. In this article, we will discuss Spring Transaction Management with simple examples.

A transaction is a logical unit of work that consists of one or more operations or statements. It is an atomic unit, which means that either all the operations within the transaction are committed successfully, or all of them are rolled back if an error occurs. This ensures data consistency and integrity.

The following diagram illustrates how transaction management works:


       Transaction management is an essential part of enterprise applications because it ensures data integrity and consistency.

The concept of a transaction is based on the ACID properties:

  • Atomicity

  • Consistency

  • Isolation

  • Durability


  • Atomicity
           Atomicity ensures that a transaction is executed as an "all-or-nothing" operation. This means that either all the operations in a transaction are completed successfully, or none of them are.

If any operation within a transaction fails, the entire transaction is rolled back, ensuring that no partial changes are saved to the database.


  • Consistency
          Consistency ensures that the database remains in a valid and consistent state before and after a transaction. Any data written to the database must satisfy all defined constraints, rules, cascades, and triggers. If a transaction violates any of these rules, it is rolled back to maintain data integrity.

  • Isolation
             Isolation ensures that each transaction is executed independently in a concurrent environment, preventing interference from other transactions and avoiding data inconsistency or corruption.

  • Durability

Durability ensures that once a transaction has been committed, its changes are permanently saved in the database. These changes remain intact even in the event of a system crash, power failure, or other unexpected errors.

Spring supports two types of transaction management:          
  • Programmatic Transaction Management
  • Declarative Transaction Management


Programmatic transaction management: 

            Programmatic transaction management means that you explicitly write transaction management code around your business logic. Although this approach provides greater flexibility and fine-grained control over transactions, it is more difficult to maintain and results in a significant amount of boilerplate code.

Example:--

onlineBooking() {
T1.start();
     checkAvailability();
T1.Commit();

T2.start();

     selectItems();
payment();
itemConfirmation();
T2.commit();
}

I will discuss this in next upcoming post in details.


Declarative transaction management:

               Declarative transaction management separates transaction management from the business logic. Transactions are managed using annotations or XML-based configuration, making the code cleaner, easier to maintain, and less error-prone.

We will discuss declarative transaction management in detail in an upcoming post.


Spring transaction propagation

         Propagation is the ability to decide how the business methods should be encapsulated in both logical or physical transactions.
  • REQUIRED
       The same transaction, if already exists, will be used in the current bean method execution context
if the transaction does not exist already then a new transaction will be created, if multiple methods configured with REQUIRED behavior then they will share the same transaction.

  • REQUIRES_NEW
      This behavior means a new transaction will always be created irrespective of whether a transaction exists or not each transaction runs independently
  • NESTED
        This behavior makes nested Spring transactions to use the same physical transaction but sets savepoints between nested invocations so inner transactions may also rollback independently of outer transactions.
  • MANDATORY
     This propagation states that an existing opened transaction must already exist. If not an exception will be thrown by the container.
  • NEVER
         This behavior states that an existing opened transaction must not already exist. If a transaction exists an exception will be thrown by the container. This is totally opposite to Mandatory propagation.

  • NOT_SUPPORTED
          The NOT_SUPPORTED behavior will execute outside of the scope of any transaction. If an opened transaction already exists it will be paused.
  • SUPPORTS
           The SUPPORTS behavior will execute in the scope of a transaction if an opened transaction already exists.If there isn't an already opened transaction the method will execute anyway but in a non-transaction way.


Spring transaction isolation level 

                 Isolation level defines how the changes made to some data repository by one transaction affect other simultaneous concurrent transactions, and also how and when that changed data becomes available to other transactions. When we define a transaction using the Spring framework we are also able to configure in which isolation level that same transaction will be executed.
  • READ_UNCOMMITTED 
        This isolation level states that a transaction may read data that  is still uncommitted by other transactions.
  • READ_COMMITTED
        This isolation level states that a transaction can't read data that is not yet committed by other transactions.
  • REPEATABLE_READ
        This isolation level states that if a transaction reads one record from the database multiple times the result of all those reading operations must always be the same.
  • SERIALIZABLE 
          This isolation level is the most restrictive of all isolation levels. Transactions are executed with locking at all levels (read, range and write locking) so they appear as if they were executed in a serialized way.


Related Posts:--
1) What is IOC Container in Spring? Difference between BeanFactory and ApplicationContext
2) Spring MVC with Hibernate CRUD Example
3) Spring Annotations and examples
4) Spring Configuration Metadata (XML, Annotation and Java)
5) Spring @Qualifier Annotation with example
6) What is Autowiring in Spring ? Explain Autowiring modes and limitations with examples

2 comments:

  1. This information you provided in the blog that is really unique I love it!! Thanks for sharing such a great blog Keep posting..
    Spring Interview Questions and Answers

    ReplyDelete
  2. i like this post
    BMW x5 M is one of the best choices, every version of it offers good acceleration and the best technology features wallpaperdig

    ReplyDelete