Scrivio Interview library
InternalsIntermediate6 min read · updated 12 November 2026

How does Spring's Propagation.NESTED use savepoints, how does it differ from REQUIRES_NEW, and what pitfalls exist when using it with JPA and Hibernate?

Propagation.NESTED keeps running in the caller's physical transaction and sets a JDBC savepoint when the nested method starts; if the method throws, Spring rolls back to that savepoint and the outer transaction can carry on and commit. REQUIRES_NEW instead suspends the outer transaction and runs a separate transaction on a second connection that commits on its own. With JPA, NESTED is risky: support depends on the transaction manager and version, and rolling back to a savepoint does not roll back Hibernate's persistence context, so managed entities can disagree with the database.

Why this question is asked

Both propagation settings promise that a failure in one step does not have to ruin the whole operation, so they are easy to confuse. They work in very different ways, and choosing the wrong one gives subtle bugs: lost audit records, exhausted connection pools, or entities that hold values the database never saved. Interviewers ask this to see whether we know what Spring does at the JDBC level. In this answer, we will recap savepoints and propagation, follow NESTED and REQUIRES_NEW step by step, compare them, and then look at the JPA pitfalls and when each fits.

1A Quick Recap: Propagation and Savepoints

Propagation tells Spring what to do when a @Transactional method is called while a transaction is already active. The default, REQUIRED, joins the existing transaction. A savepoint is a marker inside a database transaction that we can roll back to without rolling back the whole transaction; in JDBC it is Connection.setSavepoint() and rollback(savepoint).

2How NESTED Works

When a method with Propagation.NESTED is called inside an existing transaction, Spring does not open a new connection. It calls setSavepoint() on the current connection and runs the method. If the method returns normally, Spring releases the savepoint and the work stays part of the outer transaction. If it throws, Spring rolls back to the savepoint, undoing only the nested work, and rethrows. The caller can catch the exception and continue; the outer transaction is still healthy and can commit. If there is no outer transaction, NESTED behaves like REQUIRED and starts one.

Java
@Service
class ImportService {
private final RowImporter importer;
ImportService(RowImporter importer) { this.importer = importer; }
@Transactional
public int importAll(List<Row> rows) {
int ok = 0;
for (Row r : rows) {
try { importer.importRow(r); ok++; } // SAVEPOINT ... RELEASE, or ROLLBACK TO SAVEPOINT
catch (InvalidRowException e) { log.warn("skipped {}", r.id()); }
}
return ok; // 998 of 1,000; one COMMIT for all of them
}
}
@Service
class RowImporter {
@Transactional(propagation = Propagation.NESTED)
public void importRow(Row r) { jdbc.update("INSERT INTO item ...", r.values()); validate(r); }
}

Notice that importRow is on a different bean. As with any @Transactional attribute, a call on this would bypass the proxy, and no savepoint would be set.

3How REQUIRES_NEW Differs

REQUIRES_NEW suspends the outer transaction: Spring unbinds its connection from the thread, takes a second connection from the pool, and starts a completely independent transaction. When the inner method finishes, that transaction commits or rolls back on its own, and the outer one resumes.

The difference shows when the outer transaction fails later. With NESTED, the nested work was only ever part of the outer transaction, so an outer rollback removes it too. With REQUIRES_NEW, the inner work already committed and stays. REQUIRES_NEW has its own dangers: each concurrent request holds two connections, so under load a pool of 10 can be exhausted by 5 requests waiting for their second connection, and if the inner transaction touches rows the outer one has locked, it waits for a transaction that is waiting for it, a deadlock the database cannot detect, because the outer transaction is not waiting on a database lock but on our own Java thread, which is waiting for the inner one.

NESTED versus REQUIRES_NEW
AspectNESTEDREQUIRES_NEW
Connectionsame connectionsecond connection from the pool
MechanismJDBC savepointseparate physical transaction
Inner failureroll back to savepoint, outer continuesinner rolls back, outer continues
Outer rolls back laterinner work rolled back tooinner work stays committed
Main risksJPA context not rolled backpool exhaustion, waits on outer's locks

4Which Transaction Managers Support NESTED

DataSourceTransactionManager, the one used with JdbcTemplate and plain JDBC, supports NESTED with JDBC savepoints out of the box. JpaTransactionManager can support it only when its JPA dialect can reach the underlying JDBC connection to create savepoints; whether that works depends on the Spring version, the dialect and configuration, so check your version. If it is not supported, Spring throws NestedTransactionNotSupportedException at the nested call rather than silently ignoring it. JtaTransactionManager generally does not support NESTED.

5The JPA Pitfall: The Persistence Context Is Not Rolled Back

Even where NESTED works with JPA, it rolls back only the database. Hibernate's persistence context, the first-level cache of managed entities in the current transaction, knows nothing about savepoints.

Java
@Transactional(propagation = Propagation.NESTED)
public void applyDiscount(Order order) {
order.setTotal(order.getTotal().subtract(TEN)); // managed entity changed in memory
em.flush(); // UPDATE sent to the database
throw new PricingException(); // Spring rolls back to the savepoint
}
// back in the outer transaction:
// order.getTotal() -> 90, still the changed value in memory
// database row -> 100, the savepoint rollback undid the UPDATE
// at outer commit, dirty checking may write 90 again

After the rollback to the savepoint, the database row is back to 100, but the entity still says 90 and Hibernate still considers it managed. When the outer transaction flushes, it may write the change the nested step was supposed to undo, or it may leave the entity inconsistent for later logic. Entities persisted inside the nested step remain in the context even though their rows were rolled back. Detaching or refreshing the affected entities can repair this, but it is fragile.

6When Each One Fits

NESTED is a good fit for best-effort sub-steps in JDBC batch work: import 1,000 rows in one transaction, skip the invalid ones, commit the rest together. For JPA code I would avoid it and either validate before writing, or use separate small transactions per item with REQUIRES_NEW or TransactionTemplate, accepting that each commits independently. REQUIRES_NEW is the right choice when the inner work must survive an outer rollback.

7Follow-up questions

When would REQUIRES_NEW be the right choice, for example audit logging?

Audit logging is the classic case. If a user tries a forbidden operation and the main transaction rolls back, we still want a record of the attempt. Writing the audit row in a REQUIRES_NEW method commits it independently, so the outer rollback does not remove it. The inner transaction should be short, touch only the audit table so it cannot wait on the outer transaction's locks, and the connection pool must be sized for two connections per request on that path. An alternative is to write the audit event after the outer transaction finishes, using a transaction synchronization callback.

What happens if the nested method throws a checked exception?

Spring's default rollback rules apply: unchecked exceptions and errors trigger the rollback to the savepoint, checked exceptions do not, unless rollbackFor says otherwise.

Can NESTED be used inside a REQUIRES_NEW transaction?

Yes. NESTED always sets its savepoint in whichever transaction is current, so inside a REQUIRES_NEW method it nests within that new transaction.

8Conclusion

In this answer, we saw that NESTED runs inside the outer physical transaction with a JDBC savepoint, rolls back only its own work on failure, and is rolled back with the outer transaction, while REQUIRES_NEW suspends the outer transaction and commits independently on a second connection, risking pool exhaustion and waits on the outer transaction's locks. DataSourceTransactionManager supports NESTED; with JPA support depends on the version, and the persistence context is not rolled back to the savepoint, so NESTED fits JDBC batch work and JPA code is better served by separate transactions.

What to say in the interviewAbout 90 seconds aloud

Open with

With Propagation.NESTED, Spring stays in the existing physical transaction, on the same connection, and calls setSavepoint when the nested method starts. If the method throws, Spring rolls back to the savepoint, so only the nested work is undone, and the caller can catch the exception and the outer transaction can still commit.

Then, if they want more

If the outer transaction rolls back later, the nested work goes with it. REQUIRES_NEW is different: it suspends the outer transaction, takes a second connection and runs an independent transaction that commits on its own, so its work survives an outer rollback.

The risks there are that each request holds two connections, which can exhaust the pool, and the inner transaction can block on locks the outer one holds. NESTED works out of the box with DataSourceTransactionManager.

With JpaTransactionManager it depends on the dialect and version, and the bigger pitfall is that rolling back to the savepoint doesn't roll back Hibernate's persistence context. Managed entities keep their changed values, and dirty checking may even write them again at the outer commit.

So I'd use NESTED for best-effort steps in JDBC batch work, like skipping invalid rows in an import, and for JPA I'd prefer separate transactions or validating before writing. REQUIRES_NEW fits things like audit logs that must survive a rollback.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How does Spring's Propagation.NESTED use savepoints, how does it differ from...": https://scrivio-interview-library.netlify.app/databases/acid-and-isolation/8db5238fd4/