Spring: @Transactional rolls back on unchecked exceptions only

RuntimeException and Error trigger a rollback; a checked exception commits unless you say otherwise.

Code
@Transactional
public void a() throws Exception {
    save();
    throw new Exception("checked");   // COMMITS
}

@Transactional(rollbackFor = Exception.class)
public void b() throws Exception {
    save();
    throw new Exception("checked");   // rolls back
}
Output
a(): the row is still there.
b(): the row is gone.
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-11