Card 02/ 07

ExampleDifficulty: Intermediate1 min

Two Writes, Committed Together

bookTrip makes two separate save() calls. Both land in the database together, because @Transactional treats the whole method as one unit — this is the ordinary, working case the next two cards are both about breaking.

java
@Transactional
void bookTrip() {
    repo.save(new TripEntity("booked"));
    repo.save(new TripEntity("booked"));
}
text
both rows committed together, count = 2
run in a container — neither save is visible until the method returns successfully

Nothing about either individual save() call commits anything on its own — the transaction the method opened is what actually commits, once, after both writes have succeeded.