<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Intelligrape  Groovy &#38; Grails Blogs &#187; Spring Transactions</title>
	<atom:link href="http://www.intelligrape.com/blog/tag/spring-transactions/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.intelligrape.com/blog</link>
	<description></description>
	<lastBuildDate>Thu, 02 Feb 2012 07:48:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<image>
			<title>Intelligrape  Groovy &amp; Grails Blogs</title>
			<url>http://www.intelligrape.com/blog/wp-content/uploads/2011/05/favicon2.ico</url>
			<link>http://www.intelligrape.com/blog</link>
			<width></width>
			<height></height>
			<description></description>
		</image>		<item>
		<title>Grails Transactions using @Transactional annotations and Propagation.REQUIRES_NEW</title>
		<link>http://www.intelligrape.com/blog/2010/07/30/grails-transactions-using-transactional-annotations-and-propagation-requires_new/</link>
		<comments>http://www.intelligrape.com/blog/2010/07/30/grails-transactions-using-transactional-annotations-and-propagation-requires_new/#comments</comments>
		<pubDate>Fri, 30 Jul 2010 08:01:37 +0000</pubDate>
		<dc:creator>Abhishek Tejpaul</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Grails]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[@Transactional]]></category>
		<category><![CDATA[Annotations]]></category>
		<category><![CDATA[Grails Declarative Transactions]]></category>
		<category><![CDATA[Grails Transactions]]></category>
		<category><![CDATA[Propagation.REQUIRES_NEW]]></category>
		<category><![CDATA[Spring Transactions]]></category>

		<guid isPermaLink="false">http://www.intelligrape.com/blog/?p=1405</guid>
		<description><![CDATA[Hi All,
Here is how you can implement a new transaction in an already executing transaction in Grails which uses nothing but Spring framework&#8217;s Transaction mechanism as an underlying implementation. Spring provides @Transactional annotations to provide declarative transactions. We can use the same in our Grails project to achieve the transactional behavior.
Here is the scenario: You [...]]]></description>
			<content:encoded><![CDATA[<p>Hi All,</p>
<p>Here is how you can implement a new transaction in an already executing transaction in Grails which uses nothing but Spring framework&#8217;s Transaction mechanism as an underlying implementation. Spring provides <strong><em>@Transactional </em></strong>annotations to provide declarative transactions. We can use the same in our Grails project to achieve the transactional behavior.</p>
<p><em>Here is the scenario:</em> You have two domain classes named <strong>SecuredAccount</strong> and <strong>AccountCreationAttempt</strong>. You try to transactionally save the SecuredAccount object which in turn creates a AccountCreationAttempt object which writes to the database stating: &#8220;There is an attempt to create a new SecuredAccount at this time: &lt;current date and time&gt;&#8221;. Point to note here is that even if the creation of the new SecuredAccount object fails, the record must still be written to the database so that the Administrator can validate whether the attempt at the specific time was by a legitimate user or an attacker. </p>
<p><em>Here is the code:</em></p>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #a1a100;">import org.springframework.transaction.annotation.*</span>
<span style="color: #000000; font-weight: bold;">Class</span> MyService <span style="color: #66cc66;">&#123;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">static</span> transactional <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">false</span>
<span style="color: #000000; font-weight: bold;">def</span> anotherService
&nbsp;
@Transactional
<span style="color: #000000; font-weight: bold;">def</span> createSecuredAccount<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #000000; font-weight: bold;">def</span> securedAccount <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SecuredAccount<span style="color: #66cc66;">&#40;</span>userId:<span style="color: #ff0000;">&quot;John&quot;</span><span style="color: #66cc66;">&#41;</span>
securedAccount.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span>flush:<span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>
anotherService.<span style="color: #006600;">createAccountCreationAttempt</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">RuntimeException</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Error thrown in createSecuredAccount()&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #a1a100;">import org.springframework.transaction.annotation.*</span>
<span style="color: #000000; font-weight: bold;">class</span> AnotherService <span style="color: #66cc66;">&#123;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">static</span> transactional <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">false</span>
&nbsp;
@Transactional<span style="color: #66cc66;">&#40;</span>propagation <span style="color: #66cc66;">=</span> Propagation.<span style="color: #006600;">REQUIRES_NEW</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">def</span> createAccountCreationAttempt<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #000000; font-weight: bold;">def</span> accountCreationAttempt <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AccountCreationAttempt<span style="color: #66cc66;">&#40;</span>logRemarks: <span style="color: #ff0000;">&quot;There is an attempt to create a new SecuredAccount at this time: {new Date()}&quot;</span><span style="color: #66cc66;">&#41;</span>
accountCreationAttempt.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span>flush:<span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Now in this scenario, AccountCreationAttempt object always gets persisted whether or not the transaction for creating SecuredAccount object fails.</p>
<p>Here are few gotchas regarding the above transactions:</p>
<p>1.) First of all, for <em>Propagation.REQUIRES_NEW </em>to work as intended, it has to be inside a new object i.e. a new service in our example. If we had put the <em>createAccountCreationAttempt()</em> method in the MyService there would be no new transaction spawned and even no log entry would be made. This is Spring&#8217;s proxy object implementation of transactions and you can read more about it here:</p>
<p><a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html#transaction-declarative-annotations" target="_blank">http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html#transaction-declarative-annotations</a>.</p>
<p>Please pay special attention to the &#8220;<strong>NOTE</strong>&#8221; sub-section.This is what it states:</p>
<p>&#8220;In proxy mode (which is the default), only external method calls coming in through the proxy are intercepted. This means that self-invocation, in effect, a method within the target object calling another method of the target object, will not lead to an actual transaction at runtime even if the invoked method is marked with<em><strong> @Transactional</strong></em>.&#8221;</p>
<p>2.) Secondly, all the <em><strong>@Transactional</strong></em> methods should have a public visibility i.e. createSecuredAccount() and createAccountCreationAttempt() methods should be public methods, and not private or protected. This again is Spring&#8217;s <em><strong>@Transactional</strong> </em>annotations implementation and you can read about it at the same <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html#transaction-declarative-annotations" target="_blank">link</a> as provided above. Note the right side-bar titled &#8220;<strong>Method visibility and <em>@Transactional</em></strong>&#8220;.</p>
<p>Well, once you keep note of these gotchas I guess you are all set to make good use of <strong><em>@Transactional</em></strong> annotations and its full power.</p>
<p>Cheers !!!</p>
<p>- Abhishek Tejpaul<br />
abhishek@intelligrape.com<br />
[IntelliGrape Software Pvt. Ltd.]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.intelligrape.com/blog/2010/07/30/grails-transactions-using-transactional-annotations-and-propagation-requires_new/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

