I've seen many implementations on the web of people managing their NHibernate sessions and transactions in a HttpModule.
The HttpModule:
If people use this strategy how are they handling the following scenario:
It seems that there is no good way to rollback the transaction in the above scenario. The only plan I can come up with is to:
Seeing as so many people seem to be using the HttpModule approach I'm hoping there is a third way of managing this scenario that I haven't thought of?
You can use some kind of global exception handling. Now I am using System.AppDomain.CurrentDomain.UnhandledException
. In this handler you will need to call Transaction.Rollback()
; And also condsider setting some flag (that also lives during current request only) that will point wehether you need to commit your transaction or roll back. This can make code more clear.
Edit
Alternativly you can use Error event of the HttpApplication
public class HelloWorldModule : IHttpModule
{
void Init(HttpApplication application)
{
application.BeginRequest +=
(new EventHandler(this.Application_BeginRequest));
application.EndRequest +=
(new EventHandler(this.Application_EndRequest));
//this is it
applicaiton.Error +=
(new EventHandler(this.Application_Error));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With