Juan, I have tested that scenario and it does leak, unless you promote
the transaction to a distributed transaction. The solution I have
settled on is this one:
using (var scope = new
TransactionScope(TransactionScopeOption.Suppress))
{
using (ISession session = sessionFactory.OpenSession())
{
using (var scope = new
TransactionScope(TransactionScopeOption.RequiresNew))
{
// This will hit the database
session.CreateSQLQuery("SELECT
GETDATE()").ExecuteUpdate();
Transaction.Current.PromoteToDistributed();
}
}
}
Notes:
1. When you call session.Dispose, you cannot be in another
transaction. Otherwise it leaks
2. When you do your work, you must ensure it is a distributed
transaction. Otherwise it leaks
3. You cannot re-use a session, because the transaction completes on
another thread your get a race condition
After my last reply I tested a few other scenarios using the
nHibernate BeginTransaction and they failed. But there is hope, a
colleague pointed out the following blog, he is testing it and I'll
post back if it works.
http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/
On May 18, 2:21 pm, Juan <[email protected]> wrote:
> Iain,connectionleaks occur because the session is not properly
> disposed/closed. The current pattern I have been using with no
> problems is
>
> ISession session = OpenSessio();
> try
> {
> using(var ts = new TransactionScope())
> {
> ...
> ts.Complete();
> }}
>
> finally { session.Dispose(); }
> On Apr 15, 12:48 am, Iain <[email protected]> wrote:
>
>
>
>
>
> > Hi All,
>
> > I am having aconnectionleakwhen we using the ambient transaction,
> > and the transaction is aborted. That is, I get the exception:
> > System.InvalidOperationException: Timeout expired. The timeout period
> > elapsed prior to obtaining aconnectionfrom the pool. This may have
> > occurred because all pooled connections were in use and max pool size
> > was reached.
>
> > I am guessing something is holding onto a reference to theconnection,
> > or an internal part of theconnection. As adding a watch to following
> > and debug breaking within the transaction also causes this issue.
> > ((System.Data.SqlClient.SqlInternalConnectionTds)
> > ((System.Data.SqlClient.SqlConnection)
> > (session.Connection))._innerConnection)._connectionPool.Count
>
> > Is this a known issue or unsupported? Currently our workaround is to
> > promote the transaction to a distributed transaction which seems to
> > work ok, as per this
> > article:http://davybrion.com/blog/2010/03/msdtc-woes-with-nservicebus-and-nhi...
>
> > Here is the unit test that re-produces the issue:
>
> > [Test]
> > public void CanUseSystemTransactionsToAbort()
> > {
> > for (var i = 0; i < 200; i++)
> > {
> > using (ISession session = sessions.OpenSession())
> > using (TransactionScope tx = new
> > TransactionScope(TransactionScopeOption.RequiresNew))
> > {
> > W s = new W();
> > session.Save(s);
> > session.Flush();
> > }
> > }
>
> > }
>
> > Cheers,
>
> > Iain
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group
> athttp://groups.google.com/group/nhusers?hl=en.- Hide quoted text -
>
> - Show quoted text -
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.