<?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>Dude, where is my Kaizen? &#187; Ambient transactions</title>
	<atom:link href="http://www.bjoernrochel.de/tag/ambient-transactions/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bjoernrochel.de</link>
	<description>Björn Rochel&#039;s weblog</description>
	<lastBuildDate>Mon, 21 Jun 2010 07:26:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Ambient transactions and NHibernate</title>
		<link>http://www.bjoernrochel.de/2008/08/22/ambient-transactions-and-nhibernate/</link>
		<comments>http://www.bjoernrochel.de/2008/08/22/ambient-transactions-and-nhibernate/#comments</comments>
		<pubDate>Fri, 22 Aug 2008 15:29:30 +0000</pubDate>
		<dc:creator>BjRo</dc:creator>
				<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[xUnit]]></category>
		<category><![CDATA[Ambient transactions]]></category>

		<guid isPermaLink="false">http://bjro.de/2008/08/22/ambient-transactions-and-nhibernate/</guid>
		<description><![CDATA[About two weeks ago we had some discussions in the german Alt.NET mailing list whether ambient transactions can be used in combination with NHibernate, especially regarding performance implications of such an approach.
Background of that discussion was that my colleague Sergey and I wanted to implement the repository pattern based on Linq 2 NHibernate in a [...]]]></description>
			<content:encoded><![CDATA[<p>About two weeks ago we had some discussions in the german Alt.NET mailing list whether ambient transactions can be used in combination with NHibernate, especially regarding performance implications of such an approach.</p>
<p>Background of that discussion was that my colleague <a href="http://shishkin.org" target="_new">Sergey</a> and I wanted to implement the repository pattern based on Linq 2 NHibernate in a way that exposes NO NHibernate dependency to a surrounding layer. Besides that we didn&#8217;t want to dublicate the UnitOfWork pattern that NHibernate implements internally. Because of that we decided to try out ambient transactions (System.Transactions) as our UnitOfWork.</p>
<p>Sergey has already posted about the design we&#8217;re currently investigating, so I won&#8217;t go into detail about that here. You can read more here:</p>
<ul>
<li><a href="http://sergeyshishkin.spaces.live.com/blog/cns!9F19E53BA9C1D63F!263.entry" target="_new">RepositoryPatter revised</a></li>
<li><a href="http://sergeyshishkin.spaces.live.com/blog/cns!9F19E53BA9C1D63F!265.entry" target="_new">UnitOfWork Pattern revised</a></li>
<li><a href="http://sergeyshishkin.spaces.live.com/blog/cns!9F19E53BA9C1D63F!264.entry" target="_new">Specification Pattern revised</a></li>
</ul>
<p>We started with a simple test comparison between the behavior of NHibernates native ITransactions and an NHibernate using TransactionScope for transactions.Â  What we noticed :</p>
<p><strong>FlushMode.Commit doesn&#8217;t work when using ambient Transactions</strong></p>
<p>The following (xUnit) test fails:</p>
<pre class="brush: csharp">
[Fact]
public void Session_should_be_clean_after_commited_transaction()
{
    using (var tx = new TransactionScope())
    {
          SaveTwoPatients();
          tx.Complete();
    }

        Session.IsDirty().ShouldBeFalse();
}
</pre>
<p>But this can be easily fixed like this:</p>
<pre class="brush: csharp">
[Fact]
public void Session_should_be_clean_after_commited_transaction_Fixed()
{
    using (var tx = new TransactionScope())
    {
        Transaction.Current.TransactionCompleted += (s, e) =&gt;
        {
            if (e.Transaction.TransactionInformation.Status == TransactionStatus.Committed)
            {
                Session.Flush();
                Session.Clear();
            }
        };

        SaveTwoPatients();
        tx.Complete();
    }

    Session.IsDirty().ShouldBeFalse();
}
</pre>
<p><strong>For some reason ambient Transaction are FASTER than NHibernates native counter parts</strong></p>
<p>We have also some testsÂ  that perform a transactional insert a 1000 times. The duration of those tests are quite surprising. Have a look:</p>
<p><a title="NHibernateTransactions" href="http://www.bjoernrochel.de/wp-content/uploads/2008/08/nhibernatetransactions.png"><img src="http://www.bjoernrochel.de/wp-content/uploads/2008/08/nhibernatetransactions.png" alt="NHibernateTransactions" width="80%" height="80%" /></a></p>
<p>This has nothing to do with jitting in the CLR or the order how test are executed. I double checked the durations and ran each test on his own.Â  I didn&#8217;t expect that gap, but I&#8217;m also not a NHibernate pro. Any thoughts?</p>
<p>I&#8217;ll write more about our experiences with that approach soon. Our concept looks good on paper and our first impressions are not disproving either, so stay tuned . . .</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bjoernrochel.de/2008/08/22/ambient-transactions-and-nhibernate/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
