Search Tutorials


Spring Boot Transactions - Understanding Transaction Isolation | JavaInUse

Spring Boot Transactions - Understanding Transaction Propagation

In previous tutorial - Spring Boot Transaction Management Example we saw what are transactions and implemented declarative transaction management. Also in previous tutorial we had implemented the various transaction propagation types. In this tutorial we will be understanding what is transaction isolation and its different types.

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

Lets Begin-

What is Transaction Isolation?
Transaction Isolation defines the database state when 2 transactions concurrently act on the same database entity. It involves locking of database records.
Spring Boot Transaction Isolation Example
The following are the types of Transaction Isolation Levels-
  • REPEATABLE_READ
    For MySQL the default isolation level is REPEATABLE_READ.
    Spring Boot Transaction Isolation - REPEATABLE_READ
  • SERIALIZABLE

    Spring Boot Transaction Isolation - SERIALIZABLE
  • READ_UNCOMMITTED

    Spring Boot Transaction Isolation - READ_UNCOMMITTED
  • READ_COMMITTED

    Spring Boot Transaction Isolation - READ_COMMITTED
When using Transaction Isolation with Spring Boot, the default transaction isolation taken is that of the underlying database. So for our spring boot application the default transaction isolation will be REPEATABLE_READ since we are using MySQL database. We can change the transaction isolation level as follows-
 	 package com.javainuse.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import com.javainuse.model.Employee;
import com.javainuse.model.EmployeeHealthInsurance;
import com.javainuse.service.EmployeeService;
import com.javainuse.service.HealthInsuranceService;
import com.javainuse.service.OrganizationService;

@Service
public class OrganzationServiceImpl implements OrganizationService {

@Autowired
EmployeeService employeeService;

@Autowired
HealthInsuranceService healthInsuranceService;

@Override
@Transactional(rollbackFor = InvalidInsuranceAmountException.class, isolation = Isolation.SERIALIZABLE)
public void joinOrganization(Employee employee, EmployeeHealthInsurance employeeHealthInsurance)
throws InvalidInsuranceAmountException {
employeeService.insertEmployee(employee);
try {
healthInsuranceService.registerEmployeeHealthInsurance(employeeHealthInsurance);
} catch (InvalidInsuranceAmountException e) {
throw new InvalidInsuranceAmountException("Exception is thrown");
}
}

@Override
public void leaveOrganization(Employee employee, EmployeeHealthInsurance employeeHealthInsurance) {
employeeService.deleteEmployeeById(employee.getEmpId());
healthInsuranceService.deleteEmployeeHealthInsuranceById(employeeHealthInsurance.getEmpId());
}
}