nhibernate sessionfactory 实例在 Web 服务上多次
我有一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想你已经提到了解决方案。您必须对 Sessionfactory-Provider 使用 Singleton。就我个人而言,我更喜欢使用 Spring 来管理我的 ApplicationContext 并连接我的对象,但你不必这样做。只需不要将 NHibernatePersistencia-Object 用作服务,而将其用作 SessionProvider 就可以了。
您的服务可能如下所示:
...对于您的 SessionProvider,我建议始终根据请求打开新会话。长期会话缓慢且不断增长。考虑到网络服务中的多个用户,这似乎不是一个好主意。
这很简单,但应该可行。也许你想看看这个 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:
...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.
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.