NHibernate SessionFactory

Mor*_*rph 4 c# nhibernate

他们说在NHibernate中构建一个会话工厂是昂贵的,它应该只发生一次.我对此使用单例方法.这是在第一次请求会话时完成的.

我的问题:你是否应该每次都关闭Session工厂?如果是这样,人们什么时候会这样做?

Lli*_*ane 6

这就是我用Java做的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;
    }

}
Run Code Online (Sandbox Code Playgroud)

当你不再需要它时,你可以释放你的SessionFactory我想,但说实话,我从来没有关闭我的会话工厂

  • 在.NET中几乎一样.由于FluentNHibernate,只有配置略有不同. (2认同)