<?xml version="1.0" encoding="UTF-8"?><rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
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/"
> <channel><title>Comments on: Avoiding Leaking Connections With NHibernate And TransactionScope</title> <atom:link href="http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/feed/" rel="self" type="application/rss+xml" /><link>http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/</link> <description>inquisitive: adjective. given to inquiry, research, or asking questions; eager for knowledge; intellectually curious</description> <lastBuildDate>Wed, 08 Feb 2012 11:42:42 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>By: Travis Shepherd</title><link>http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/comment-page-1/#comment-102630</link> <dc:creator>Travis Shepherd</dc:creator> <pubDate>Mon, 14 Nov 2011 21:43:00 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/#comment-102630</guid> <description>Has anyone tried using this practice with SQL Compact?  When I do, I get an exception related to nested transactions not being supported.  If I comment out the explicit call to Session.BeginTransaction(), things work properly.</description> <content:encoded><![CDATA[<p>Has anyone tried using this practice with SQL Compact?  When I do, I get an exception related to nested transactions not being supported.  If I comment out the explicit call to Session.BeginTransaction(), things work properly.</p> ]]></content:encoded> </item> <item><title>By: NHibernate and ADO.NET Connection Pooling &#124; SeekPHP.com</title><link>http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/comment-page-1/#comment-101453</link> <dc:creator>NHibernate and ADO.NET Connection Pooling &#124; SeekPHP.com</dc:creator> <pubDate>Tue, 27 Sep 2011 13:01:38 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/#comment-101453</guid> <description>[...] are also unneccessaryly distributed transactions created. A possible workaround described in http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/ therefore does not work (at least not with NHibernate 3.1.0). I am using Informix. The same problem [...]</description> <content:encoded><![CDATA[<p>[...] are also unneccessaryly distributed transactions created. A possible workaround described in <a
href="http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/" rel="nofollow">http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/</a> therefore does not work (at least not with NHibernate 3.1.0). I am using Informix. The same problem [...]</p> ]]></content:encoded> </item> <item><title>By: Maheshjj</title><link>http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/comment-page-1/#comment-100154</link> <dc:creator>Maheshjj</dc:creator> <pubDate>Mon, 11 Jul 2011 16:04:00 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/#comment-100154</guid> <description>Hi Davy, I am facing a similar problem in my application where we get &quot;distributed transaction completed. either enlist this session in a new transaction or the null transaction. after disposing a connection&quot; and i guess its because of some leaking connections. We use transaction scope and create a transaction using nhibernate session within it. In case of rollback what should be done. Should we rollback the nhibernate transaction and leave the transaction scope without completing it ? or should we rollback/dispose both the objects. Please help!</description> <content:encoded><![CDATA[<p>Hi Davy, I am facing a similar problem in my application where we get &#8220;distributed transaction completed. either enlist this session in a new transaction or the null transaction. after disposing a connection&#8221; and i guess its because of some leaking connections. We use transaction scope and create a transaction using nhibernate session within it. In case of rollback what should be done. Should we rollback the nhibernate transaction and leave the transaction scope without completing it ? or should we rollback/dispose both the objects. Please help!</p> ]]></content:encoded> </item> <item><title>By: Davy Brion</title><link>http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/comment-page-1/#comment-99635</link> <dc:creator>Davy Brion</dc:creator> <pubDate>Mon, 13 Jun 2011 09:49:00 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/#comment-99635</guid> <description>yup, just tried the original failing testcase of the post with NH 3.1 and it works... looks like it&#039;s not an issue anymore :)</description> <content:encoded><![CDATA[<p>yup, just tried the original failing testcase of the post with NH 3.1 and it works&#8230; looks like it&#8217;s not an issue anymore <img
src='http://d18sni7re4ly7f.cloudfront.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p> ]]></content:encoded> </item> <item><title>By: Val</title><link>http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/comment-page-1/#comment-99517</link> <dc:creator>Val</dc:creator> <pubDate>Wed, 01 Jun 2011 21:42:00 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/#comment-99517</guid> <description>I also think that in 3.0 this is not an issue any more. My scenario is as follows: WCF service (InstanceContextMode.PerCall, TransactionIsolationLevel=IsolationLevel.ReadCommitted, TransactionScopeRequired=true, TransactionAutoComplete=true). Basically all this can be thought of
as if each service call is wrapped in a using (var scope = new TransactionScope(TransactionScopeOption.Required))
{
    ...
}I tried one and the same service call with and without NHibernate BeginTransaction. Watched the behavior with NHProf, the collection pool with perfmon and SQL Server profiler and didn&#039;t see any indications of leaking connections in either case. The only difference was that in the first case (with BeginTransaction) there were two nested transactions:Case #1 with BeginTransaction:
**************************************-- statement #1enlisted session in distributed transaction with isolation level: ReadCommitted-- statement #2begin transaction with isolation level: ReadCommitted-- statement #3select userevent0_.Id               as Id1_0_,...-- statement #4SELECT usereventn0_.UserEvent_id   as UserEvent1_0_,...-- statement #5commit transaction-- statement #6commit transaction
Case #2 without BeginTransaction
**************************************-- statement #1enlisted session in distributed transaction with isolation level: ReadCommitted-- statement #2select userevent0_.Id               as Id1_0_,...-- statement #3SELECT usereventn0_.UserEvent_id   as UserEvent1_0_,...-- statement #4commit transaction </description> <content:encoded><![CDATA[<p>I also think that in 3.0 this is not an issue any more. My scenario is as follows: WCF service (InstanceContextMode.PerCall, TransactionIsolationLevel=IsolationLevel.ReadCommitted, TransactionScopeRequired=true, TransactionAutoComplete=true). Basically all this can be thought of<br
/> as if each service call is wrapped in a using (var scope = new TransactionScope(TransactionScopeOption.Required))<br
/> {<br
/>     &#8230;<br
/> }</p><p>I tried one and the same service call with and without NHibernate BeginTransaction. Watched the behavior with NHProf, the collection pool with perfmon and SQL Server profiler and didn&#8217;t see any indications of leaking connections in either case. The only difference was that in the first case (with BeginTransaction) there were two nested transactions:</p><p>Case #1 with BeginTransaction:<br
/> **************************************&#8211; statement #1enlisted session in distributed transaction with isolation level: ReadCommitted&#8211; statement #2begin transaction with isolation level: ReadCommitted&#8211; statement #3select userevent0_.Id               as Id1_0_,&#8230;&#8211; statement #4SELECT usereventn0_.UserEvent_id   as UserEvent1_0_,&#8230;&#8211; statement #5commit transaction&#8211; statement #6commit transaction<br
/> Case #2 without BeginTransaction<br
/> **************************************&#8211; statement #1enlisted session in distributed transaction with isolation level: ReadCommitted&#8211; statement #2select userevent0_.Id               as Id1_0_,&#8230;&#8211; statement #3SELECT usereventn0_.UserEvent_id   as UserEvent1_0_,&#8230;&#8211; statement #4commit transaction</p> ]]></content:encoded> </item> <item><title>By: My Beef with MSDTC and Two-Phase Commits</title><link>http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/comment-page-1/#comment-99231</link> <dc:creator>My Beef with MSDTC and Two-Phase Commits</dc:creator> <pubDate>Fri, 06 May 2011 02:40:55 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/#comment-99231</guid> <description>[...] that does support 2PC on MSDTC there are weird bugs and issues that arise.&#160; NHibernate would leak connections; MySQL would forget it was a cohort in a transaction during a server restart and rollback the [...]</description> <content:encoded><![CDATA[<p>[...] that does support 2PC on MSDTC there are weird bugs and issues that arise.&#160; NHibernate would leak connections; MySQL would forget it was a cohort in a transaction during a server restart and rollback the [...]</p> ]]></content:encoded> </item> <item><title>By: Tao</title><link>http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/comment-page-1/#comment-91779</link> <dc:creator>Tao</dc:creator> <pubDate>Fri, 14 Jan 2011 21:42:30 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/#comment-91779</guid> <description>The issue seems fixed in NHibernate 3.0.0. I did the test for both AdoNetTransactionFactory and default
AdoNetWithDistributedTransactionFactory.using (var scope = new TransactionScope(TransactionScopeOption.Required))
{
using (var session = sessionFactory.OpenSession())
{
var blah = session.CreateCriteria().List();
}
}You need add &quot;enlist=dynamic&quot; in Oracle connection string for Distributed transaction. Otherwise, throw exception.</description> <content:encoded><![CDATA[<p>The issue seems fixed in NHibernate 3.0.0. I did the test for both AdoNetTransactionFactory and default<br
/> AdoNetWithDistributedTransactionFactory.</p><p>using (var scope = new TransactionScope(TransactionScopeOption.Required))<br
/> {<br
/> using (var session = sessionFactory.OpenSession())<br
/> {<br
/> var blah = session.CreateCriteria().List();<br
/> }<br
/> }</p><p>You need add &#8220;enlist=dynamic&#8221; in Oracle connection string for Distributed transaction. Otherwise, throw exception.</p> ]]></content:encoded> </item> <item><title>By: Rodrigo Caballero</title><link>http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/comment-page-1/#comment-77427</link> <dc:creator>Rodrigo Caballero</dc:creator> <pubDate>Mon, 29 Nov 2010 17:05:18 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/#comment-77427</guid> <description>Hi, this code throws an Exception&lt;code&gt;
using (var scope = new TransactionScope(TransactionScopeOption.Required)){using (var session = sessionFactory.OpenSession())using (var transaction = session.BeginTransaction()){// do what you need to do with the sessiontransaction.Commit();}scope.Complete();}
&lt;/code&gt;
&lt;code&gt;
[NHibernate.TransactionException]{&quot;Begin failed with SQL exception&quot;}
The connection is already part of a local or distributed transaction
&lt;/code&gt;I cannot begin a NHibernate transaction inside a Transaction Scope.I&#039;m using Oracle 11G and Nhibernate 2.1.2Any clues?regardsRodrigoPD. I previously turned off Enlist on Oracle, but I also have an Oracle MembershipProvider and Logging App Blocks inserts which should be held on the same TransactionScope (So it is mandatory).</description> <content:encoded><![CDATA[<p>Hi, this code throws an Exception</p><p><code><br
/> using (var scope = new TransactionScope(TransactionScopeOption.Required))</p><p> {</p><p> using (var session = sessionFactory.OpenSession())</p><p> using (var transaction = session.BeginTransaction())</p><p> {</p><p> // do what you need to do with the session</p><p> transaction.Commit();</p><p> }</p><p> scope.Complete();</p><p> }<br
/> </code><br
/> <code><br
/> [NHibernate.TransactionException]{"Begin failed with SQL exception"}<br
/> The connection is already part of a local or distributed transaction<br
/> </code></p><p>I cannot begin a NHibernate transaction inside a Transaction Scope.</p><p>I&#8217;m using Oracle 11G and Nhibernate 2.1.2</p><p>Any clues?</p><p>regards</p><p>Rodrigo</p><p>PD. I previously turned off Enlist on Oracle, but I also have an Oracle MembershipProvider and Logging App Blocks inserts which should be held on the same TransactionScope (So it is mandatory).</p> ]]></content:encoded> </item> <item><title>By: Rodrigo Caballero</title><link>http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/comment-page-1/#comment-76767</link> <dc:creator>Rodrigo Caballero</dc:creator> <pubDate>Fri, 26 Nov 2010 21:59:20 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/#comment-76767</guid> <description>@Jason,First of all thanks for the valuable infoDid you solve the WCF-Oracle 11G issue?I have to configure WCF with transactionScope per service call against some separate nhibernate sessions, so all of them should be elisted on the same TransactionScope.regardsRodrigo</description> <content:encoded><![CDATA[<p>@Jason,</p><p>First of all thanks for the valuable info</p><p>Did you solve the WCF-Oracle 11G issue?</p><p>I have to configure WCF with transactionScope per service call against some separate nhibernate sessions, so all of them should be elisted on the same TransactionScope.</p><p>regards</p><p>Rodrigo</p> ]]></content:encoded> </item> <item><title>By: Jason Alati</title><link>http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/comment-page-1/#comment-66749</link> <dc:creator>Jason Alati</dc:creator> <pubDate>Fri, 22 Oct 2010 18:36:47 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/#comment-66749</guid> <description>@SudipI&#039;ve run into that issue as well.  In order to get rid of that error, I changed the &#039;Enlist&#039; value in my connection string to false.  However, any transaction you use then becomes local.  If you were to check the System.Transactions.Transaction.Current.TransactionInformation.DistributedIdentifier value with Enlist set to true (before it hits the session.BeginTransaction(), you&#039;ll see it&#039;s populated.  Otherwise, it&#039;s all zeroes.As I am currently trying to configure WS-Atomic Transaction for my WCF service, using NHibernate and Oracle 11g R2, I need to get past this issue as well.</description> <content:encoded><![CDATA[<p>@Sudip</p><p>I&#8217;ve run into that issue as well.  In order to get rid of that error, I changed the &#8216;Enlist&#8217; value in my connection string to false.  However, any transaction you use then becomes local.  If you were to check the System.Transactions.Transaction.Current.TransactionInformation.DistributedIdentifier value with Enlist set to true (before it hits the session.BeginTransaction(), you&#8217;ll see it&#8217;s populated.  Otherwise, it&#8217;s all zeroes.</p><p>As I am currently trying to configure WS-Atomic Transaction for my WCF service, using NHibernate and Oracle 11g R2, I need to get past this issue as well.</p> ]]></content:encoded> </item> <item><title>By: Sudip</title><link>http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/comment-page-1/#comment-60498</link> <dc:creator>Sudip</dc:creator> <pubDate>Wed, 22 Sep 2010 00:18:38 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/#comment-60498</guid> <description>I find that the following snippet works fine for SQL Server 2008 (SP1) but when I use Oracle (11g), the session.BeginTransaction() call throws an exception with the message &#039;Connection is already part of a local or a distributed transaction&#039;. Has anyone else run into this?using (var scope = new TransactionScope(TransactionScopeOption.Required)){
using (var session = sessionFactory.OpenSession())
using (var transaction = session.BeginTransaction())
{
// do what you need to do with the session
transaction.Commit();
}
scope.Complete();
}</description> <content:encoded><![CDATA[<p>I find that the following snippet works fine for SQL Server 2008 (SP1) but when I use Oracle (11g), the session.BeginTransaction() call throws an exception with the message &#8216;Connection is already part of a local or a distributed transaction&#8217;. Has anyone else run into this?</p><p>using (var scope = new TransactionScope(TransactionScopeOption.Required))</p><p>{<br
/> using (var session = sessionFactory.OpenSession())<br
/> using (var transaction = session.BeginTransaction())<br
/> {<br
/> // do what you need to do with the session<br
/> transaction.Commit();<br
/> }<br
/> scope.Complete();<br
/> }</p> ]]></content:encoded> </item> <item><title>By: Amel Music</title><link>http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/comment-page-1/#comment-53219</link> <dc:creator>Amel Music</dc:creator> <pubDate>Thu, 12 Aug 2010 09:37:35 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/#comment-53219</guid> <description>Only working solution that I could find is to wrap NHibernate session and pass it connection while opening it, on dispose method I call NHibernate dispose (this doesn&#039;t close connection since it isn&#039;t owner, and after that I close connection)AdoNetTransactionFactory also doesn&#039;t work very well, based on what I see it opens calls connection pool too many times, and if I put 10 sinc threads to do some work I have \to many connections\ exception.</description> <content:encoded><![CDATA[<p>Only working solution that I could find is to wrap NHibernate session and pass it connection while opening it, on dispose method I call NHibernate dispose (this doesn&#8217;t close connection since it isn&#8217;t owner, and after that I close connection)</p><p>AdoNetTransactionFactory also doesn&#8217;t work very well, based on what I see it opens calls connection pool too many times, and if I put 10 sinc threads to do some work I have \to many connections\ exception.</p> ]]></content:encoded> </item> <item><title>By: &#171; Notki Po Polsku &#171; Simon says&#8230; architecture!</title><link>http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/comment-page-1/#comment-42284</link> <dc:creator>&#171; Notki Po Polsku &#171; Simon says&#8230; architecture!</dc:creator> <pubDate>Thu, 17 Jun 2010 03:50:03 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/#comment-42284</guid> <description>[...] Jest to jednak zamiana jednego problemu na inny. Do niedawna bowiem pojawiał się w NServiceBus wyciek pamięci, ponieważ zachowanie NHibernate w wypadku współpracy z System.Transactions jest bardzo słabo [...]</description> <content:encoded><![CDATA[<p>[...] Jest to jednak zamiana jednego problemu na inny. Do niedawna bowiem pojawiał się w NServiceBus wyciek pamięci, ponieważ zachowanie NHibernate w wypadku współpracy z System.Transactions jest bardzo słabo [...]</p> ]]></content:encoded> </item> <item><title>By: Davy Brion</title><link>http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/comment-page-1/#comment-41068</link> <dc:creator>Davy Brion</dc:creator> <pubDate>Fri, 04 Jun 2010 09:09:10 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/#comment-41068</guid> <description>@Peterinteresting... i reran the test with AdoNetTransactionFactory and you&#039;re right... it doesn&#039;t have the leaking connection problem and the connection indeed appears to enlist properly in the distributed transactioni&#039;d have to investigate further, but this quick test does make me think that AdoNetTransactionFactory is more suitable for dealing with Distributed Transactions than the AdoNetWithDistributedTransactionFactory</description> <content:encoded><![CDATA[<p>@Peter</p><p>interesting&#8230; i reran the test with AdoNetTransactionFactory and you&#8217;re right&#8230; it doesn&#8217;t have the leaking connection problem and the connection indeed appears to enlist properly in the distributed transaction</p><p>i&#8217;d have to investigate further, but this quick test does make me think that AdoNetTransactionFactory is more suitable for dealing with Distributed Transactions than the AdoNetWithDistributedTransactionFactory</p> ]]></content:encoded> </item> <item><title>By: Sam</title><link>http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/comment-page-1/#comment-41038</link> <dc:creator>Sam</dc:creator> <pubDate>Thu, 03 Jun 2010 23:46:43 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/#comment-41038</guid> <description>I have a solution for my post above, and I actually feel like a bit of a numpty now for posting it in the first place!We are using our own session class that implements ISession and passes nearly all calls directly through to a local instance of NHibernate&#039;s SessionImpl class. As it turns out, our Dispose() does a little more work than just passing through. In fact it is calling the Close() and the Disconnect() methods on the SessionImpl instance directly. Take these calls out of our custom session (so it just basically passes the call through) and I have the same leaking connection result that I expected to see!</description> <content:encoded><![CDATA[<p>I have a solution for my post above, and I actually feel like a bit of a numpty now for posting it in the first place!</p><p>We are using our own session class that implements ISession and passes nearly all calls directly through to a local instance of NHibernate&#8217;s SessionImpl class. As it turns out, our Dispose() does a little more work than just passing through. In fact it is calling the Close() and the Disconnect() methods on the SessionImpl instance directly. Take these calls out of our custom session (so it just basically passes the call through) and I have the same leaking connection result that I expected to see!</p> ]]></content:encoded> </item> <item><title>By: Sam</title><link>http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/comment-page-1/#comment-40988</link> <dc:creator>Sam</dc:creator> <pubDate>Thu, 03 Jun 2010 06:01:35 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/#comment-40988</guid> <description>@DavyI&#039;ve been running some tests within our solution framework to see if we are vulnerable to this problem since our pattern for using TransactionScope is the same as your test case. The difference with our&#039;s is that we use the AdoNetTransactionFactory. From my investigations so far, I&#039;ve found that connections are not leaked when this class is used instead of the default AdoNetWithDistrubtedTransactionFactory. So I decided to switch to the AdoNetWithDistrubtedTransactionFactory to make sure that I could replicate your results. On the first iteration of the loop, my test ran into the following exception.System.InvalidOperationException: Disconnect cannot be called while a transaction is in progress.After a little investigation, I found that this is the exception NHibernate throws if ConnectionManager.Disconnect() is called while there is still an active transaction. This method is called when a session is Disposed, which in this case is happening at the end of the session using block within the TransactionScope using block. So I had to ask: why does this not happen in your code?!using (var scope = new TransactionScope(TransactionScopeOption.Required))
{
using (var session = sessionFactory.OpenSession())
{
var blah = session.CreateCriteria().List();
} // Disconnect called here, but transaction is still active, so exception is thrown.
}If we change the code to be as follows, then I get the same result as you (i.e. max pool size reached exception).using (var session = sessionFactory.OpenSession())
{
using (var scope = new TransactionScope(TransactionScopeOption.Required))
{
var blah = session.CreateCriteria().List();
}
}This code isn&#039;t particularly useful within our solution, so we will stick to the AdoNetTransactionFactory for now, as this is working well for us. I believe this is because of what Peter has said above, the actual Ado Connection is enlisting in the transaction even if NHibernate is ignoring it. I&#039;m just worried because the distributed transaction factory class SHOULD work for this! I&#039;ve seen several posts from people having the same problem as I found, but so far no one has suggested a reason for it. Have you got any ideas about it? Have you come across it before?We are using 2.1.2 and I have posted our config below (using the distributed transaction factory instead)Thanks!Sam
NHibernate.Connection.DriverConnectionProvider
NHibernate.Dialect.MsSql2005Dialect
NHibernate.Driver.SqlClientDriver
ReadCommitted
20
NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu
NHibernate.Transaction.AdoNetWithDistrubtedTransactionFactory, NHibernate </description> <content:encoded><![CDATA[<p>@Davy</p><p>I&#8217;ve been running some tests within our solution framework to see if we are vulnerable to this problem since our pattern for using TransactionScope is the same as your test case. The difference with our&#8217;s is that we use the AdoNetTransactionFactory. From my investigations so far, I&#8217;ve found that connections are not leaked when this class is used instead of the default AdoNetWithDistrubtedTransactionFactory. So I decided to switch to the AdoNetWithDistrubtedTransactionFactory to make sure that I could replicate your results. On the first iteration of the loop, my test ran into the following exception.</p><p>System.InvalidOperationException: Disconnect cannot be called while a transaction is in progress.</p><p>After a little investigation, I found that this is the exception NHibernate throws if ConnectionManager.Disconnect() is called while there is still an active transaction. This method is called when a session is Disposed, which in this case is happening at the end of the session using block within the TransactionScope using block. So I had to ask: why does this not happen in your code?!</p><p>using (var scope = new TransactionScope(TransactionScopeOption.Required))<br
/> {<br
/> using (var session = sessionFactory.OpenSession())<br
/> {<br
/> var blah = session.CreateCriteria().List();<br
/> } // Disconnect called here, but transaction is still active, so exception is thrown.<br
/> }</p><p>If we change the code to be as follows, then I get the same result as you (i.e. max pool size reached exception).</p><p>using (var session = sessionFactory.OpenSession())<br
/> {<br
/> using (var scope = new TransactionScope(TransactionScopeOption.Required))<br
/> {<br
/> var blah = session.CreateCriteria().List();<br
/> }<br
/> }</p><p>This code isn&#8217;t particularly useful within our solution, so we will stick to the AdoNetTransactionFactory for now, as this is working well for us. I believe this is because of what Peter has said above, the actual Ado Connection is enlisting in the transaction even if NHibernate is ignoring it. I&#8217;m just worried because the distributed transaction factory class SHOULD work for this! I&#8217;ve seen several posts from people having the same problem as I found, but so far no one has suggested a reason for it. Have you got any ideas about it? Have you come across it before?</p><p>We are using 2.1.2 and I have posted our config below (using the distributed transaction factory instead)</p><p>Thanks!</p><p>Sam</p><p> NHibernate.Connection.DriverConnectionProvider<br
/> NHibernate.Dialect.MsSql2005Dialect<br
/> NHibernate.Driver.SqlClientDriver<br
/> ReadCommitted<br
/> 20<br
/> NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu<br
/> NHibernate.Transaction.AdoNetWithDistrubtedTransactionFactory, NHibernate</p> ]]></content:encoded> </item> <item><title>By: Peter Karouzos</title><link>http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/comment-page-1/#comment-40984</link> <dc:creator>Peter Karouzos</dc:creator> <pubDate>Thu, 03 Jun 2010 04:51:28 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/#comment-40984</guid> <description>Hi Davy,I hope I can shed some light. The project I am on uses the same pattern as your test case. Create a transaction scope, open session, execute sql. However we use ADONetTransactionfacory. Doing the same test we are not seeing connections leaking. I believe this is because the ADO connection automatically enlists in the ambient transaction. So when there is no transactionscope.commit or exception is raised the impact of the session using is to rollback transaction and close connection. In other words we are using NHibernate to create the connection, execute the sql and perform the ORM stuff and rely on transactionscope to manage the distributed transactions.I wonder one of the issues with AdoNetWithDistributedTransactionFactory class is related to the connection automatically enlisting in the ambient transaction when the code is assuming it is managing the enlistment. I am concerned when I see this in the codepublic void AfterTransaction()
{
if (IsAfterTransactionRelease)
{
AggressiveRelease();
}
else if (IsAggressiveRelease &amp;&amp; batcher.HasOpenResources)
{
log.Info(&quot;forcing batcher resource cleanup on transaction completion; forgot to close ScrollableResults/Enumerable?&quot;);
batcher.CloseCommands();
AggressiveRelease();
}
else if (IsOnCloseRelease)
{
// log a message about potential connection leaks
log.Debug(
&quot;transaction completed on session with on_close connection release mode; be sure to close the session to release ADO.Net resources!&quot;);
}
transaction = null;
}cheers
peter</description> <content:encoded><![CDATA[<p>Hi Davy,</p><p>I hope I can shed some light. The project I am on uses the same pattern as your test case. Create a transaction scope, open session, execute sql. However we use ADONetTransactionfacory. Doing the same test we are not seeing connections leaking. I believe this is because the ADO connection automatically enlists in the ambient transaction. So when there is no transactionscope.commit or exception is raised the impact of the session using is to rollback transaction and close connection. In other words we are using NHibernate to create the connection, execute the sql and perform the ORM stuff and rely on transactionscope to manage the distributed transactions.</p><p>I wonder one of the issues with AdoNetWithDistributedTransactionFactory class is related to the connection automatically enlisting in the ambient transaction when the code is assuming it is managing the enlistment. I am concerned when I see this in the code</p><p>public void AfterTransaction()<br
/> {<br
/> if (IsAfterTransactionRelease)<br
/> {<br
/> AggressiveRelease();<br
/> }<br
/> else if (IsAggressiveRelease &amp;&amp; batcher.HasOpenResources)<br
/> {<br
/> log.Info(&#8220;forcing batcher resource cleanup on transaction completion; forgot to close ScrollableResults/Enumerable?&#8221;);<br
/> batcher.CloseCommands();<br
/> AggressiveRelease();<br
/> }<br
/> else if (IsOnCloseRelease)<br
/> {<br
/> // log a message about potential connection leaks<br
/> log.Debug(<br
/> &#8220;transaction completed on session with on_close connection release mode; be sure to close the session to release ADO.Net resources!&#8221;);<br
/> }<br
/> transaction = null;<br
/> }</p><p>cheers<br
/> peter</p> ]]></content:encoded> </item> <item><title>By: Davy Brion</title><link>http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/comment-page-1/#comment-39114</link> <dc:creator>Davy Brion</dc:creator> <pubDate>Fri, 14 May 2010 08:26:37 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/#comment-39114</guid> <description>@Peterhaven&#039;t tried it with AdoNetTransactionFactory yet, since that&#039;s not even used by default.  Even when you&#039;re not using TransactionScope, NH will use AdoNetWithDistributedTransactionFactory</description> <content:encoded><![CDATA[<p>@Peter</p><p>haven&#8217;t tried it with AdoNetTransactionFactory yet, since that&#8217;s not even used by default.  Even when you&#8217;re not using TransactionScope, NH will use AdoNetWithDistributedTransactionFactory</p> ]]></content:encoded> </item> <item><title>By: Peter Karouzos</title><link>http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/comment-page-1/#comment-38986</link> <dc:creator>Peter Karouzos</dc:creator> <pubDate>Thu, 13 May 2010 03:57:19 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/#comment-38986</guid> <description>Hiis the leaking happening with both transaction factory classes AdoNetTransactionfactory and AdoNetWithDistributedTransactionFactory</description> <content:encoded><![CDATA[<p>Hi</p><p>is the leaking happening with both transaction factory classes AdoNetTransactionfactory and AdoNetWithDistributedTransactionFactory</p> ]]></content:encoded> </item> <item><title>By: Davy Brion</title><link>http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/comment-page-1/#comment-38900</link> <dc:creator>Davy Brion</dc:creator> <pubDate>Wed, 12 May 2010 06:59:27 +0000</pubDate> <guid
isPermaLink="false">http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/#comment-38900</guid> <description>@Juanah, i once had an issue where Disposing the session caused problems in combination with TransactionScope.  But that was when the TransactionScope was created before the Session was. In that situation, i just had to close the session instead of disposing it</description> <content:encoded><![CDATA[<p>@Juan</p><p>ah, i once had an issue where Disposing the session caused problems in combination with TransactionScope.  But that was when the TransactionScope was created before the Session was. In that situation, i just had to close the session instead of disposing it</p> ]]></content:encoded> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 4/26 queries in 0.020 seconds using disk: basic
Object Caching 642/647 objects using disk: basic
Content Delivery Network via Amazon Web Services: CloudFront: d18sni7re4ly7f.cloudfront.net

Served from: davybrion.com @ 2012-02-09 03:52:34 -->
