DEV Community

Cover image for What I Discovered About @Transactional in Spring Boot ๐Ÿ”
Priya
Priya

Posted on

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! ๐Ÿ˜Š

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.