Video in progress

Strong Junior 🌱 Spring

What happens if you put @Transactional on a private method?

Nothing — no transaction is opened. Spring works through a proxy, and the proxy only intercepts public methods called from outside (in Spring 6 with CGLIB, protected/package too). A private method is completely invisible to the proxy.

📖 Answer

@Service
public class BonusService {
    public void award(long userId) {
        addPoints(userId);
    }

    @Transactional // has no effect
    private void addPoints(long userId) { ... }
}

Two reasons stack up here:

  1. the proxy can't override a private method;
  2. addPoints is called from inside award — that's this.addPoints(), an internal call that bypasses the proxy.

You get no warning at all — the code works, just without a transaction. You find out when half-written data shows up in the database.

Move the annotation to the public method that's called from outside:

@Transactional
public void award(long userId) {
    addPoints(userId); // now it runs inside the same transaction
}

IntelliJ IDEA often highlights spots like this in yellow — don't ignore it.