Scrivio Interview library
DebuggingFoundations5 min read · updated 12 November 2026

A @Transactional method throws a checked IOException after writing two rows, but the rows are committed. Why, and how do you change it?

By default Spring rolls back only on unchecked exceptions, RuntimeException and Error, and commits when a checked exception such as IOException leaves the method, following the old EJB convention that checked exceptions are expected business outcomes. So the two rows were committed. We change it with @Transactional(rollbackFor = IOException.class), or by wrapping the IOException in an unchecked exception, and Spring Framework 6.2 added a global option on @EnableTransactionManagement to roll back on all exceptions; check your version. Kotlin teams are hit hardest, because Kotlin has no checked exceptions and nothing in the code warns them.

Why this question is asked

This is one of the most common transaction surprises in Spring, and it is dangerous because nothing looks wrong: the method threw, the caller saw the error, and the developer assumes the work was undone. Days later someone finds rows from a failed import. Interviewers ask it because the answer shows whether we know the rollback rules rather than assuming them. In this answer, we will reproduce the problem, explain where the rule comes from, fix it three ways, and look at why it catches Kotlin developers in particular.

1Reproducing the Problem

The method writes two rows and then reads a file, which fails.

Java
@Transactional
public void importBatch(Path file) throws IOException {
repository.save(new Batch("B-1")); // row 1
repository.save(new Batch("B-2")); // row 2
String content = Files.readString(file); // NoSuchFileException, a subclass of IOException
parse(content);
}
// Caller sees: java.nio.file.NoSuchFileException: /data/in/batch.csv
// SELECT count(*) FROM batch; -- 2: both rows committed

Notice that the exception does reach the caller. Spring did not swallow it; it only decided that this kind of exception should not undo the transaction.

2Why Spring Commits on Checked Exceptions

When an exception leaves a @Transactional method, the TransactionInterceptor asks the method's rollback rules whether it should roll back. The default rule, in DefaultTransactionAttribute.rollbackOn(), returns true only for RuntimeException and Error. For anything else, it commits and then rethrows.

The convention comes from EJB. In that model, an unchecked exception means a system failure, so the container rolls back, while a checked exception is an 'application exception', a business outcome such as InsufficientFundsException that the caller is expected to handle, and the work done so far may be meaningful. Spring kept the same default to make migration from EJB easy, and it has stayed for compatibility ever since.

Step throughWhat happens when an exception leaves the method
  1. Exception leaves the @Transactional method
  2. Does a rollbackFor or noRollbackFor rule match?
    YesFollow that ruleNoUse the default rule
  3. Is it a RuntimeException or an Error?
    YesRoll back, then rethrowNoCommit, then rethrow

3 steps in order. Play them, or step through them one at a time.

3Fix 1: rollbackFor on the Method

The direct fix is to list the exception in rollbackFor. The rule matches the class and its subclasses, so IOException also covers NoSuchFileException.

Java
@Transactional(rollbackFor = IOException.class)
public void importBatch(Path file) throws IOException { ... }
// SELECT count(*) FROM batch; -- 0: rolled back
@Transactional(rollbackFor = Exception.class) // common team convention: roll back on everything
public void importAll(Path dir) throws IOException, ParseException { ... }

Many teams adopt rollbackFor = Exception.class as a convention, or a composed annotation such as @TransactionalService that includes it, so nobody has to remember each checked exception.

4Fix 2: Wrap It in an Unchecked Exception

If the caller cannot do anything useful with an IOException, it can be wrapped where it happens. Java already has UncheckedIOException for this.

Java
@Transactional
public void importBatch(Path file) {
repository.save(new Batch("B-1"));
repository.save(new Batch("B-2"));
String content;
try {
content = Files.readString(file);
} catch (IOException e) {
throw new UncheckedIOException(e); // RuntimeException: default rule rolls back
}
parse(content);
}

Even better, read the file before writing anything. Then a missing file fails before the transaction has done any work, and there is nothing to roll back.

5Fix 3: Change the Default for the Application

Spring Framework 6.2 added a global option so that every @Transactional method rolls back on any exception, set through @EnableTransactionManagement with rollbackOn = RollbackOn.ALL_EXCEPTIONS. Check your version for the exact attribute and how Spring Boot exposes it.

Java
@Configuration
@EnableTransactionManagement(rollbackOn = RollbackOn.ALL_EXCEPTIONS) // Spring Framework 6.2+, check your version
class TransactionConfig {}

This is attractive for new services, but switching an existing service changes behaviour for every method that currently relies on committing after a checked exception, so it needs tests on the exception paths before it is turned on.

6Why Kotlin Teams Are Surprised

Kotlin does not have checked exceptions: a Kotlin function can throw IOException without declaring it, and the compiler never asks the caller to handle it. A Kotlin developer therefore has no reason to think of IOException as different from IllegalStateException. Yet Spring's rule is based on the Java class hierarchy, not on the language, so an IOException thrown from a Kotlin @Transactional function still commits.

For Kotlin services, the global setting from 6.2 or a team-wide rollbackFor = [Exception::class] convention is close to essential, because the code gives no visual hint of which exceptions are checked.

7Follow-up questions

Does rollbackFor = Exception.class also cover RuntimeException?

Yes. RuntimeException is a subclass of Exception, so the rule matches it too, and Error is still rolled back by default. The annotation adds to the default rules rather than replacing them.

Can you write rollbackFor with a class name instead?

Yes, rollbackForClassName takes names as strings, but a typo then fails silently and the way names are matched has changed between releases (check your version), so the class-based rollbackFor is safer.

How would you test this?

With an integration test that is not itself @Transactional: call the method with a missing file, assert the exception, then count the rows in a fresh transaction and assert zero. A @Transactional test would roll back at the end anyway and hide the problem.

8Conclusion

In this answer, we saw that Spring commits when a checked exception like IOException leaves a @Transactional method, because the default rule rolls back only on RuntimeException and Error, following the EJB convention that checked exceptions are business outcomes. We fixed it with rollbackFor, by wrapping in UncheckedIOException or reading the file first, or with the global setting on @EnableTransactionManagement that Spring Framework 6.2 added, and we saw why Kotlin teams, who have no checked exceptions, are surprised most.

What to say in the interviewAbout 80 seconds aloud

Open with

Spring's default rollback rule only rolls back on unchecked exceptions, RuntimeException and Error. If a checked exception like IOException leaves the method, the transaction interceptor commits and then rethrows it, so the caller sees the error but the two rows are saved.

Then, if they want more

That rule comes from the EJB convention, where a checked exception was treated as an expected business outcome and an unchecked one as a system failure, and Spring kept it for compatibility. There are a few ways to change it.

I can add rollbackFor = IOException.class to the annotation, and it covers subclasses like NoSuchFileException; many teams just use rollbackFor = Exception.class everywhere. I can wrap the IOException in an unchecked exception, like UncheckedIOException, so the default rule applies. Better still, I'd read the file before writing anything, so the failure happens before there's anything to roll back.

And since Spring Framework 6.2 there's a global option on @EnableTransactionManagement to roll back on all exceptions, which I'd check for our version and consider for new services, with tests on exception paths before switching an existing one.

It's especially important for Kotlin, because Kotlin has no checked exceptions, so developers don't think of IOException as special, but Spring still commits on it.

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 @Transactional method throws a checked IOException after writing two rows, but the...": https://scrivio-interview-library.netlify.app/databases/transactions/0ae964e7c5/