I'm using the session-per-request pattern for nhibernate and am running out
of database connections. This takes about 1 week to occur on a heavily
loaded server, taking about 100,000 hits per day. The code I use to manage
my session is below. My theory is that the httpsession could timeout before
the EndRequest event, causing the reference to the session to be lost, and
therefore no closed. Is that even possible? If so, what can be done about
it?

Public Class NHibernateSessionPerRequestModule
    Implements IHttpModule

    Public Sub New()
    End Sub


    ' In the Init function, register for HttpApplication
    ' events by adding your handlers.
    Public Sub Init(ByVal application As HttpApplication) _
            Implements IHttpModule.Init
        AddHandler application.BeginRequest, _
            AddressOf Me.Application_BeginRequest
        AddHandler application.EndRequest, _
            AddressOf Me.Application_EndRequest
    End Sub

    Private Sub Application_BeginRequest(ByVal source As Object, _
             ByVal e As EventArgs)
        'Fires when the request begins
        Try
            If (HttpContext.Current.Request.Path.ToLower().IndexOf(".aspx")
> -1) Then
                Dim session As NHibernate.ISession =
StaticSessionManager.OpenSession()
                session.BeginTransaction()
                CurrentSessionContext.Bind(session)
            End If
        Catch ex As Exception
            Dim objLoggingController As New
AlertPay.Utilities.LoggingController
            objLoggingController.Write("Error in creating NHibernate
Session", "Information", 0, "0", "NHibernateSessionPerRequestModule")
        End Try


    End Sub

    Private Sub Application_EndRequest(ByVal source As Object, _
        ByVal e As EventArgs)

        If
(CurrentSessionContext.HasBind(StaticSessionManager.SessionFactory)) Then
            Dim session As NHibernate.ISession =
CurrentSessionContext.Unbind(StaticSessionManager.SessionFactory)
            If Not session Is Nothing Then
                Try
                    session.Transaction.Commit()
                Catch
                    session.Transaction.Rollback()
                    Dim objLoggingController As New
AlertPay.Utilities.LoggingController
                    objLoggingController.Write("Error in commiting
NHibernate Session. Rolling back transaction", "Information", 0, "0",
"NHibernateSessionPerRequestModule")
                Finally
                    If session.IsOpen Then
                        session.Close()
                    End If
                End Try
            End If
        End If



    End Sub

    Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
    End Sub

End Class


-- 
Robin Nadeau, B.Eng.
Software Developer

--~--~---------~--~----~------------~-------~--~----~
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