Here's what we use:

public class StaticSessionManager
{

        public static readonly ISessionFactory SessionFactory;

        static StaticSessionManager()
        {
            try
            {
                Configuration cfg = new Configuration();

                if (SessionFactory != null)
                    throw new Exception("trying to init SessionFactory
twice!");

                SessionFactory = cfg.Configure().BuildSessionFactory();
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex);
                throw new Exception("NHibernate initialization failed", ex);
            }
        }
        public static ISession OpenSession()
        {
            return SessionFactory.OpenSession();
        }
        public static void CloseSession()
        {
            SessionFactory.Close();
        }
}

On Wed, Feb 4, 2009 at 1:19 PM, Robin Nadeau <[email protected]> wrote:

> Use the singleton pattern.
>
>
> On Wed, Feb 4, 2009 at 10:00 AM, CCL <[email protected]> wrote:
>
>>
>> Hi,
>>
>> We have introduced NHibernate in a .NET web application, using a best
>> pactice for handling the lifecycle of sessions etc.
>>
>> After deploying the app, we noticed that the aspnet_wp process memory
>> kept growing until returning an OutOfMemoryExeption and recycling -
>> about every four days.
>>
>> Debugging memory dumps of the process showed that we had multiple
>> SessionFactory objects (168 objects in one dump).
>>
>> In the best practice, creation of the SessionFactory class was
>> triggered via the "Init()" function in global.asax. But reading about
>> that event, I realize that it is called once for every instance of
>> httpApplication which indeed can exist in multiple instances. So I
>> instead moved the creation of the SessionFactory to
>> "Application_OnStart()" in global.asax - which is only triggered once
>> for the entire application.
>>
>> This solved my growing memory problem - and I now only have ONE
>> SessionFactory object.
>>
>> But the question remains - what is the recommended approach, is it to
>> create the SessionFactory instance via Init() or Application_OnStart()
>> in global.asax? Looking around I cad find examples of both, but no
>> article comparing the approaches. Does anyone have a comment on this
>> matter? I solved my immediate problem, but perhaps I am missing
>> something else when I do this change...
>>
>> Best regards,
>> Carl
>>
>> >>
>>
>
>
> --
> Robin Nadeau, B.Eng.
> Software Developer
>



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