Scrivio Interview library
ScenarioIntermediate6 min read · updated 12 November 2026

A team uses noRollbackFor on a business exception so that a failure record is saved, but now partial order data is also committed when that exception occurs. How would you redesign it?

noRollbackFor tells Spring to commit the whole transaction when that exception is thrown, so every order row written before the failure is committed along with the failure record. The redesign is to let the business transaction roll back as a whole and write the failure record separately: in a REQUIRES_NEW transaction or after the rollback, ideally from @TransactionalEventListener(phase = AFTER_ROLLBACK) or a TransactionSynchronization afterCompletion callback. Where possible, validate before writing so the exception happens before any changes, and add tests that assert the database state after each failure path.

Why this question is asked

The team's goal was reasonable: when an order fails for a business reason, keep a record of why, for support and analytics. The tool they reached for, noRollbackFor, does something much broader than they intended, and the result is partial orders in the database. Interviewers ask this scenario because the fix requires separating two units of work that were accidentally merged, and choosing the right mechanism for each. In this answer, we will look at the current design and why it commits too much, then redesign it in three ways and finish with the tests that prove it works.

1The Current Design

Here is a simplified version. The order and its lines are saved, the credit check fails with a business exception, and a failure record is saved in the catch block. noRollbackFor was added so the failure record would survive.

Java
@Transactional(noRollbackFor = CreditRejectedException.class)
public void placeOrder(OrderRequest req) {
Order order = orders.save(Order.from(req)); // INSERT order
lines.saveAll(OrderLine.from(order, req)); // INSERT 3 lines
try {
credit.check(req.customerId(), order.total()); // throws CreditRejectedException
} catch (CreditRejectedException e) {
failures.save(new OrderFailure(req.customerId(), e.reason()));
throw e;
}
}
// After a rejection: order + 3 lines + failure record all COMMITTED

The failure record is there, as wanted. So are the order and its lines, for an order that was rejected. Other parts of the system now see a real-looking order with no payment.

2Why noRollbackFor Commits Everything

A transaction is all or nothing. Rollback rules do not choose which rows to keep; they choose what happens to the entire transaction when an exception leaves the method. noRollbackFor = CreditRejectedException.class means 'when this exception is thrown, commit as if the method succeeded'. Every write in the transaction, the order, the lines and the failure record, is part of the same unit, so they all commit.

The real problem is that two unrelated units of work were placed in one transaction: the order, which should be all or nothing, and the failure record, which should survive the order's failure. Any fix must split them.

3Redesign 1: Roll Back the Order, Write the Failure Separately

We remove noRollbackFor so the business exception rolls the order back, and we write the failure record in its own transaction. The simplest form calls a separate bean with REQUIRES_NEW.

Java
@Transactional // default rules: rollback on the exception
public void placeOrder(OrderRequest req) {
Order order = orders.save(Order.from(req));
lines.saveAll(OrderLine.from(order, req));
try {
credit.check(req.customerId(), order.total());
} catch (CreditRejectedException e) {
failureLog.record(req.customerId(), e.reason()); // REQUIRES_NEW on another bean: commits now
throw e; // order and lines roll back
}
}
// After a rejection: failure record COMMITTED, order and lines ROLLED BACK

This works and is easy to follow, but it holds a second connection while the outer transaction is suspended, and it writes the failure record before we know the outer transaction actually rolled back. A cleaner version reacts to the rollback itself.

4Redesign 2: React to the Rollback With an Event

Spring can run code after a transaction completes. We publish an event inside the transaction, and a listener with phase AFTER_ROLLBACK runs only if the transaction rolls back, after it has done so. Since Spring Framework 6.1, a transactional event listener that writes to the database must use REQUIRES_NEW or NOT_SUPPORTED, because the original transaction is already finished.

Java
@Transactional
public void placeOrder(OrderRequest req) {
events.publishEvent(new OrderAttempted(req.customerId())); // carries what we know so far
Order order = orders.save(Order.from(req));
lines.saveAll(OrderLine.from(order, req));
credit.check(req.customerId(), order.total()); // throws, transaction rolls back
}
@Component
class OrderFailureRecorder {
@TransactionalEventListener(phase = TransactionPhase.AFTER_ROLLBACK)
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void onRollback(OrderAttempted e) {
failures.save(new OrderFailure(e.customerId(), "order rolled back"));
}
}
Step throughOrder rejected, failure recorded after rollback
OrderServiceTx managerDatabaseFailureRecorder
begin, publish OrderAttempted
insert order and lines
CreditRejectedException: rollback
ROLLBACK: order and lines gone
AFTER_ROLLBACK: onRollback(event)
new tx: insert failure record, COMMIT

6 messages in order. Play them, or step through them one at a time.

The failure is recorded exactly when the order did not happen, whatever caused the rollback. To record the business reason, the service can attach it to the event before rethrowing, or the listener can look it up. A lower-level alternative is to register a TransactionSynchronization and check the status in afterCompletion(int status), where STATUS_ROLLED_BACK tells us what happened.

One limit is durability: if the application crashes between the rollback and the listener, the record is lost. For an analytics record that is usually acceptable; for a record that must exist, REQUIRES_NEW before the rollback or a message to a durable queue is safer.

5Redesign 3: Validate Before Writing

Often the best fix is to check first. If the credit decision does not depend on the saved order, we run it before any insert. A rejection then happens before the transaction has written anything, and the failure record can be saved on its own, with nothing to roll back.

Java
public void placeOrder(OrderRequest req) { // not transactional itself
CreditDecision d = credit.decide(req.customerId(), req.total());
if (d.rejected()) {
failureLog.record(req.customerId(), d.reason()); // one short transaction
throw new CreditRejectedException(d.reason());
}
orderWriter.create(req); // @Transactional: order and lines together
}

This version is also faster, since rejected orders never touch the order tables, and it avoids holding a transaction open during a remote credit check.

6Testing Every Failure Path

The original bug survived because the tests checked that the exception was thrown and that the failure record existed, but never that the order did not. For each failure path we should assert the full database state afterwards: which tables have rows and which do not. The test must not be @Transactional itself, or it will roll back everything at the end and hide the real behaviour.

Java
@Test
void creditRejectionLeavesNoOrderButRecordsFailure() {
assertThatThrownBy(() -> service.placeOrder(rejectedRequest()))
.isInstanceOf(CreditRejectedException.class);
assertThat(orderRepository.count()).isZero(); // 0
assertThat(lineRepository.count()).isZero(); // 0
assertThat(failureRepository.count()).isEqualTo(1); // 1
}

7Follow-up questions

When is noRollbackFor the right tool?

When the exception really means 'the work so far is valid and should be kept', for example a warning-like exception after all writes are complete and correct. If any write before the exception should be undone, noRollbackFor is wrong.

Why not catch the exception and save the record without rethrowing?

Then the method returns normally and commits everything, which is the same bug in a different form. The order would be saved as if it succeeded, and the caller would not know it failed.

Does AFTER_ROLLBACK run if the exception was thrown before any write?

Only if the transaction existed and the event was published inside it. If no transaction is active when the event is published, the listener does not run unless fallbackExecution = true is set.

8Conclusion

In this answer, we saw that noRollbackFor commits the entire transaction, so the order and its lines were committed along with the failure record. We split the two units of work: the order rolls back as a whole, and the failure is recorded in a REQUIRES_NEW transaction, in an AFTER_ROLLBACK event listener or afterCompletion callback, or, best of all, by validating before any write. Tests that assert the full database state after each failure path keep the bug from coming back.

What to say in the interviewAbout 90 seconds aloud

Open with

The issue is that noRollbackFor doesn't mean keep this one row; it means when this exception is thrown, commit the whole transaction. So the order and its lines, written before the credit check failed, are committed along with the failure record.

Then, if they want more

Really there are two units of work mixed into one transaction: the order, which should be all or nothing, and the failure record, which should survive the order's failure. So I'd remove noRollbackFor and let the business exception roll the order back, then write the failure record separately.

The simple version calls a separate bean with REQUIRES_NEW from the catch block before rethrowing. A cleaner version publishes an event in the transaction and records the failure in a TransactionalEventListener with phase AFTER_ROLLBACK, which runs only once the rollback has happened; the listener needs REQUIRES_NEW to write, and Spring 6.1 checks that.

A TransactionSynchronization afterCompletion callback is the lower-level equivalent. If the credit check doesn't need the saved order, I'd go further and validate before writing anything, so a rejection never touches the order tables and the failure record is one short transaction.

And I'd add tests, not themselves transactional, that check the full database state after each failure path: no order, no lines, one failure record, because the original tests clearly only checked that the exception was thrown.

About this answer

Last updated
12 November 2026
Versions it names
Spring Framework 6
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 team uses noRollbackFor on a business exception so that a failure record is saved, but...": https://scrivio-interview-library.netlify.app/databases/transactions/722b31a986/