What is @Transactional in Spring?

Why Interviewers Ask This

This question tests conceptual clarity. Interviewers want to hear a precise, confident definition before moving to more complex Spring Boot topics. It also reveals how well you can explain technical ideas to non-experts.

Answer

@Transactional is Spring's declarative transaction management annotation. When applied to a method or class, Spring wraps the execution in a database transaction — if the method completes successfully, the transaction is committed; if a RuntimeException is thrown, the transaction is rolled back. Usage: @Service public class TransferService { @Transactional public void transfer(Long fromId, Long toId, BigDecimal amount) { Account from = accountRepository.findById(fromId).orElseThrow(); Account to = accountRepository.findById(toId).orElseThrow(); from.debit(amount); to.credit(amount); accountRepository.save(from); accountRepository.save(to); // If any line throws a RuntimeException, entire transfer is rolled back } }. Key attributes: readOnly = true — optimization for read-only methods (disables dirty checking); rollbackFor = {CheckedException.class} — also rollback for checked exceptions (default: only RuntimeException); noRollbackFor = {BusinessException.class}; timeout = 30 — transaction timeout in seconds; propagation — behavior when a transaction already exists: REQUIRED (default — join existing or create new), REQUIRES_NEW (always create new, suspend outer), SUPPORTS, MANDATORY, NEVER, NOT_SUPPORTED, NESTED; isolation — transaction isolation level. Important: @Transactional only works when called externally (via Spring's proxy). Self-invocation (calling a @Transactional method from within the same class) bypasses the proxy. Spring Data JPA repositories: all CrudRepository/JpaRepository methods are @Transactional by default.

Common Mistake

Many candidates answer correctly but can't explain the 'why'. Always be prepared to justify your answer with a concrete example or use case from your Spring Boot experience.