NHibernate Session (and Stateless Session) and Long-Term Application

In the context of the Windows web service, which is designed to run tasks, we are trying to reuse the NHibernate DAL that we developed for the web application.

For session management, we have two options, each of which has its own advantages and disadvantages:

Sessional session

  • Continues to grow as it tracks everything (L1 cache / session)
  • It is necessary to close the session carefully, it seems to be insufficient to clear the L1 cache (which I noticed using the memory profiler).

idle session

  • Mappings cannot be reused at this time. All packages declared with "lazy = true" end with the following exception (even if the session is not closed):

Initialization [...] failed to lazily initialize the role collection: [...] the session or session is not closed

Obviously, it cannot update the mappings (they are shared with the web application) with lazy = "false", this will be a huge drawback for execution

  • Cannot interact with L2 cache: when deploying a shared L2 cache, the service will not be able to invalidate the L2 cache so that the web application has fresh updated data.

NHibernate has proven its effectiveness so far, we have successfully used the state session and NHibernate LINQ in a web context, with a structural map for dependency injection.

My questions:

  • Are there any good solutions for using NHibernate in a long stream?
  • I would rather use a stateful session, but how do I avoid a memory leak?
+4
source share
2 answers

The problem is solved! There were actually several problems.

The first of these concerns instance scope and multithreading:

  • Create a new session for each thread.
  • Once the thread has completed its work, clear all instances attached to the thread. With StructureMap in the stream, use new HybridLifecycle().FindCache().DisposeAndClear(); . This will close the session and attach to it.
  • When the life cycle is associated with a thread, StructureMap uses the ThreadStatic variable to maintain a reference to the object’s cache. So the trick is to call the StructureMap ObjectFactory in the stream. Initially, in our application, the main thread was responsible for creating new threads and calling ObjectFactory. This is the main mistake that we made, and really could not clean the threads after their work.

Session Type:

  • You do not need to use StateLessSession once the created StateFul instances are carefully configured. In our case, StatelessSession has too many flaws (main cache management)

Important Note: Be careful to create the NHibernate NHibernate Session Factory only once!

When NHibernate instances are managed carefully, there is no memory leak.

+2
source

It is never recommended to maintain an open session for a long time.

My suggestion is to redesign your process to separate the database related code from the non-database related code, so any database related operation can be stored in a short session.

0
source

Source: https://habr.com/ru/post/1438863/


All Articles