Menu

Spring Transactional pitfalls: self-invocation and rollback
📰

Spring Transactional pitfalls: self-invocation and rollback

DEV Community: java·duong vu·1 day ago
#zXouBAN2
Long article (2687 words) — no AI summary yet.
Reading 0:00
15s threshold

Originally published at Shipped Code .

You put @Transactional on a service method, an exception is thrown halfway through, and the first row is still in the database. Usually one of 2 things happened. The call never went through Spring's proxy, so there was no transaction at all, or the exception was a checked one, which commits by default. Most of the other pitfalls come from propagation. An inner REQUIRED method that fails dooms the whole outer transaction, while REQUIRES_NEW commits on its own.

Pitfall Symptom Fix

Self-invocation ( this.place() ) No transaction, rows saved before the exception stay Call through another bean, or use TransactionTemplate

Checked exception Method throws, data is committed anyway Throw unchecked domain exceptions; rollbackFor when you can't

Swallowed inner failure UnexpectedRollbackException at commit Don't catch and continue, or give the inner call REQUIRES_NEW

Audit row under REQUIRED Audit disappears with the failed operation REQUIRES_NEW for the audit write (needs a…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.