Working with the @Transactional Annotation in Spring Framework

Alexander Obregon
3 min readAug 11, 2023

--

Image Source

Introduction

The Spring Framework has become a leading choice among developers when it comes to creating enterprise-grade applications, and for good reason. It provides a comprehensive programming model and configuration model for modern Java-based applications, making development easier and more efficient.

One of the features that make Spring stand out is its support for transaction management. Transactions are fundamental to any enterprise application dealing with persistent storage. It ensures data consistency and integrity.

Spring Framework offers a powerful and easy-to-use transaction management abstraction using the @Transactional annotation. In this post, we'll explore what @Transactional is and how to use it effectively.

Understanding Transactions

Before diving into @Transactional, let's take a brief moment to understand transactions. In the context of databases, a transaction is a set of operations that are executed as a single unit of work. If all the operations are successful, the transaction is committed, and all changes are saved in the database. If any operation fails, the transaction is rolled back, and all changes are undone.

Transactions are governed by the ACID principles: Atomicity, Consistency, Isolation, and Durability. These principles ensure that data remains consistent and reliable even in the face of failures.

The @Transactional Annotation

Spring’s @Transactional annotation is used to indicate that a method or class should be executed within a transaction context. The annotation-based transaction management is a form of declarative transaction management in Spring.

Consider the following code snippet:

import org.springframework.transaction.annotation.Transactional;

@Service
public class EmployeeService {

@Autowired
private EmployeeRepository employeeRepository;

@Transactional
public void addEmployee(Employee employee) {
employeeRepository.save(employee);
}
}

Here, addEmployee() method is annotated with @Transactional. This means that if anything goes wrong within the addEmployee() method and an exception is thrown, the transaction will be rolled back, and no data will be saved in the database.

Propagation and Isolation Levels

Spring’s @Transactional provides a set of properties that you can use to customize the transaction behavior. Two of the most important ones are propagation and isolation.

Propagation

Propagation defines how transactions relate to each other. There are several types of propagation behaviors, but here we’ll focus on three: REQUIRED, REQUIRES_NEW, and SUPPORTS.

@Transactional(propagation = Propagation.REQUIRED)
public void addEmployee(Employee employee) {
employeeRepository.save(employee);
}

In this example, the addEmployee() method will run within a transaction. If there is an existing transaction, this method will join that transaction. If there is no transaction, a new one will be created.

Isolation

Isolation levels define the degree to which a transaction must be isolated from the data modifications made by any other transaction.

@Transactional(isolation = Isolation.SERIALIZABLE)
public void addEmployee(Employee employee) {
employeeRepository.save(employee);
}

In this example, we’ve set the isolation level to SERIALIZABLE, which provides the highest level of isolation.

Rollback For Specific Exceptions

By default, Spring rolls back the transaction for unchecked exceptions (a subclass of RuntimeException) and for Error. However, we can define the rollback rules for specific exceptions.

@Transactional(rollbackFor = EmployeeNotFoundException.class)
public Employee getEmployee(Long id) throws EmployeeNotFoundException {
return employeeRepository.findById(id)
.orElseThrow(() -> new EmployeeNotFoundException("Employee not found"));
}

In this example, we’ve set the transaction to roll back when an EmployeeNotFoundException is thrown.

Conclusion

Spring’s @Transactional annotation is an invaluable tool for simplifying transaction management in enterprise applications. It provides an array of customizable properties to suit various application needs. However, understanding its mechanics is crucial to avoid data inconsistencies and other complexities. Armed with the insights provided in this blog post, you are now better positioned to effectively utilize @Transactional annotation in your Spring applications, while being aware of the potential pitfalls and best practices.

  1. Spring Framework Documentation — Transaction Management
  2. Spring Framework Reference — @Transactional Annotation
  3. Baeldung — Spring @Transactional — A Comprehensive Guide
Spring Boot icon by Icons8

--

--

Alexander Obregon

Software Engineer, fervent coder & writer. Devoted to learning & assisting others. Connect on LinkedIn: https://www.linkedin.com/in/alexander-obregon-97849b229/