<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel><title>نوشته های Ali Sohrabi</title><link>https://virgool.ir/feed/@alisohrabi</link><description></description><language>fa</language><pubDate>2026-11-11 02:13:32</pubDate><image><url>https://files.virgool.ir/upload/users/40594/avatar/mkcDmn.jpg</url><title>Ali Sohrabi</title><link>https://virgool.ir/@alisohrabi</link></image><item><title>Rollback in Hibernate Transactions</title><link>https://virgool.ir/@alisohrabi/rollback-in-hibernate-transactions-jv8tkqan2sfi</link><description>Why Some Exceptions Trigger Rollback and Others Don’tOne of the most common sources of bugs in Spring + Hibernate applications is misunderstanding transaction rollback behavior.Many developers assume:“If an exception happens, the transaction will rollback.”That assumption is wrong by default.Let’s clarify how it really works — and how to avoid costly data inconsistencies.The Core Rule (Spring Default)In Spring-managed transactions (@Transactional):🔴 Unchecked Exceptions(RuntimeException, Error)➡ Trigger rollback automatically🟡 Checked Exceptions(Exception, IOException, SQLException)➡ DO NOT trigger rollback by defaultThis rule surprises many developers — especially those new to Spring.Why Does Spring Work This Way?This behavior is intentional and based on Java’s exception philosophy:Checked exceptions→ Often represent recoverable or business-related conditions→ Spring assumes you might want to commit and handle themUnchecked exceptions→ Represent programming errors or system failures→ Spring assumes the transaction is unsafe to commitA Simple Example@Transactional&#xA;public void createUserWithCheckedException() throws IOException {&#xA;    userRepository.save(new User(&#34;Alice&#34;));&#xA;    throw new IOException(&#34;Checked exception&#34;);&#xA;}&#xA;✅ The user WILL be savedNow compare that with:@Transactional&#xA;public void createUserWithRuntimeException() {&#xA;    userRepository.save(new User(&#34;Bob&#34;));&#xA;    throw new RuntimeException(&#34;Runtime exception&#34;);&#xA;}&#xA;❌ The transaction WILL rollbackSame code. Different exception type. Very different outcome.Forcing Rollback on Checked ExceptionsYou can explicitly tell Spring to rollback:@Transactional(rollbackFor = IOException.class)&#xA;public void createUserSafely() throws IOException {&#xA;    userRepository.save(new User(&#34;Charlie&#34;));&#xA;    throw new IOException(&#34;Now it rolls back&#34;);&#xA;}&#xA;This is essential when checked exceptions should indicate transaction failure.Preventing Rollback for Runtime ExceptionsSometimes you want the opposite:@Transactional(noRollbackFor = IllegalStateException.class)&#xA;public void processNonCriticalFailure() {&#xA;    repository.save(new Entity());&#xA;    throw new IllegalStateException(&#34;Non-critical issue&#34;);&#xA;}&#xA;⚠ Use this carefully — it can be dangerous if misused.Best Practice: Use Runtime Exceptions for Transaction FailuresMost mature Spring applications follow this rule:All exceptions that should rollback a transaction should be uncheckedExample:public class InsufficientBalanceException extends RuntimeException {&#xA;    public InsufficientBalanceException(String message) {&#xA;        super(message);&#xA;    }&#xA;}&#xA;Why this works well:Clear intentAutomatic rollbackCleaner service layerNo rollbackFor noise everywhereA Common Anti-Pattern (Very Important)❌ Catching exceptions inside a @Transactional method@Transactional&#xA;public void badExample() {&#xA;    try {&#xA;        repository.save(new Entity());&#xA;        throw new RuntimeException();&#xA;    } catch (Exception e) {&#xA;        // swallowed&#xA;    }&#xA;}&#xA;✅ Transaction will commit❌ Data may be inconsistentIf you catch an exception, Spring assumes everything is fine unless you manually mark rollback.Hibernate vs Spring: A Quick NotePure Hibernate (Session API)→ Any exception usually marks the transaction rollbackSpring + Hibernate (most projects)→ Spring rules apply (unchecked only by default)Understanding this difference matters when debugging legacy code.Key Takeaways✅ Unchecked exceptions → rollback (default)⚠ Checked exceptions → commit (default)🔧 Use rollbackFor only when necessary🧠 Prefer unchecked exceptions for transactional failures❌ Don’t swallow exceptions inside transactionsIf you’ve ever seen “ghost data”, partial updates, or unexpected commits — this rule is often the reason.Understanding it once can save hours of debugging later.💬 Have you been bitten by this behavior before?</description><category>Ali Sohrabi</category><author>Ali Sohrabi</author><pubDate>Sun, 28 Dec 2025 18:32:36 +0330</pubDate></item></channel></rss>