@Transactional — Interview Answer — Spring Technical Interview Questions & Answers

Model answer covering propagation and rollback rules.

@Transactional — Interview Answer

  • Model answer: @Transactional makes a method a single database unit of work.
  • On runtime failure, changes can roll back together.
  • I place it on service methods that update multiple records.
  • I can mention readOnly and propagation if they ask deeper.
  • Use a transfer-money style example.

Technical Interview Tip: All-or-nothing update example.

Code Examples for @Transactional — Interview Answer

@Transactional — Interview Answer — working example

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

@Service
class TransferService {
  private final AccountRepository accounts;

  TransferService(AccountRepository accounts) {
    this.accounts = accounts;
  }

  @Transactional
  public void transfer(long fromId, long toId, long amount) {
    Account from = accounts.findById(fromId).orElseThrow();
    Account to = accounts.findById(toId).orElseThrow();
    from.debit(amount);
    to.credit(amount);
  }
}

@Transactional — Interview Answer — @Transactional wraps a method in a DB transaction — rollback on runtime failures by default.

How to Explain @Transactional — Interview Answer to an Interviewer

What is @Transactional — Interview Answer (sb)?

Used for a short @Transactional — Interview Answer sb example a small REST app Not just the heading

Give one small example of @Transactional — Interview Answer (sb).

the smallest @Transactional — Interview Answer sb example you can type Model answer covering propagation and rollback rules

What mistake do beginners make with @Transactional — Interview Answer (sb)?

only saying “@Transactional — Interview Answer sb” with no example Show the fix

Where do you use @Transactional — Interview Answer (sb)?

a small REST app a short @Transactional — Interview Answer sb example