Spring Boot Transaction Management - Transaction Rollback Example
For an application transaction if any action fails then all other actions gets rolled back. Previous Transaction Management Example we had tested the rollback by throwing an unchecked exception.
However in real time scenarios it is the checked exception that gets thrown. These are business exceptions based on some logic
So how will our transactions behave in case of Checked Exceptions? In case of checked exceptions the previously executed transactions do not get rolled back automatically even if we have used transaction annotation. We need to inform the application how to handle roll back in event of checked exception. This is achieved using the RollbackFor annotation.
Spring Boot Transaction Management - Table of Contents
Spring Boot Transaction Management Example Spring Boot Transactions - Understanding Transaction Propagation Spring Boot Transactions - Understanding Transaction Rollbacks Spring Boot Transactions - Understanding Transaction Isolation
Video
This tutorial is explained in the below Youtube Video.Lets Begin-
We will be modifying the code we had created previously for Transaction Management.We will create a custom checked exception called InvalidInsuranceAmountException. According to our business logic if the insurance coverage amount is less than zero then this exception should get thrown. The maven project will be as follows -
Create the Custom Exception as follows-
package com.javainuse.service.impl; public class InvalidInsuranceAmountException extends Exception { private static final long serialVersionUID = 1L; public InvalidInsuranceAmountException(String cause) { super(cause); } }The HealthInsuranceService interface throws this exception for the registerEmployeeHealthInsurance method.
package com.javainuse.service; import com.javainuse.model.EmployeeHealthInsurance; import com.javainuse.service.impl.InvalidInsuranceAmountException; public interface HealthInsuranceService { void registerEmployeeHealthInsurance(EmployeeHealthInsurance employeeHealthInsurance) throws InvalidInsuranceAmountException; void deleteEmployeeHealthInsuranceById(String empid); }In the HealthInsuranceServiceImpl class for the registerEmployeeHealthInsurance we put a check for verifying if the coverage amount is less than zero. If it is then we throw the InvalidInsuranceAmountException.
package com.javainuse.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.javainuse.dao.HealthInsuranceDao; import com.javainuse.model.EmployeeHealthInsurance; import com.javainuse.service.HealthInsuranceService; @Service @Transactional public class HealthInsuranceServiceImpl implements HealthInsuranceService { @Autowired HealthInsuranceDao healthInsuranceDao; @Override public void registerEmployeeHealthInsurance(EmployeeHealthInsurance employeeHealthInsurance) throws InvalidInsuranceAmountException { if (employeeHealthInsurance.getCoverageAmount() < 0) { throw new InvalidInsuranceAmountException("Coverage Amount Should not be negative"); } healthInsuranceDao.registerEmployeeHealthInsurance(employeeHealthInsurance); } @Override public void deleteEmployeeHealthInsuranceById(String empid) { healthInsuranceDao.deleteEmployeeHealthInsuranceById(empid); } }