Based on the topic "Using NHibernate with ASP.Net" (
http://wiki.nhibernate.org/display/NH/Using+NHibernate+with+ASP.Net) I have implemented NHibernate session management by opening a session when an HttpRequest begins and closing it when this request ends. My question is how should I do transaction management within the scope of this request. Here are two approaches I can think of:
1) Begin a transation when the HttpRequest begins (right after the session is opened) and commit the transaction when the HttpRequest ends. The advantage of this approach is that begin and commit transactions are tucked away in framework code and developers do not have to deal with it explicitly. However, it is difficult to rollback when an exception occurs because the transaction is not accessible to application code.
2) Second approach is to let the application code do transaction management. Disadvantage is that now transaction begin and commit become the responsibility of application programmer. However the advantage is that in case of an exception, doing a rollback is very easy because the transaction is easily available to the application code.
Any opnions on these two approaches? Are there other approaches that people have tried?
Thanks