NHibernate session (and stateless conversation) and long-running applications

In the context of Windows Web Services for running jobs, we try to reuse the NHibernate DAL we developed for Web applications.

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

Stateful meetings

>As everything is tracked (L1/session cache) will grow a lot
>It needs to be closed carefully, the session processing seems to be insufficient to clear the L1 cache (I noticed using the memory analyzer)

Stateless Conference

>Currently, the mapping cannot be reused. The statement is All bags with “lazy = true” will end up with the following exception (even if the session has not been closed):

Initializing […] failed to lazily initialize a collection of role:
[…], no session or session was closed

Obviously, we cannot update the mapping with lazy=”false” (they are shared with the web application), which will be a huge disadvantage for performance< /p>

>Cannot interact with the L2 cache: When the shared L2 cache is deployed, the service will not be able to invalidate the L2 cache data so that the Web application can get the latest data

NHibernate has been so far It turns out to be good, we have successfully used stateful sessions and NHibernate LINQ in the Web context, and used the structure diagram of dependency injection.

My question is:

> Are there any good solutions for using NHibernate in long-running threads?
>I prefer to use stateful sessions, but how to avoid memory leaks?

The problem is solved! There are actually several questions.

The first one is about the scope of the instance and multithreading:

>Create a new session for each thread.
> Once the thread finishes its work, clear all instances connected to the thread. Using StructureMap, in the thread, use the new HybridLifecycle().FindCache().DisposeAndClear();. It will cause the session attached to the thread to be closed and disposed.
>When the life cycle is thread scoped, StructureMap uses the ThreadStatic variable to keep a reference to the object cache. So the trick is to call StructureMap’s ObjectFactory in the thread. Initially, in our application, the main thread is responsible for creating new threads , And call ObjectFactory. This is the main mistake we made, and once the work is done, we really can’t clean up the thread.

Session type:

>Just handle the instantiated StateFul session carefully , There is no need to use StateLessSession. In our example, StatelessSession has too many shortcomings (cache management is the main one)

Important note: Be careful to instantiate NHibernate NHibernate Session Factory once!

When carefully managing the NHibernate instance, there is no memory leak.

In the context of the Windows Web service used to run the job, we try to reuse our NHibernate DAL for web application development.

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

Stateful meetings< /p>

>It will grow a lot as everything is tracked (L1/session cache)
>It needs to be closed carefully, the session processing does not seem to be enough to clear the L1 cache (I noticed using the memory analyzer)

Stateless meetings

>Currently, the mapping cannot be reused. All bags declared as “lazy = true” will end up with the following exception (even if the session has not been closed):

Initializing […] failed to lazily initialize a collection of role:
[…], no session or session was closed

Obviously, we cannot update the mapping with lazy = “false” (They are shared with the web application), which will be a huge disadvantage for performance

>Cannot interact with L2 cache: When the shared L2 cache is deployed, the service will not be able to invalidate the L2 cache data So that the web application can get the latest data

NHibernate has proven to be good so far, we have successfully used stateful sessions and NHibernate LINQ in the web context, and used the structure diagram of dependency injection .

My question is:

>Are there any good solutions for using NHibernate in long-running threads?
>I prefer to use stateful sessions, but how to avoid memory leaks?

The problem is solved! There are actually several questions.

The first one is about the scope of the instance and multithreading:

>Create a new session for each thread.
> Once the thread finishes its work, clear all instances connected to the thread. Using StructureMap, in the thread, use the new HybridLifecycle().FindCache().DisposeAndClear();. It will cause the session attached to the thread to be closed and disposed.
>When the life cycle is thread scoped, StructureMap uses the ThreadStatic variable to keep a reference to the object cache. So the trick is to call StructureMap’s ObjectFactory in the thread. Initially, in our application, the main thread is responsible for creating new threads , And call ObjectFactory. This is the main mistake we made, and once the work is done, we really cannot clean up the thread.

Session type:

>Just handle the instantiated StateFul session carefully , There is no need to use StateLessSession. In our example, StatelessSession has too many shortcomings (cache management is the main one)

Important note: Be careful to instantiate NHibernate NHibernate Session Factory once!

When carefully managing the NHibernate instance, there is no memory leak.

Leave a Comment

Your email address will not be published.