Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

A method annotated @Transactional(propagation = NESTED) works with JdbcTemplate but throws NestedTransactionNotSupportedException after the team moves to JPA with JpaTransactionManager. Why, and what are the alternatives?

NESTED is implemented with JDBC savepoints: Spring sets a savepoint on the connection and rolls back to it if the nested method fails. That is safe for JdbcTemplate, but with JPA the EntityManager holds its own in-memory state, and Hibernate cannot roll its persistence context back to a savepoint, so JpaTransactionManager supports savepoints only for JDBC access and not for JPA operations; depending on the JPA dialect and version it refuses with NestedTransactionNotSupportedException. The alternatives are REQUIRES_NEW for independent work, restructuring so we validate before writing, or explicit savepoints only around pure JDBC work.

Why this question is asked

Savepoints are a neat tool: try a piece of work, and if it fails, undo just that piece and carry on. Teams that relied on NESTED with plain JDBC are often surprised when it breaks after moving to JPA, because nothing else in the transaction setup changed. Interviewers ask this to see whether we understand that JPA adds a second layer of state, the persistence context, on top of the database connection, and that the database's undo button does not reach it. In this answer, we will recap how NESTED works with JDBC, see why JPA cannot follow, look at what the failure looks like, and then go through the alternatives.

1A Quick Recap: How NESTED Works With JDBC

A savepoint is a named marker inside a database transaction. ROLLBACK TO SAVEPOINT undoes everything since the marker while keeping the transaction and everything before the marker alive. JDBC exposes it through Connection.setSavepoint() and Connection.rollback(savepoint).

With DataSourceTransactionManager, propagation NESTED inside an existing transaction means: set a savepoint, run the method, release the savepoint on success or roll back to it on failure. All the state lives in the database, so rolling back to the savepoint really does undo everything the nested method did.

Java
@Transactional
public void importAll(List<Row> rows) {
for (Row r : rows) {
try { rowImporter.importRow(r); } // NESTED, on another bean
catch (DataIntegrityViolationException e) { failures.add(r); }
}
}
// JDBC log for one bad row:
// SAVEPOINT SAVEPOINT_3
// insert into rows ... ; -- duplicate key
// ROLLBACK TO SAVEPOINT SAVEPOINT_3
// ... later: COMMIT of all good rows

2Why JPA Cannot Roll Back to a Savepoint

With JPA, the database is not the only place where state lives. The EntityManager keeps a persistence context, an in-memory map of every entity it has loaded or persisted in this transaction, with a snapshot of each one for dirty checking. Changes are written to the database only when the context is flushed.

Now imagine a savepoint rollback. The database forgets the rows inserted after the savepoint, but the persistence context still contains those entities as managed objects, with generated ids, and still holds modifications to other entities made in the nested method. At the next flush, Hibernate might try to update rows that no longer exist, or skip inserting entities it thinks are already saved. The two layers disagree, and Hibernate has no mechanism to return its context to the state it had at the savepoint.

That is why JpaTransactionManager supports savepoints only for JDBC access code that uses the same connection, and not for work done through the EntityManager. The Spring documentation for the transaction manager in your version describes this; check it for the exact wording.

State after ROLLBACK TO SAVEPOINT
LayerWith JdbcTemplateWith JPA EntityManager
Database rowsBack to the savepointBack to the savepoint
Persistence contextNot involvedStill holds the rolled-back entities
Next flushNothing pendingMay update missing rows or skip inserts
ResultConsistentDatabase and memory disagree

3What the Failure Looks Like

Whether Spring refuses up front depends on the JPA dialect, the component that adapts Spring to a specific JPA provider. If the dialect cannot give Spring access to the underlying JDBC connection, Spring has no way to create a savepoint at all and throws NestedTransactionNotSupportedException when the NESTED method is called. With Hibernate's dialect, the savepoint may be created on the JDBC connection, which is the more dangerous case because it seems to work while leaving the persistence context out of step.

Java
@Transactional(propagation = Propagation.NESTED)
public void importRow(Row r) { entityManager.persist(r.toEntity()); }
// Possible outcome, depending on dialect and version:
// org.springframework.transaction.NestedTransactionNotSupportedException:
// JpaDialect does not support savepoints - check your JPA provider's capabilities

Either way, the conclusion is the same: NESTED is not a tool for JPA work, and code that relied on it must be restructured. Check your Spring version's documentation for the exact behaviour.

4Alternative 1: REQUIRES_NEW for Independent Work

If each row should be committed or rejected on its own, REQUIRES_NEW gives each importRow() call a separate transaction with its own EntityManager. A failure rolls back that transaction and discards its persistence context completely, so there is no stale state.

Java
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void importRow(Row r) { entityManager.persist(r.toEntity()); }
// good rows commit one by one; a bad row rolls back alone

The semantics differ from NESTED: rows commit immediately, so if the outer transaction later fails, the imported rows stay. And each call holds a second connection while the outer one is suspended. Often the outer method then does not need a transaction at all, which removes that cost.

5Alternative 2: Validate Before Writing

Many uses of NESTED exist only to catch a constraint violation after the fact. It is usually better to check first: look up existing keys in one query, validate each row in memory, and write only the valid ones in a single transaction. The import then never needs to undo anything, and it is faster, because a failed insert followed by a rollback is much more expensive than a lookup.

Java
Set<String> existing = repository.findExistingCodes(codesOf(rows)); // one query
List<Row> valid = rows.stream().filter(r -> !existing.contains(r.code()) && r.isValid()).toList();
repository.saveAll(toEntities(valid)); // one transaction, no savepoints

There is still a small race if another transaction inserts the same key at the same moment, so the unique constraint stays as the final guard, and a rare violation simply fails the batch, which is retried.

6Alternative 3: Explicit Savepoints for Pure JDBC Work

If one part of the use case is pure JDBC, such as a bulk insert into a staging table with JdbcTemplate, savepoints still work for that part, either through NESTED on a JdbcTemplate-only method or explicitly through TransactionTemplate. The rule is that no JPA entity may be touched between the savepoint and its rollback, and the persistence context should be flushed before the savepoint so pending JPA changes are not caught in it. Because that rule is easy to break later, I would keep such code small and comment it clearly.

7Follow-up questions

Does NESTED work with JtaTransactionManager?

Generally no; JTA does not support nested transactions, so Spring throws NestedTransactionNotSupportedException there as well, unless the specific JTA setup supports it. Check your server and version.

What does NESTED do when no transaction exists?

It behaves like REQUIRED and starts a new transaction, so the method still runs transactionally. The savepoint behaviour only applies when it is called inside an existing transaction.

Could we clear the EntityManager after rolling back to the savepoint?

Clearing detaches every entity, including those loaded before the savepoint that should still be managed, so their later changes would not be saved. It trades one inconsistency for another, which is why it is not a general fix.

8Conclusion

In this answer, we saw that NESTED is built on JDBC savepoints, which undo database work but cannot undo the in-memory persistence context that JPA keeps, so JpaTransactionManager supports savepoints only for JDBC access and may refuse with NestedTransactionNotSupportedException. We replaced it with REQUIRES_NEW for independent units, with validating before writing so nothing needs undoing, or with explicit savepoints kept to pure JDBC work.

What to say in the interviewAbout 90 seconds aloud

Open with

NESTED is implemented with JDBC savepoints. With DataSourceTransactionManager and JdbcTemplate, Spring sets a savepoint, runs the method, and on failure rolls back to the savepoint, and that works because all the state is in the database.

Then, if they want more

With JPA there's a second layer of state, the persistence context, which holds managed entities and their snapshots in memory. A savepoint rollback undoes the rows in the database but not the persistence context, so Hibernate would still think those entities are saved, and the next flush could update rows that don't exist or skip inserts.

Hibernate can't roll its context back to a savepoint, so JpaTransactionManager supports savepoints only for plain JDBC access on the same connection, not for EntityManager operations, and depending on the JPA dialect and version it throws NestedTransactionNotSupportedException. I'd check the docs for our version.

For alternatives, if each piece of work is independent, I'd use REQUIRES_NEW, which gives each call its own transaction and its own persistence context, accepting that it commits immediately and needs a second connection.

Often the better answer is to validate before writing: look up existing keys in one query, filter invalid rows, and save only valid ones, with the unique constraint as a final guard. And if a part is pure JDBC, savepoints can still be used there, as long as no entities are touched in between.

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 "A method annotated @Transactional(propagation = NESTED) works with JdbcTemplate but...": https://scrivio-interview-library.netlify.app/databases/transactions/2bd27e7812/