<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vijay Rawat on Medium]]></title>
        <description><![CDATA[Stories by Vijay Rawat on Medium]]></description>
        <link>https://medium.com/@vijayrawatsan?source=rss-2fe30cdece86------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*r_bccPOlTT_ULD1B.jpeg</url>
            <title>Stories by Vijay Rawat on Medium</title>
            <link>https://medium.com/@vijayrawatsan?source=rss-2fe30cdece86------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 01:58:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vijayrawatsan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Hibernate Issues Series : RollbackException]]></title>
            <link>https://medium.com/ofbusiness-tech/hibernate-issues-series-rollbackexception-94174bfce89b?source=rss-2fe30cdece86------2</link>
            <guid isPermaLink="false">https://medium.com/p/94174bfce89b</guid>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[rollbacks]]></category>
            <category><![CDATA[hibernate]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[exception]]></category>
            <dc:creator><![CDATA[Vijay Rawat]]></dc:creator>
            <pubDate>Tue, 05 Jun 2018 03:31:01 GMT</pubDate>
            <atom:updated>2018-06-05T03:31:01.490Z</atom:updated>
            <content:encoded><![CDATA[<p>This the first blog in my Hibernate Issues Series. In this piece, I will be introducing a few frequent and not-so-common issues relating to hibernate transactions.</p><p>Whenever we see this exception:</p><blockquote>javax.persistence.RollbackException: Transaction marked as rollbackOnly</blockquote><p>our default reaction is to search stackoverflow, mostly finding the solution as “using @Transactional’s noRollbackFor for CulpritException.class”</p><p><a href="#42fd">[Directly go to Caching the culprit section]</a></p><p>Today lets dive in and find how this exception is thrown by Spring framework.</p><p>We know that @org.springframework.transaction.annotation.Transactional is supported via aspects in spring using the <strong>proxy pattern</strong>.</p><p>You can check the spring wrapper <a href="https://gist.github.com/vijayrawatsan/753f2896d18fd982453a3c3b99867ffd">here</a> (<a href="https://gist.github.com/vijayrawatsan/753f2896d18fd982453a3c3b99867ffd">gist link</a>)<br>Essentially what it’s doing is :</p><ul><li>Creating a transaction around the method.</li><li>Invoke the method.</li><li>Handle exception.</li><li>Cleanup of resources.</li></ul><p>When @Transaction annotated method throw’s an exception, the control goes to <strong>TransactionAspectSupport#completeTransactionAfterThrowing(TransactionInfo txInfo, Throwable ex)</strong><br>Internally this method does the following:</p><ul><li>Checks if <strong>rollbackFor</strong> attribute is defined with <strong>@Transactional(rollbackFor = AnyException.class)</strong></li><li>If yes, then check if thrown exception is subclass of AnyException.class, in this case rollback the transaction.</li><li>If no, check if thrown exception is a subclass of RuntimeException or Error, in this case rollback the transaction.</li><li>If <strong>rollbackFor </strong>attribute is not present, check if thrown exception is a subclass of RuntimeException or Error, if yes rollback the transaction.</li><li>If we want to not rollback for a certain exception we can use <strong>noRollbackFor </strong>attribute.</li></ul><p>Now we know the theory lets see some code examples to corroborate our theory.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6cce2305203ae5745e46d56ae9dff2a3/href">https://medium.com/media/6cce2305203ae5745e46d56ae9dff2a3/href</a></iframe><p>Lets see what’s happening in above examples.</p><p><strong>Example 1:</strong></p><p>In this example (<strong>UserService#createUserEx1()</strong>), we expect the transaction to commit because we are catching the exception thrown by exception service.</p><p>As per our theory, proxy will initialize the transaction, call this method, handles any caught exception, and then performs cleanup. Since this method(<strong>createUserEx1),</strong> catches the exception thrown by exception service. No exception reaches the proxy code and everything works as expected.</p><p><strong>Example 2:</strong></p><p>In this example (<strong>UserService#createUserEx2()</strong>) also, we expect the transaction to commit because we are catching the exception thrown by exception service.</p><p>This is a tricky example because, against our expectation, transaction rollbacks. So what’s happening?</p><ul><li>When we call <strong>createUserEx2() </strong>a transaction proxy(P1) is created, within which <strong>createUserEx2()</strong> method is called.</li><li>Call to <strong>throwExceptionTransactional</strong>() creates another transaction proxy(P2) is, within which <strong>throwExceptionTransactional()</strong> method is called. Since no <strong>propagation </strong>attribute is defined, same transaction will be forwarded for this method (lets call it <strong>T1</strong>).</li><li>There are 2 proxies but one transaction.</li><li>When <strong>throwExceptionTransactional() </strong>method throws exception, its proxy(P2) catches the transaction(<strong>T1</strong>), call the method to rollback it (basically sets the <strong>rollback flag to true</strong>), and re-throws the exception back in the chain.</li><li>Control returns to <strong>createUserEx2() </strong>method which catches the re-thrown exception.</li><li>The proxy(P1) of <strong>createUserEx2() </strong>method now tries to commit the transaction(T1) because of a clean exit from this method. But transaction T1 is already marked for rollback in P2 thus causing javax.persistence.RollbackException: Transaction marked as rollbackOnly</li></ul><p><strong>Example 3</strong></p><p>In example 3, <strong>createUserEx3() </strong>method looks almost same as <strong>createUserEx2() </strong>the only difference is the call to <strong>throwExceptionTransactionalRequiresNew() </strong>method which is a @Transactional annotated method with a special attribute propagation = Propagation.<em>REQUIRES_NEW (which creates a new transaction even if this method is called from a @</em>Transactional<em> </em>method<em>)</em></p><p>If you run this example, you will find that the code runs successfully without any exception. Why is that?</p><ul><li>When we call <strong>createUserEx3() </strong>a transaction proxy(P1) is created, within which <strong>createUserEx3()</strong> method is called.</li><li>Call to <strong>throwExceptionTransactionalRequiresNew</strong>() creates another transaction proxy(P2) is, within which <strong>throwExceptionTransactionalRequiresNew()</strong> method is called. Because of Propagation.<em>REQUIRES_NEW</em>, a new transaction will be created for this method as well (lets call it <strong>T2</strong>).</li><li>There are 2 proxies and 2 transactions.</li><li>When <strong>throwExceptionTransactionalRequiresNew() </strong>method throws exception, its proxy(P2) catches the transaction(<strong>T2</strong>), call the method to rollback it, and re-throws the exception back in the chain. (Transaction <strong>T2 </strong>is successfully rolled back after proxy P2 exits).</li><li>Control returns to <strong>createUserEx3() </strong>method which catches the re-thrown exception.</li><li>The proxy(P1) of <strong>createUserEx3() </strong>method is now successfully able to commit the transaction(T1) because of a clean exit from this method.</li></ul><p><strong>Example 4</strong></p><p>This example shows you how to avoid rollback in case of a specific exception using @Transacional attribute <strong>noRollbackFor. </strong>Using this attribute we can tell the transaction proxy to not rollback in case of a specific exception.</p><p><strong>Catching the culprit</strong></p><p>Above examples explains how can you craft your code in a way to avoid rollback exception. But when you run into javax.persistence.RollbackException: Transaction marked as rollbackOnly the stacktrace can only tell you which @Transactional method threw this exception but does not tell anything about the exception itself or where the actual exception is was thrown. How to find the source of exception then?</p><p>If you are still reading, you know where the proxy code is. You can simply put a debug point in <strong>TransactionAspectSupport#completeTransactionAfterThrowing()</strong> method and check which exception is thrown from where, by simply making your way back in the stacktrace.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=94174bfce89b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ofbusiness-tech/hibernate-issues-series-rollbackexception-94174bfce89b">Hibernate Issues Series : RollbackException</a> was originally published in <a href="https://medium.com/ofbusiness-tech">OFB Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>