the problem isn't failing code. the problem is a memory leak. the
instance of the connection (or transaction?) is never properly cleaned
up. there are 2 choices.
1. update to NH3 (targets 3.5) where the problem is resolved.
2. use a try catch to explicitly commit/rollback the NH transaction
using(var tx = session.BeginTransaction())
{
   try
   {
      session.Get<Entity>(id).Property = newValue;
      tx.Commit();
   }
   catch
   {
      tx.Rollback();
      throw;
   }
}
depending on how you manage NH transactions you can isolate the
transaction management to a single class.


On Jan 13, 12:01 pm, Graham Bunce <[email protected]> wrote:
> Hi,
>
> I'm using NH 2.1.2GA and am attempting to reproduce the leaky
> connection bug with Transaction Scope. I'm looking at this as our
> existing architecure doesn't use the recommended pattern of
> TransactionScope -> Open Session -> Start NH transaction, and although
> we get no issues, I've always known it's something we'd have to fix.
>
> I'm trying to reproduce the error using the blog entry provided by
> Davy Brion, i.e.:
>
>         [Test]
>         public void Make_NH_Connections_Leak()
>         {
>             var counter = 101;
>
>             for (int i = 0; i < counter; i++)
>             {
>                 using (var scope = new
> TransactionScope(TransactionScopeOption.Required))
>                 {
>                     using (var session = this.OpenSession())
>                     {
>                         var blah =
> session.CreateCriteria<DomainClass>().List<DomainClass>();
>                     }
>                 }
>             }
>
>         }
>
> The thing is... this doesn't fail. The only way i can get it to fail
> is if I maintain a reference to TransactionScope in a list, i.e. I
> keep 100 of them active. At which point it does fail, but that is
> exactly what I expect in that scenario.
>
> I'm guessing the above succeeds because the Transaction Scope is
> disposed of, thereby releasing the connection, which is the pattern
> I'd expect to be used by most people.
>
> Therefore, I'm a little confused. I'm sure NH hasn't been re-worked to
> get around this issue since the original post (esp. 2.1.2GA), but is
> this still a problem?  I'm using .NET 3.5 SP1 and SQL Server 2008.
>
> Thanks

-- 
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.

Reply via email to