In previous post, explained the CRUD operation of Spring with Hibernate, this article we can discuss about Spring Transaction Management with simple examples.
A transaction is a logical unit of work that contains one or more statements. A transaction is an atomic unit. The effects of all the statements in a transaction can be either all committed or all rolled back.
See the below diagram to show the transaction management,
A transaction is a logical unit of work that contains one or more statements. A transaction is an atomic unit. The effects of all the statements in a transaction can be either all committed or all rolled back.
See the below diagram to show the transaction management,
Transaction management is an important part of enterprise applications to ensure data integrity and consistency.
The concept of transactions can be described as ACID(Atomicity, Consistency, Isolation, Durability) property:
- Atomicity:
If one part of a transaction fails then entire transaction fails.
- Consistency:
- Isolation:
- Durability:
Spring supports two types of transaction management:
- Programmatic Transaction Management
- Declarative Transaction Management
Programmatic transaction management:
Programmatic means you have transaction management code surrounding your business code. This gives extreme flexibility, but is difficult to maintain and, well, boilerplate.
Example:--
onlineBooking() {
T1.start();
checkAvailability();
T1.Commit();
T2.start();
selectItems();
payment();
itemConfirmation();
T2.commit();
}
Declarative transaction management:
Transaction management is separated from business code and only annotations or XML based configurations are used to manage the transactions.
I will discuss this in upcoming post in details.
Next:
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