Card 01/ 07

ConceptDifficulty: Intermediate1 min

What @Transactional Actually Wraps

@Transactional on a method starts a database transaction before the method runs, and commits it — or rolls it back — when the method returns or throws. Not the HTTP request that triggered it, and not anything a caller does afterward: the method, and only for as long as it's executing.

java
@Transactional
void bookRide(RideRequest request) {
    ride = repo.save(new Ride(request));
    paymentService.charge(request.riderId(), fare);
    // if charge() throws, the save() above rolls back too
}

It works through a proxy — the same mechanism spring-core covers for scoped beans. Every call to bookRide actually arrives at the proxy first, which opens the transaction, delegates to the real method, and closes the transaction based on what happens.