Updated April 7, 2023
Definition of Spring Boot Transaction Management
In spring boot or any other application transaction management is very important aspect, because it is related to the RDBMS part which is responsible to be taken care of the data integrity and consistency in general. We can define a transaction as the single unit of work, which define some task related to transaction in the database, it could be anything like, fetching of object, updating, creating, deleting, and many more. So in order to ensure the integrity of data we use transaction management in spring boot so that no data can be loss. Spring provides us annotation to handle the transaction, in the coming section of the tutorial we will see its internal working and usage for better understating and make the application more reliable for user to use.
Syntax:
As we discussed that it is a mechanism to handle the database transaction in the application, which ensure us the data consistency if any of the fault occur while running the code. Let’s take a closer look at the syntax to get better understating see below;
@Transactional
public return_type method_name() {
// logic goes here ..//
}
In the above syntax we are trying to use @Transactional annotation which will help us to provide the data integrity. let's taken an closer look at the practice syntax for better understating see below;
e.g. :
@Transactional
public void test() {
// logic goes here ..//
}
How to perform transaction management in Spring boot?
As we already discussed that transaction management is very important mechanism that need to be there in the application when we want to ensure the data integrity and consistency to the user data. By the help of this, we can handle the error and fault while running the code. @Transactional is always used above the method to ensure that this code while revert all the changes of any kind of error or exception occur while running it. In the spring boot application, we write and use this @Transactional annotation with the business logic which is data critical for us. In this section, we will see one practice example without @Transactional annotation which will explain how the data fails. Let’s get started;
Transactional management, in general, can be define by the ACID properties which are as follows;
1) Atomicity: This property define that the transaction should be a success or nothing at all. This means if the expectation occurs while executing the code it should rollback the previous changes as well. If the code is success, then nothing to worry.
2) Consistency : This property define the data consistency, which means we should maintain the constraints in the database in such a way that the data base should be in consistent state and does not after before or after the transaction.
3) Isolation: This property define the independency of the transaction which means execution of one transaction should not affect the other one. they should be independent.
4) Durability: This properties define, once the transaction is completed then its changes should be store and should be written to the disk, even in the case of system failure also.
Now we will see one sample transaction flow chart which will define how it works, it will also explain the steps in which it goes while executing the business-critical logic for us see below;
1) In the first step the transaction is being and it will start executing the business logic.
2) Suppose we want to add a new employee entry into the database, while doing the employee creating we are making entry into these tables for their information.
3) So in this steps entries should be created in all three tables or anything at all in case of failure.
4) In the end transaction would be end.
5) NOTE: if the transaction is successful then we should entries in all the tables, but in case of failure or any exception occur then t should rollback from the tables.
Below see the flow chart for reference for better understating;
Examples
Steps should be taken while using transactional management in spring boot application see below;
1) First we need to have the spring boot application in place, create it using spring initializer by putting all the details and import this inside the editor after extraction, below find the reference URL:
URL:
https://start.spring.io/
2) To make this run we would require the entity in order to create the student object inside the database, below is the code for student class;
e.g.:
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.time.LocalDateTime;
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private Long id;
@Column(name = "STUDENT_NAME")
private String name;
@Column(name = "ROLL_NO")
private int rollno;
@Column(name = "CITY")
private String city;
@Column(name = "DEP")
private String dep;
}
3) In this step we will store the object of a student into the database for this we will create one service which will contain the create method for us. for reference see below code;
e.g.:
@Service
public class StudentService {
@Autowired
private StudentRepository studentRepository;
@Transactional
public void create(Student student){
studentRepository.save(student);
System. out.println("Transaction successfull.");
}
}
Annotate the cerate student method with @Transactional annotation and it handle the after process for us.
4) In this method we will create new repository interface which will handle the database operation for us, like any transaction for reference see the below code.
e.g.:
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.time.LocalDateTime;
import java.util.List;
@Repository
public interface StudentRepository extends JpaRepository<Student, String> {
}
5) run the above application and it should run without any error and print the below output for us, try to call the method using any rest tool,
Output:
Conclusion
As we have seen in the tutorial, it is very important to annotate our method using the @Transactional annotation which is responsible to executing the business-critical logic for us, it will help and prevent us from data inconsistency issues later, it is very easy to use a handle in spring boot.
Recommended Articles
This is a guide to Spring Boot Transaction Management. Here we discuss definition, parameters, syntax, How to perform transaction management in Spring boot? examples with code implementation. You may also have a look at the following articles to learn more –