@Transactional lets Spring Data MongoDB run several MongoDB operations as one multi-document ACID transaction, but only when you register a MongoTransactionManager bean and run MongoDB against a replica set or sharded cluster. Without both, the annotation does nothing useful for MongoDB: Spring Boot does not auto-configure the Mongo transaction manager, so your writes run one by one and are never rolled back together.
Single-document writes in MongoDB are already atomic. You only need @Transactional when one business operation must update several documents or collections and either all changes persist or none do.
The annotation itself is generic Spring. It tells Spring's transaction infrastructure to open a transaction before the method starts, commit it when the method returns, and roll it back when a runtime exception escapes. The thing that makes it work with MongoDB is the PlatformTransactionManager that Spring looks up.
For MongoDB that manager is MongoTransactionManager (blocking code) or ReactiveMongoTransactionManager (reactive code). It starts a ClientSession, begins a MongoDB transaction on it, and binds the session to the current thread so MongoTemplate and Spring Data repositories automatically use it.
MongoDB transactions have infrastructure prerequisites that no annotation can fix.
| Requirement | Detail |
|---|---|
| Deployment type | Replica set (MongoDB 4.0+) or sharded cluster (4.2+). A standalone mongod rejects transactions. |
| Driver and Spring | Spring Data MongoDB 2.1+ for imperative transactions. Current Spring Boot 3.x uses Spring Data MongoDB 4.x. |
| Transaction manager | You must declare a MongoTransactionManager bean yourself. |
| Collections | Before MongoDB 4.4 a collection had to exist before a transaction could write to it. From 4.4 collections and indexes can be created inside a transaction. |
| Time limit | Transactions abort after 60 seconds by default (transactionLifetimeLimitSeconds). |
For local development, run a single-node replica set. With Docker:
Then point the application at it with spring.data.mongodb.uri=mongodb://localhost:27017/shop?replicaSet=rs0.
Declare the bean in a configuration class. Spring Data MongoDB then makes @Transactional aware of MongoDB.
If your project also uses JPA or JDBC, you now have two transaction managers. Give each one a name and select it with @Transactional("transactionManager") so Spring does not pick the wrong one.
The classic case is moving money between two accounts. Both updates must succeed together.
If IllegalStateException is thrown, or either save fails, MongoDB discards every write made inside the method. The reads inside the method also see a consistent snapshot of the data.
Spring's defaults apply to MongoDB as well.
RuntimeException and Error) trigger a rollback. For a checked exception use @Transactional(rollbackFor = Exception.class).@Transactional method from another method of the same class bypasses the Spring proxy, so no transaction starts. Move the method to another bean.public methods of Spring-managed beans. It is ignored on private methods.UncategorizedMongoDbException containing a TransientTransactionError label. Retrying the whole method is the correct response.When you need retries or finer control, use TransactionTemplate with the same manager:
For Spring WebFlux and reactive repositories, declare a ReactiveMongoTransactionManager instead. @Transactional then works on methods that return Mono or Flux, and the transaction is bound to the Reactor subscriber context rather than a thread.
No. A standalone MongoDB server rejects multi-document transactions, so you need at least a single-node replica set, even in development.
No. Updates to one document are atomic in MongoDB. Embedding related data in one document is often better than using a transaction.
The usual causes are a missing MongoTransactionManager bean, a self-invocation that skips the proxy, or a checked exception that is not listed in rollbackFor.
No. Spring Boot configures the Mongo client and template, but you must define the MongoTransactionManager bean yourself.
@Transactional gives MongoDB multi-document ACID behaviour in Spring only when three things are true: MongoDB runs as a replica set or sharded cluster, a MongoTransactionManager bean exists, and the call passes through the Spring proxy. Use it for operations that touch several documents, keep the methods short, and prefer a well-designed single document where atomicity alone is the goal.