Without deep knowledge about how the sessions get garbage collection,
I would not let the garbage collector clean up your session. I would
let the caller make clear when he finished its stuff.

For instance, make the repository disposable. The clean up all the
sessions and transactions in the dispose method of the repository.

By the way, if you have the data "in the thread", you probably have
many thread static fields. Make sure to clean them up as well, since
there is a thread pool which reuses the threads and then you get ghost
data from previous threads.

Let me tell you how we solved the problem. We implemented a contextual
transaction for NH, because transaction scopes need actually be
defined in the business logic, but there you don't want to have any NH
specific stuff. It looks like this:

using (TransactionManager.CreateTransactionScope())
{
  // make calls to the repository or calls to other
  // classes that use the repository
  ...
  // if you reach this point, no exception occurred
  TransactionManager.Commit()
}

If you leave the scope without calling commit before, it gets rolled
back. After the using statement, everything is cleaned up.

There is no reference to NH in the business assembly, since the
TransactionManager is called using an interface and does not have any
NH specific types in the interface.

The session is made by the first use of it, and closed on commit or
end of transaction scope.

On 7 Aug., 02:34, Graham Bunce <[email protected]> wrote:
> All,
>
> I'm using an "ambient context" pattern where my code doesn't explictly
> open or close a session. It simply adds to a repository (wrapping NH)
> and when it persists the repository, the persist method writes data
> out within a Transaction Scope.
>
> The first time any code talks the repository, it will automatically
> open an NH session on that thread. Similar concept to the Open Session
> In View pattern.
>
> My question is that we've started doing a lot of multi-threaded
> programming so my pattern is opening sessions for each thread. I'm ok
> with this as I store a session against the thread data. What I'm
> concerned about is that the programmer is not explictly closing the
> session. They would just finish the thread and let it get terminated
> by .NET. So, what will happen to the sessions in NH? Are they
> automatically garbage collected (as nothing references them as the
> thread that did reference them has terminated) or does NH somehow keep
> track of the session in its factory, even though it isn't being used.
>
> Have I got a potential memory leak?
>
> 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