General forum

They say that building a factory session in NHibernate is expensive and that it only needs to happen once. On this, I use a singleton approach. This is the first time a session is requested.

My question is: will there be a time when you should close the factory session? If so, when to do it?

+3
source share
4 answers

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 no longer need it. I think, but to be honest, I never closed a factory session

+6
source

AZ: SessionFactory, . , SessionFactory. . , , SessionFactory.

+2

, , .

+1

sessionfactory. . , Spring . factory

0

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


All Articles