NHibernate Forge
The official new home for the NHibernate for .NET community

Comments: NHibernate and the Unit of Work Pattern

Comments (18)
History (3)

NHibernate and the Unit of Work Pattern

Sort by: Published Date | Most Recent | Most Useful
1 2 3 4 Next >
By: gianfragolo Posted on 12-17-2008 9:05

Hi Gabriel,

in your UOW implementation, I see a static variable ISession _currentSession. In web environment when multiple concurrent requests/threads start (can be took many time to execute...) can dispose current static Session in use, and of course we have a common error: "You are not in a unit of work.".

Why not store _currentSession in Local.Data storage ?

thx Luca

By: bk8133 Posted on 12-06-2009 6:59

a very very good article

By: ElEspada Posted on 12-25-2009 13:48

Excellent article!!!

I have a question...Why in your design you tie the UoW factory interface to the Hibernate ISession (IUnitOfWorkFactory.CurrentSession)?, at the beginning of the article you claim that the idea of your design is to abstract the data manipulation from a specific infrastructure (ex. NHibernate, EF, Memory, etc) otherwise you can use ISession in a simple way, so, unless I miss something, it seems the original design goal is broken.

Thanks, JC

By: guido Posted on 05-27-2010 5:25

Hi Gabriel, nice and useful implementation. I am using it. But gianfragolo is right. ISession _currentSession must be saved in a LocalData storage. Otherwise serious troubles do occur at least in Web environment. It just takes hitting Refresh fast enough a small number of time to have very bad problems.

Anyway when moving to LocalData storage this serious problem disappear.

By: Krlos Posted on 08-04-2010 18:12

Section: Implementation For Smart Clients

[ThreadStatic]

private static Hashtable _localData = new Hashtable();

Initialization has to be encapsulated on a property, because initialization just occur in main thread, that's why in the background thread _localData is always null, example:

[ThreadStatic]

private static Hashtable _localData;

private static Hashtable LocalHashTable

           {

               get

               {

                   if (_localData == null)

                       _localData = new Hashtable();

                   return _localData;

               }

           }

Powered by Community Server (Commercial Edition), by Telligent Systems