Web app NHibernate Session Management Summer of NHibernate style?

I was curious so I googled around. HttpContext. Items can be used by multiple threads.

Nhibernate Sessions are not thread safe. And then there is there answer: ( possible dupe ) stackoverflow.com/questions/629719/nhibe... So it seems this recipe for disaster can be managed effectively. Because you didn't post the code that Summer of Nhibernate uses its hard to say whether your implementation won't fall over.

– mattnss Oct 12 '10 at 14:19 @mattnss - Read the answer I linked too. Looks like if your storing the session and not the factory itself you can get into trouble. – jfar Oct 12 '10 at 14:42 So in my code ive posted, i'm only storing the SessionFactory in a static variable, so it should be thread safe?

– mattnss Oct 13 '10 at 18:38 You should read up on what the static keyword does, teach a man to fish and everything. Needless to say that is probably the worst thing you can do if you want sessions to be unique to each controller and httprequest. – jfar Oct 13 '10 at 22:27 I wholeheartedly agree about the teach a man to fish aspect.

I ask the question because a lot of people seem to reference the Summer of Nbernate webcasts to Nbernate newbies and it would be good to know if learning it that way would be good for just theory or if it can be taken as a practical example towards the end of his sessions (I know in the beginning of his screencasts he does mention the initially introduced method to retrieve the session is not how you would execute it in the real world). – mattnss Oct 14 '10 at 1:50.

Taking a page from jfar I think I finally taught myself to fish (or maybe cast my pole in the right direction) on the subject and found some good articles that shed some light to elaborate on how the Summer of N Googling around to find more details on Nbernate and Contextual Sessions helped me get a better grasp of the topic. I'm still new to Nbernate (and web programming in general) so please bear with me if this question and answer seem elementary. It seems the Summer of Nbernate scheme is thread safe.My explanation will reference names from my code sample above.

The SessionFactory variable is static, so its accessible from every thread, it is however a thread safe object (whereas as mentioned the ISession is not thread safe). The NbernateSessionPerRequestModule establishes two events that will fire on a web request (Application_BeginRequest) and at the end of one (Application_EndRequest). Application_BeginRequest will grab an ISession object from which you can then bind it to the current context.

That helps ensure that the ISession object is used for the duration of the web request and only for that thread. After a web request begins, an ISession is then retrievable with the . GetCurrentSession() for use with the _companyRepository.

The Application_BeginRequest will also begin the transaction for you. The Application_EndRequest will unbind the ISession object, commit all changes at the end of your web request, and close the ISession. Peter Wigle has an article which shows a similar example of how to implement session management: http://pwigle.wordpress.com/2008/11/21/nhibernate-session-handling-in-aspnet-the-easy-way In that article the Global.

Asax is used to specify events for binding and unbinding instead of creating an IHttpModule. Ayende has a nice clean example that uses the HttpContext to manage your ISessions: http://ayende.com/Blog/archive/2009/08/05/do-you-need-a-framework.aspx If I am way off base and or am not understanding something please feel free to interject, but as far as I can tell so long as a different ISession is being binded to each thread context it will be thread safe (unless there is a need to have a cross threaded ISession).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions