These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: NHibernate open session in view with HttpModule
PostPosted: Wed Oct 11, 2006 11:48 am 
Newbie

Joined: Mon Jul 17, 2006 3:22 pm
Posts: 2
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: NHibernate 1.0.2


I am new to NHibernate, but have *some* experience with using Hibernate 3 in Java. As I read the many posts on session management for NHibernate, I began to adopt the HttpModule approach for a "session per request" pattern implementation.

I noticed that in the Java implementation using a filter, a transaction was "started" along with the session obtained. That way, the transaction logic did not have to be embedded within the service layer or within the DAOs themselves.

Here is what I have so far, based on the HttpModule posted by Ed Courtenay:



Code:
public class NHibernateHttpModule : IHttpModule {

        private static readonly string SESSION_KEY = "NHibernateSession";
        private static readonly string TRANSACTION_KEY = "NHibernateTransaction";

        public void Init( HttpApplication context ) {
            context.EndRequest += new EventHandler( context_EndRequest );
        }

        public void Dispose( ) { }

        private void context_BeginRequest( object sender,
                                           EventArgs e ) {
            HttpApplication application = ( HttpApplication )sender;
            HttpContext context = application.Context;

            ISession session = NHibernateHelper.OpenSession( );
            context.Items[SESSION_KEY] = session;
            ITransaction transaction = session.BeginTransaction( );
            context.Items[ TRANSACTION_KEY ] = transaction;
        }

        private void context_EndRequest( object sender, EventArgs e ) {
           
            HttpApplication application = ( HttpApplication )sender;
            HttpContext context = application.Context;

            ISession session = context.Items[SESSION_KEY] as ISession;
            ITransaction transaction = context.Items[ TRANSACTION_KEY ] as ITransaction;
           
            if ( transaction != null ) {

                try {
                   
                    transaction.Commit( );
                   
                } catch ( Exception ) {
                   
                    transaction.Rollback();
                    throw;
                }

            }
           
            if ( session != null ) {
                try {
                    session.Flush( );
                    session.Close( );
                } catch { }
            }

            context.Items[SESSION_KEY] = null;
            context.Items[TRANSACTION_KEY] = null;
        }

        public static ISession CurrentSession {

            get {

                HttpContext currentContext = HttpContext.Current;
                ISession session = currentContext.Items[SESSION_KEY] as ISession;

                if ( session == null ) {
                    session = NHibernateHelper.OpenSession( );
                    currentContext.Items[SESSION_KEY] = session;
                    ITransaction transaction = session.BeginTransaction( );
                    currentContext.Items[TRANSACTION_KEY] = transaction;
                }

                return session;
            }
        }
       
}

Any idea why this approach would not work?


Code:
`


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 11, 2006 4:19 pm 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
You can find a .NET implementation of this at
http://www.codeproject.com/aspnet/NHibernateBestPractices.asp.
Billy did a similar adaptation of Courtenay's code.

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject: Great reference!
PostPosted: Thu Oct 12, 2006 8:54 am 
Newbie

Joined: Mon Jul 17, 2006 3:22 pm
Posts: 2
mnichols,

The best practices document you pointed me to really helped - this implementation appears to be very similar to the filter approach from the Java side.

Do you have any personal experience / know of anyone using this approach in .NET? What will the application container do in the event an exception is thrown from the commit in the filter? I am still working on the implemenation so I haven't begun testing as of yet.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 12, 2006 1:13 pm 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
It depends on the flush mode you are using. Personally, i flushmode.commit so any exceptions occuring in my Repository are wrapped with a more generic PersistenceException and then bubbles on up to the Service Layer for handling.

If you look in Billy's NHibernateSessionManager class, you'll see the transactino being committed and if there are exceptions, the transaction is rolled back and then the exception rethrown, which in turn can be handled by layers higher up (like in a service layer that made the call inside a try...catch block).


This is a very common scenario for ASP.NET apps using NHibernate.

That help?

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.