DEV Community

Cover image for What I Discovered About @Transactional in Spring Boot 🔍
Priya
Priya

Posted on

5 3 2

What I Discovered About @Transactional in Spring Boot 🔍

Yesterday, I ran into a tricky problem while working on a REST API. Since I’m new to Spring and just starting to explore its features, I wasn't quite sure how to handle it. Here’s what I was trying to achieve:

  1. Delete a User: Simple enough, right? Just remove the user.
  2. Transfer Admin Rights: If the user being deleted is an admin and the only one, I had to give those admin rights to someone else.

The challenge: If anything goes wrong during these steps, I wanted everything to roll back, ensuring no partial changes or inconsistencies. Think of it as making sure everything behaves like a single, unbreakable atom ⚛.

What is Atomicity?
In database terms, atomicity is like a magic undo button. It’s part of the ACID properties that help keep things in order:

  • Atomicity: Either all parts of the task succeed, or none do.
  • Consistency: The database stays in a valid state.
  • Isolation: Each task is independent and doesn’t interfere with others.
  • Durability: Once a change is committed, it’s permanent.

Enough Theory! 🥱😴

So, how do you achieve atomicity in Spring Boot?

The magic lies in Spring's @Transactional annotation.

The Problem:

  1. Delete a user.
  2. If the user is an admin, transfer admin rights to another user.
  3. If anything goes wrong during these steps, make sure everything gets rolled back!

The Fix:

Here’s the secret sauce 🧑‍🍳: @Transactional

@Transactional
public void deleteUser(Long userId) {
    // Step 1: Delete the user
    // Step 2: Transfer admin rights if needed
    // If anything fails, rollback all operations
}
Enter fullscreen mode Exit fullscreen mode

With @Transactional, if any exception occurs, all operations in that method will be rolled back. It makes sure that both steps (deleting a user and transferring admin rights) are treated as one single atomic action!


What Can @Transactional Do?

1. Bundle Actions Together

Group multiple actions together so that if one fails, everything rolls back.

@Transactional
public void processUserDeletion(Long userId) {
    deleteUser(userId);         // Remove the user
    transferAdminRights(userId);   // Give admin rights to someone else
}
Enter fullscreen mode Exit fullscreen mode

2. Automatic Rollbacks

Automatically undo all changes if something goes wrong.

@Transactional
public void processUserDeletion(Long userId) {
    deleteUser(userId);          // Might fail
    transferAdminRights(userId); // Might fail
    // If any part fails, everything rolls back
}
Enter fullscreen mode Exit fullscreen mode

3. Set Isolation Levels

Control how transactions interact with each other. For a deeper dive into isolation levels, check out this article on Transaction Isolation Levels. 📚

@Transactional(isolation = Isolation.READ_COMMITTED)
public void checkUserStatus() {
    // Reads committed data only
}
Enter fullscreen mode Exit fullscreen mode

4. Customize Rollback Rules

Specify which exceptions should trigger a rollback.

@Transactional(rollbackFor = {SQLException.class}, noRollbackFor = {IOException.class})
public void processUser(Long userId) throws SQLException, IOException {
    // Rollback for SQL errors, not for IO errors
}
Enter fullscreen mode Exit fullscreen mode

5. Set Timeouts

Ensure transactions don’t run too long.

@Transactional(timeout = 10) // 10 seconds
public void performTimedOperation() {
    // Must complete in 10 seconds or roll back
}
Enter fullscreen mode Exit fullscreen mode

Wrap-Up

Using @Transactional helps keep your transactions smooth and reliable. If something goes wrong, everything will roll back to maintain data consistency!

Got questions or tips on Spring Boot transactions? Drop a comment below!

Happy coding! 😊

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (4)

Collapse
 
ashtad123 profile image
ASHTAD IRANI

short and precise !

Collapse
 
priya01 profile image
Priya

Thanks, Ashtad! Glad you found it helpful! 😊

Collapse
 
afzal_hussain_5aea6884a45 profile image
Afzal Hussain

Thanks for sharing. I shall try

Collapse
 
priya01 profile image
Priya

Thanks, Afzal! Let me know if you have any questions.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up