开发者

nhibernate sessionfactory instance more than once on web service

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 t开发者_开发百科his.miSessionFactory; }
    }

    #endregion

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜