@Transactional(propagation = Propagation.NESTED) runs a method inside the caller's existing transaction but wraps it in a JDBC savepoint. If the nested method fails, Spring rolls back only to that savepoint, so the outer transaction can continue and still commit. If the outer transaction rolls back, everything, including the nested work, is rolled back.
NESTED is easy to confuse with REQUIRES_NEW. The difference is that NESTED uses the same physical transaction and connection, while REQUIRES_NEW opens a separate one. Support also depends on the transaction manager, which is the most common reason it does not behave as expected.
When a method with Propagation.NESTED is called, Spring checks whether a transaction is already active.
REQUIRED and starts a new transaction.The real commit happens only once, when the outermost transaction completes.
| Behaviour | REQUIRED | REQUIRES_NEW | NESTED |
|---|---|---|---|
| Physical transaction | Shared with caller | Separate, caller suspended | Shared with caller |
| Database connection | Same | A second connection | Same |
| Inner failure | Marks whole transaction rollback-only | Rolls back only the inner transaction | Rolls back to the savepoint |
| Outer rollback | Rolls back everything | Inner work already committed | Rolls back inner work too |
| Needs savepoint support | No | No | Yes |
Use NESTED when a step is optional or recoverable and you want to keep the rest of the transaction. Use REQUIRES_NEW when the inner work must survive even if the outer transaction fails, such as an audit record.
The following service imports a list of orders. A bad row should be skipped without losing the rows already processed.
Each call to importRow gets its own savepoint. A failing row rolls back to its savepoint, the exception is caught in importAll, and the loop continues. When importAll returns, one commit persists every successful row.
The two methods are in separate beans on purpose. Spring applies transactions through a proxy, so a call from one method to another in the same class bypasses the proxy and the NESTED setting is ignored.
Nested transactions need a resource that supports savepoints. In practice:
DataSourceTransactionManager (plain JDBC, JdbcTemplate, MyBatis) supports NESTED through JDBC savepoints.JpaTransactionManager supports it when the JPA dialect can reach the JDBC connection. Hibernate's dialect does, so it works with Spring Data JPA on a JDBC database. Hibernate's session state is not rolled back to the savepoint, so entities changed inside the failed nested call can stay dirty in the persistence context.JtaTransactionManager does not support savepoints for distributed transactions. Nested calls throw NestedTransactionNotSupportedException.The flag that controls this is nestedTransactionAllowed on AbstractPlatformTransactionManager. It is enabled for the JDBC and JPA managers; check it if you build a custom manager. The database must also support savepoints, which PostgreSQL, MySQL with InnoDB, Oracle and SQL Server do.
RuntimeException and Error trigger a rollback. Use rollbackFor for checked exceptions.flush() inside the nested method if you want constraint violations to surface there rather than at the outer commit.No. NESTED shares the caller's connection and transaction and uses a savepoint. REQUIRES_NEW suspends the caller and uses a second connection with an independent commit.
No. Releasing the savepoint does not commit anything. The data becomes durable only when the outermost transaction commits.
Your transaction manager does not allow nested transactions, which is typical with JtaTransactionManager. Switch to a JDBC or JPA transaction manager, or use REQUIRES_NEW.
Yes, with the default JpaTransactionManager and Hibernate on a JDBC database. Remember that the persistence context is not reverted after a savepoint rollback, so detach or reload affected entities.
Propagation.NESTED lets part of a transaction fail and roll back to a savepoint while the rest continues. It shares one physical transaction, so nothing is committed until the outer transaction ends. It requires savepoint support from the transaction manager and the database, and it must be called through a Spring proxy from a different bean. Choose it for recoverable steps, and choose REQUIRES_NEW when the inner work must commit independently.