Hackforge Academy

Category: java

Introduction to Transaction Management

Published on 24 Jul 2026

Explanation

A transaction is a group of database operations executed as a single unit of work. Spring Boot manages transactions using the @Transactional annotation, ensuring that all operations either complete successfully or roll back if an error occurs.

Code:

@Service
public class StudentService {

    @Transactional
    public void saveStudent(Student student) {
        repository.save(student);
    }
}

Explanation

Spring transactions follow the ACID principles: Atomicity ensures all operations succeed or fail together, Consistency maintains valid data, Isolation prevents concurrent transaction conflicts, and Durability guarantees committed data is permanently stored.

Code:

A - Atomicity
C - Consistency
I - Isolation
D - Durability

Explanation

When an unchecked exception occurs inside a @Transactional method, Spring automatically rolls back all database changes. This prevents partial updates and keeps the database in a consistent state.

Code:

@Transactional
public void registerStudent(Student student) {
    repository.save(student);

    if (true) {
        throw new RuntimeException("Registration Failed");
    }
}

Explanation

Propagation defines how a transactional method behaves when another transaction already exists. Common options include REQUIRED, REQUIRES_NEW, SUPPORTS, MANDATORY, NEVER, NOT_SUPPORTED, and NESTED for different business scenarios.

Code:

@Transactional(
    propagation = Propagation.REQUIRES_NEW
)
public void saveAuditLog() {
    // Save audit record
}

Explanation

REQUIRED joins an existing transaction or creates a new one. REQUIRES_NEW always starts a new transaction. SUPPORTS uses an existing transaction if available, otherwise executes without one. Choosing the correct propagation improves reliability and business consistency.

Code:

REQUIRED      → Join or Create
REQUIRES_NEW  → Always New
SUPPORTS      → Join if Exists
MANDATORY     → Existing Required
NESTED        → Nested Transaction

🚀 Learn Spring Boot with real-world projects

💡 Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
⚛️

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community