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:
If any operation within a transaction fails, the entire transaction is rolled back, ensuring that no partial changes are saved to the database.
- Consistency:
- Isolation:
- 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();
}
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
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
- NESTED
- MANDATORY
- NEVER
- NOT_SUPPORTED
- SUPPORTS
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
- READ_COMMITTED
- REPEATABLE_READ
- SERIALIZABLE
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


