0
0
SpringbootConceptBeginner · 3 min read

@Transactional in Spring: What It Is and How It Works

@Transactional is a Spring annotation that manages database transactions automatically. It ensures that a group of operations either all succeed or all fail, keeping data consistent.
⚙️

How It Works

Imagine you are paying for groceries and want to make sure all items are scanned and paid for before leaving the store. If something goes wrong, you want to cancel the whole purchase instead of paying for only some items. @Transactional works similarly for database operations.

When you mark a method with @Transactional, Spring starts a transaction before the method runs. If everything inside the method finishes without errors, Spring commits the transaction, saving all changes. But if an error happens, Spring rolls back the transaction, undoing all changes made during that method call. This keeps your data safe and consistent.

💻

Example

This example shows a simple service method that transfers money between two accounts. The @Transactional annotation ensures both debit and credit happen together or not at all.

java
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BankService {

    @Transactional
    public void transferMoney(Account from, Account to, double amount) {
        from.debit(amount);
        to.credit(amount);
        // If an exception occurs here, both debit and credit are undone
    }
}

class Account {
    private double balance;

    public void debit(double amount) {
        if (balance < amount) {
            throw new RuntimeException("Insufficient funds");
        }
        balance -= amount;
    }

    public void credit(double amount) {
        balance += amount;
    }
}
Output
If transferMoney completes successfully, balances update; if an exception occurs, no changes are saved.
🎯

When to Use

Use @Transactional when you have multiple database operations that must all succeed or fail together. This is common in financial apps, order processing, or any case where partial updates could cause errors or inconsistent data.

For example, when creating an order, you might save the order details, update inventory, and charge the customer. If any step fails, @Transactional rolls back all changes to keep your data reliable.

Key Points

  • Atomicity: All operations inside a @Transactional method succeed or fail as one.
  • Automatic rollback: Spring undoes changes if an error occurs.
  • Declarative: Just add the annotation; no manual transaction code needed.
  • Supports propagation: Transactions can join or start new ones as needed.

Key Takeaways

@Transactional ensures database operations are all-or-nothing to keep data consistent.
Use it when multiple related database changes must succeed together.
Spring handles starting, committing, and rolling back transactions automatically.
If an error happens inside a @Transactional method, all changes are undone.
It simplifies transaction management by using a simple annotation.