What does the @Transactional annotation do in Spring?

@Transactional marks a method, class, or interface as running within a database transaction boundary, so that all the database operations performed inside it are treated as a single atomic unit -- either everything commits together, or, if an exception occurs, everything rolls back together.

Key Points

Example

A service method that debits one account and credits another is annotated with @Transactional so that if the credit step throws an exception after the debit already ran, Spring rolls back the debit too, leaving the accounts in a consistent state.

@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);
    }
}

Interview Tip

A concise interview answer is

@Transactional wraps a method's database operations in a single atomic transaction using a Spring AOP proxy -- if an unchecked exception is thrown, everything rolls back; otherwise it all commits together. A classic use case is a funds transfer, where debiting one account and crediting another must succeed or fail as one unit.

Last updated: 11 November 2026

Previous question: How do you write a query method for sorting in Spring Data JPA?

Next question: What is the difference between FetchType.EAGER and FetchType.LAZY?

More Spring Data JPA interview questions

All Spring Data JPA questions