nhibernate sessionfactory 实例在 Web 服务上多次

发布于 2024-10-12 04:47:59 字数 1807 浏览 7 评论 0原文

我有一个使用 nhibernate 的网络服务。我在存储库库上有一个单例模式,但每次调用服务时,它都会创建一个会话工厂的新实例,这是非常昂贵的。我能做些什么?

    #region Atributos

    /// <summary>
    /// Session
    /// </summary>
    private ISession miSession;

    /// <summary>
    /// Session Factory
    /// </summary>
    private ISessionFactory miSessionFactory;
    private Configuration miConfiguration = new Configuration();
    private static readonly ILog log = LogManager.GetLogger(typeof(NHibernatePersistencia).Name);

    private static IRepositorio Repositorio;

    #endregion

    #region Constructor

    private NHibernatePersistencia()
    {
        //miConfiguration.Configure("hibernate.cfg.xml");
        try
        {
            miConfiguration.Configure();
            this.miSessionFactory = miConfiguration.BuildSessionFactory();
            this.miSession = this.SessionFactory.OpenSession();
            log.Debug("Se carga NHibernate");

        }
        catch (Exception ex)
        {
            log.Error("No se pudo cargar Nhibernate " + ex.Message);
            throw ex;
        }

    }

    public static IRepositorio Instancia
    {
        get
        {
            if (Repositorio == null)
            {
                Repositorio = new NHibernatePersistencia();
            }
            return Repositorio;
        }
    }

    #endregion

    #region Propiedades

    /// <summary>
    /// Sesion de NHibernate
    /// </summary>
    public ISession Session
    {
        get { return miSession.SessionFactory.GetCurrentSession(); }
    }

    /// <summary>
    /// Sesion de NHibernate
    /// </summary>
    public ISessionFactory SessionFactory
    {
        get { return this.miSessionFactory; }
    }

    #endregion

我可以通过什么方式为所有服务创建单个实例?

I have a web service that use nhibernate. I have a singleton pattern on the repositorry library but on each call the service, it creates a new instance of the session factory which is very expensive. What can i do?

    #region Atributos

    /// <summary>
    /// Session
    /// </summary>
    private ISession miSession;

    /// <summary>
    /// Session Factory
    /// </summary>
    private ISessionFactory miSessionFactory;
    private Configuration miConfiguration = new Configuration();
    private static readonly ILog log = LogManager.GetLogger(typeof(NHibernatePersistencia).Name);

    private static IRepositorio Repositorio;

    #endregion

    #region Constructor

    private NHibernatePersistencia()
    {
        //miConfiguration.Configure("hibernate.cfg.xml");
        try
        {
            miConfiguration.Configure();
            this.miSessionFactory = miConfiguration.BuildSessionFactory();
            this.miSession = this.SessionFactory.OpenSession();
            log.Debug("Se carga NHibernate");

        }
        catch (Exception ex)
        {
            log.Error("No se pudo cargar Nhibernate " + ex.Message);
            throw ex;
        }

    }

    public static IRepositorio Instancia
    {
        get
        {
            if (Repositorio == null)
            {
                Repositorio = new NHibernatePersistencia();
            }
            return Repositorio;
        }
    }

    #endregion

    #region Propiedades

    /// <summary>
    /// Sesion de NHibernate
    /// </summary>
    public ISession Session
    {
        get { return miSession.SessionFactory.GetCurrentSession(); }
    }

    /// <summary>
    /// Sesion de NHibernate
    /// </summary>
    public ISessionFactory SessionFactory
    {
        get { return this.miSessionFactory; }
    }

    #endregion

In which way can i create a single instance for all services?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酸甜透明夹心 2024-10-19 04:47:59

我想你已经提到了解决方案。您必须对 Sessionfactory-Provider 使用 Singleton。就我个人而言,我更喜欢使用 Spring 来管理我的 ApplicationContext 并连接我的对象,但你不必这样做。只需不要将 NHibernatePersistencia-Object 用作​​服务,而将其用作 SessionProvider 就可以了。

您的服务可能如下所示:

public class YourService : IYourService
{
    public User GetUsers(int id)
    {
        using(NHibernatePersistencia.OpenSession())
        {
            return session.Load(typeof(user), id);
        }
    }      
}

...对于您的 SessionProvider,我建议始终根据请求打开新会话。长期会话缓慢且不断增长。考虑到网络服务中的多个用户,这似乎不是一个好主意。

public class NHibernatePersistencia
{
    /* ...  */
    public ISession OpenSession()
    {
        return this.SessionFactory.OpenSession();
    }
    /* ...  */
}

这很简单,但应该可行。也许你想看看这个 nhibernate .info 示例。 NHibernateHelper 与 NHibernatePersistencia-Class 非常相似。

i think you already mentioned the solution. You have to use a Singleton for the Sessionfactory-Provider. Personally i prefer to use Spring to manage my ApplicationContext an wire my objects but you don't have to. Simply don't use the NHibernatePersistencia-Object as a Service but as a SessionProvider and you are fine.

Your Service could look like this:

public class YourService : IYourService
{
    public User GetUsers(int id)
    {
        using(NHibernatePersistencia.OpenSession())
        {
            return session.Load(typeof(user), id);
        }
    }      
}

...and for you SessionProvider i'd suggest to always open new sessions per request. Long term sessions are slow and growing. Considering multiple users in a webservice this does not seem to be a good idea.

public class NHibernatePersistencia
{
    /* ...  */
    public ISession OpenSession()
    {
        return this.SessionFactory.OpenSession();
    }
    /* ...  */
}

It's simple but should work. Maybe you want to take a look at this nhibernate.info example. The NHibernateHelper there is pretty much like your NHibernatePersistencia-Class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文