NHibernate SessionFactory

c#, nhibernate

Solution

This is what i do in Java with Hibernate :

 public class HibernateUtil
{

    private static final SessionFactory sessionFactory;

    static
    {
        try
        {
            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory = new Configuration().configure().buildSessionFactory();
        }
        catch (Throwable ex)
        {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory()
    {
        return sessionFactory;
    }

}

You can free your SessionFactory when you don't need it anymore I guess, but honestly I've never closed my session factory

Problem

They say that to build a session factory in NHibernate is expensive and that it should only happen once. I use a singleton approach on this. This is done on the first time that a session is requested. My question : Would there every be a time when you should close the Session factory? If so, when would one do this?

Original source