We're using NHibernate in and asp.net MVC application.

We are implementing the Session per Request pattern, via a httpModule.

It looks pretty straight forward, but when we run with NHibernate Profiler, it 
clearly shows that the
sessions are never getting closed.

The pattern seems straight forward...but I don't understand why the sessions 
are never closing.

Here's the code I think is important.


set up the event handler:

        context.EndRequest += new EventHandler(this.context_EndRequest);



in the handler dispose the Session

    private void context_EndRequest(object sender, EventArgs e)
            {
                netLogHdl.ArchDebug("NHibernateHttpModule.context_EndRequest() 
");

                Dispose(0);// we are hitting 2 dbs and thus keep one session 
for each.
                Dispose(1);

                HttpContextBuildPolicy.DisposeAndClearAll();
            }


    private void Dispose(int sessionIndex)
            {
                netLogHdl.ArchStart("NHibernateHttpModule.Dispose", "int 
sessionIndex=\" + sessionIndex + \")");

                try
                {
                    //close the DB session
                    string sessManagerName = "";
                    string jcdcManager = "JCDC Manager";
                    string spamisManager = "Spamis Manager";

                    if (sessionIndex == 0)
                        sessManagerName = jcdcManager;
                    else
                    {
                        sessManagerName = spamisManager;
                    }


                    ISession oneSession = sessionPerDB[sessionIndex];
                    if (oneSession != null)
                    {
                        if (sessManagerName == jcdcManager) 
netLogHdl.ArchDebug(sessManagerName + " oneSession is NOT null");

                        if (oneSession.IsOpen)
                        {
                            // Don't flush - all saves should use transactions 
and calling Commit does the flush.
                            if (sessManagerName == jcdcManager) 
netLogHdl.ArchDebug(sessManagerName + " Closing the session");

                            //This will overrite it with the exact same 
session, if they don't match something weird is going on - EWB
                            oneSession = 
CurrentSessionContext.Unbind(factoryPerDB[sessionIndex]);
                            oneSession.Close();
                        }
                        else
                        {
                            if (sessManagerName == jcdcManager) 
netLogHdl.ArchDebug(sessManagerName + " Session is NOT open");
                        }

                        //if ( sessManagerName == jcdcManager ) 
netLogHdl.ArchDebug( sessManagerName + " Session got Dispose()-ing" );
                        //oneSession.Dispose();
                    }
                    else
                    {
                        if (sessManagerName == jcdcManager) 
netLogHdl.ArchDebug(sessManagerName + " Session is NULL");
                    }

                    sessionPerDB[sessionIndex] = null;

                }
                catch (Exception)
                {

                    throw;
                }


                netLogHdl.ArchEnd();
            }

Can anyone point me in the right direction? What should I look at, is the 
pattern not implemented correctly?

I'm flummoxed

Thanks!

E-

--

You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.


Reply via email to