Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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

Simplest way is to use Smart Transaction described in

http://rajputyh.blogspot.com/2011/02/nested-transaction-handling-with.html

It uses popular NHibernateSessionManager and a simple class is written to ensure that "you open transaction when you want" without worrying about what parent logic and sub logic is handling transaction.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top