Transaction Management is to manage the transaction operations in a way that maintains
the consistency and integrity of data within the database.
A few examples of transaction management involve managing database transaction operations such as -
Deleting a customer account from database.
Inserting a customer account record in the database.
Depositing an amount in an account.
Withdrawing an amount from an account.
Transaction Management can be simply explained by performing the next few steps -
The transaction operations should be atomic i.e. either of these operations
should happen entirely or nothing at all.
The transaction operations trying to access the shared data at the same time, should only be executed in isolation. i.e. one transaction at a time.
In case of any exceptional or erroneous situation taking place(while any of the transaction operations were being performed),
the effect of these operations should be rolled back
to maintain the consistency of data and not leave any anomalous data in the database.
Basic elements of a transaction
Every transaction has properties that allow us to configure it within our application.
Before we understand ways to manage transaction operations using Spring Framework,
we should understand the important properties associated with each transaction.
Some of the transaction properties are -
propagation, isolation, read-only, roll back for exception, timeout .
Using the propagation property of transaction, we can define the behaviour of a transaction i.e.
we can specify a method of a class which is going to perform the transaction operation and have this method executed
as a part of a new transaction each time it is called or part of an existing transaction.
Spring Framework provides us propagation constants through org.springframework.transaction.annotation.Propagation enumeration,
using which we can define the propagation property of a transaction.
Specifies a method which should be executed as a part of a transaction.
Searched an existing transaction and executes the associated method within it or creates a new transaction.
Specifies a method which should be executed in a new transaction.
Specifies a method which can be executed inside or outside a transaction.
Specifies a method should never be executed in a transaction. If an transaction exists, an exception is thrown.
Specifies a method should not be executed in a transaction. If an transaction exists, it is suspended until method finishes its execution.
Using the isolation property of transaction, we can define different ways of configuring the execution of concurrent transactions
within an application which helps us to maintain data consistency and integrity
i.e. we can specify whether or not a particular transaction should only be executed in isolation by
having the complete access control over the data it needs(without sharing this data with any other transaction until it completes its execution).
Spring Framework provides us isolation constants through org.springframework.transaction.annotation.Isolation enumeration,
using which we can define the isolation property of a transaction.
Specifies a default isolation level defined in the relational databases.
Specifies that a transaction cannot read or change data which is still acquired by an existing transaction.
Specifies that a transaction can read or change the data which is still acquired by an existingtransaction.
This constant helps us to acquire the lock on the data a transaction needs.
Specifies that a transaction should be executed in a proper isolation, without the intrusion from another transaction.
Using the read-only property of transaction, we can specify whether a particular transaction is going to modify the data or not.
This property of transaction takes a boolean value.
Using the rollback-for property of transaction, we can specify a single or multiple exception classes. When any of these exceptions is thrown, the associated transaction is going to rolled back(without affecting the database consistency and integrity).
Ways to perform transaction management
There are two ways to perform transaction management -
Programmatic Transaction Management - Programmatic Transaction Management involves writing the code performing the transaction management i.e.
writing the code to begin the transaction until its end, to control the execution of transaction in specific situations,
to handle the exception while the transaction
is being performed and to
rollback the transaction in specific situations etc.
To perform the transaction management programmatically, Spring Framework provide us a org.springframework.transaction.support.TransactionTemplate class.
Declarative Transaction Management - Unlike the programmatic transaction management approach, the declarative transaction management
approach doesn't require us to write or modify the source code to perform transaction management.
declarative Transaction Management involves performing the transaction management through the configuration xml file i.e.
you can declared all the transaction elements within the configuration xml file of your project.
This configuration xml file is maintained by the Spring Container in Spring Framework.
Note : Declarative transaction management is applied to methods which are performing the transaction operations.