Java · OOP, JVM and backend craft

How do transactions work in Spring?

MediumtransactionsspringacidUpdated Oct 2026
Quiz this track

Short answer

A transaction groups work into an atomic unit with commit or rollback. Spring manages transactions through proxies, usually with @Transactional, so boundaries and propagation must be understood.

A transaction provides atomicity, consistency, isolation and durability. Spring's @Transactional starts a transaction before the method and commits or rolls back after it. The annotation works through a proxy, so calls that bypass the proxy do not get transactional behaviour.

java
@Service
public class TransferService {
    @Transactional
    public void transfer(long fromId, long toId, long amount) {
        Account from = accounts.findById(fromId).orElseThrow();
        Account to = accounts.findById(toId).orElseThrow();
        from.withdraw(amount);
        to.deposit(amount);
    }
}
Both updates commit together. A failure after the withdrawal rolls it back.

Settings that change behaviour

  • Propagation: REQUIRED joins an existing transaction; REQUIRES_NEW suspends it
  • Isolation: READ_COMMITTED is common; higher levels reduce anomalies but increase contention
  • Rollback: unchecked exceptions roll back by default, checked exceptions do not unless configured
  • readOnly is a hint that can skip dirty checking and route to a replica
  • Self-invocation bypasses the proxy, so an internal call to a transactional method is not transactional

Keep transactions short and avoid remote calls inside them, because a slow dependency holds locks and connections. Put the transactional boundary in the service layer, not the controller, and be explicit about rollback rules when checked exceptions are part of the contract.

A transaction is not a substitute for concurrency control. Two transactions can still overwrite each other under read committed. Use optimistic locking with a version field or pessimistic locking when lost updates matter.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1Why does a self-invoked @Transactional method not start a transaction?
  2. 2Which exceptions trigger rollback by default?
  3. 3What is the difference between REQUIRED and REQUIRES_NEW?
  4. 4How do you prevent lost updates?

Say it in the interview

Spring manages transactions through a proxy around the bean. @Transactional starts a transaction, then commits or rolls back when the method returns or throws. By default only unchecked exceptions roll back, and self-invocation bypasses the proxy. I keep transactions in the service layer, short and free of remote calls, and I use optimistic locking when concurrent updates could overwrite each other.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Java mock interview and get scored feedback on depth, clarity and confidence.