I have an application that is growing in memory size with every hit to
our NHibernate repository object and not ever releasing that memory.
This has become very noticable since we've added database polling to
the application.. very simple query bringing back very little data but
growing the memory footprint with each hit.  Please explain to me what
is wrong with this pattern?

//pseudocode
Class NHibernateRepository

Private _factory as ISessionFactory

// Constructor injection handled with Windsor
Public Sub New NHibernateRepository(ISessionFactory factory)
     _factory = factory
End Sub

Public Sub List(Of TEntity)(dc as DetachedCriteria)
    Using session as ISession = _factory.BuildSessionFactory()
         Return session.List(dc)
    End Using
End Sub

So my first thought is that the session object is not being released.
So I moved the return outside of the using block (assigning the
results to a local IList and returning that).  No improvements.

Next thought is that maybe I shouldn't be disposing of the session
with every call.. if I want to do lazy loading an active session is
needed after all.  So I added a private Session property that only
builds the from the session factory if one does not already exist.

Private _session as ISession

Private Readonly Property Session()
    If _session Is Nothing Then
          _session = _factory.BuildSessionFactory()
    End If
    Return _session
End Property

This appears to solve the problem with the growing memory.  However I
am getting other errors that a datareader has already been opened for
an operation and needs to be closed before being reused.  Maybe I need
to work through these errors.  I'm just wondering if I'm heading in
the right direction.

Thanks!

Reply via email to