{"id":5284,"date":"2014-03-30T20:20:33","date_gmt":"2014-03-30T20:20:33","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/nested-transaction-and-ejbcontexts-setrollbackonly-collection-of-common-programming-errors\/"},"modified":"2014-03-30T20:20:33","modified_gmt":"2014-03-30T20:20:33","slug":"nested-transaction-and-ejbcontexts-setrollbackonly-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/nested-transaction-and-ejbcontexts-setrollbackonly-collection-of-common-programming-errors\/","title":{"rendered":"Nested Transaction and EJBContext&#39;s setRollbackOnly()-Collection of common programming errors"},"content":{"rendered":"<p>To add to @Philippe Marshall&#8217;s correct answer and your comment &#8211; <code>REQUIRES_NEW<\/code> will create a new transaction, independent from the first one. They are not nested. The first transaction is suspended while the second is active. Once the second transaction commits, the first one is resumed.<\/p>\n<p>You do not have to <code>setRollbackOnly()<\/code> manually. Most <code>PersistenceException<\/code>s will do that if needed. You will gain nothing by rolling back transactions that you don&#8217;t have to. For example, when querying data, you may get a <code>NoResultException<\/code> or a <code>NonUniqueResultException<\/code>. They do not cause a transaction to be rolled back as there is no risk of inconsistencies between the persistence context and the DB.<\/p>\n<p>You do not need to specify neither <code>@TransactionAttribute(REQUIRED)<\/code> nor <code>@TransactionManagement(TransactionManagementType.CONTAINER)<\/code>&#8211; both are the default settings.<\/p>\n<p>EDIT: to answer your further questions:<\/p>\n<p>I am assuming <code>@TransactionAttribute(REQUIRES_NEW)<\/code> on <code>method2<\/code> and therefore two separate transactions.<\/p>\n<p>If there is an <code>Exception<\/code> that leads to the rollback of the transaction in <code>method2<\/code>, the transaction from <code>method1<\/code> will not be rolled back if the Exception is caught. If the <code>Exception<\/code> is not caught, both transactions will be rolled back.<\/p>\n<p>When setting the rollback flag on a transaction, it does not matter whether it happens before or after DB operations, since the entire transaction is rolled back.<\/p>\n<p>Once <code>method2<\/code> returns, it&#8217;s transaction is committed. Rolling back or committing the transaction from <code>method1<\/code> afterwards has no influence on the results of the first transaction.<\/p>\n<p>A general advice &#8211; do not catch <code>Throwable<\/code> &#8211; it is much too broad and you might swallow exceptions which you would rather let propagate to the surface.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To add to @Philippe Marshall&#8217;s correct answer and your comment &#8211; REQUIRES_NEW will create a new transaction, independent from the first one. They are not nested. The first transaction is suspended while the second is active. Once the second transaction commits, the first one is resumed. You do not have to setRollbackOnly() manually. Most PersistenceExceptions [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5284","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5284","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5284"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5284\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}