views:

3057

answers:

2

I have been reading about Nhibernate for a while and have been trying to use it for a site I'm implementing. I read the article by Billy McCafferty on NHibernate best practices but I did not see any indication on where is the best place to handle transactions.

I thought of putting that code in the Data Access Object (DAO) but then I'm not sure how to handle cases in which more than one DAO is used. What are the best places to put transaction code in your NHibernate Application?

+7  A: 

It really depends on your environment. For example, we use the Open-Session-In-View (Java link but the pattern is the same in .Net) pattern on an ASP.Net site and use an HttpModule for controlling sessions and transactions. The HttpModule opens a session and starts a transaction in the BeginRequest event and then commits the transaction and closes the session in the EndRequest event.

Different environments and communication patterns will lead to different session/transaction management strategies.

There is a lot of discussion on the various strategies in the NHibernate Users Group as well as on the web in general.

Sean Carpenter
+1  A: 

Based on Ayende, you probably need to explicitly add transaction for all, even searching. Please check here: http://nhprof.com/Learn/Alerts/DoNotUseImplicitTransactions

Liang Wu