SPRING CONCEPTS

MongoDB transactions and rollback

Spring Concepts / MongoDB transactions and rollback

A transaction coordinates related writes so they commit together or roll back.

Understand the idea

The MongoDB lessons use a replica-set deployment, one database factory and a registered MongoTransactionManager. Transaction interception wraps calls through the service proxy. With ordinary defaults unchecked exceptions and Error cause rollback; checked exceptions require a matching rule such as rollbackFor. Catching an exception inside the service can change the observed outcome.

A concrete example

@Transactional(rollbackFor = IOException.class)

In the application

See the independently runnable examples in Stage 34, Stage 35.

The annotation alone does not enable MongoDB transactions; the deployment and transaction manager must support them.

See it in a working stage

Related concepts

Read the official documentation ↗
Java 21 · Spring Boot 4.1.1 · Edupoly