nhibernate会话管理器的实现
我是 Nhibernate 的新手,并且在学习过程中放慢了速度。我尝试实现一个会话管理器类来帮助我获取数据库调用的会话。下面是它的代码。有人可以说这在架构上是否正确并预见到可扩展性或性能的任何问题吗?
public static class StaticSessionManager
{
private static ISession _session;
public static ISession GetCurrentSession()
{
if (_session == null)
OpenSession();
return _session;
}
private static void OpenSession()
{
_session = (new Configuration()).Configure().BuildSessionFactory().OpenSession();
}
public static void CloseSession()
{
if (_session != null)
{
_session.Close();
_session = null;
}
}
}
在我的数据提供程序类中,我使用以下代码来获取数据。
public class GenericDataProvider<T>
{
NHibernate.ISession _session;
public GenericDataProvider()
{
this._session = StaticSessionManager.GetCurrentSession();
}
public T GetById(object id)
{
using (ITransaction tx = _session.BeginTransaction())
{
try
{
T obj = _session.Get<T>(id);
tx.Commit();
return obj;
}
catch (Exception ex)
{
tx.Rollback();
StaticSessionManager.CloseSession();
throw ex;
}
}
}
}
然后
public class UserDataProvider : GenericDataProvider<User>
{
public User GetUserById(Guid uid)
{
return GetById(uid)
}
}
页面中的最终用法
UserDataProvider udp = new UserDataProvider();
User u = udp.GetUserById(xxxxxx-xxx-xxx);
这是正确的吗?在单个页面中实例化大量数据提供程序会导致问题吗?
我现在还面临一个问题,如果我同时从多台机器执行相同的读取操作,Nhibernate 会抛出随机错误 - 我认为这是由于事务造成的。
请指教。
I am new to Nhibernate and slowing working my way thru learning it. I tried to implement a session manager class to help me get the session for my db calls. Below is the code for it. Can someone please say if this is architecturally correct and foresee any issue of scalability or performance?
public static class StaticSessionManager
{
private static ISession _session;
public static ISession GetCurrentSession()
{
if (_session == null)
OpenSession();
return _session;
}
private static void OpenSession()
{
_session = (new Configuration()).Configure().BuildSessionFactory().OpenSession();
}
public static void CloseSession()
{
if (_session != null)
{
_session.Close();
_session = null;
}
}
}
and in my data provider class, I use the following code to get data.
public class GenericDataProvider<T>
{
NHibernate.ISession _session;
public GenericDataProvider()
{
this._session = StaticSessionManager.GetCurrentSession();
}
public T GetById(object id)
{
using (ITransaction tx = _session.BeginTransaction())
{
try
{
T obj = _session.Get<T>(id);
tx.Commit();
return obj;
}
catch (Exception ex)
{
tx.Rollback();
StaticSessionManager.CloseSession();
throw ex;
}
}
}
}
and then
public class UserDataProvider : GenericDataProvider<User>
{
public User GetUserById(Guid uid)
{
return GetById(uid)
}
}
Final usage in Page
UserDataProvider udp = new UserDataProvider();
User u = udp.GetUserById(xxxxxx-xxx-xxx);
Is this something that is correct? Will instantiating lot of data providers in a single page cause issues?
I am also facing an issue right now, where if I do a same read operation from multiple machines at the same time, Nhibernate throws random errors- which I think is due to transactions.
Please advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,如果您有一个
null
会话,那么您正在构建会话工厂。您应该只在应用程序启动时调用BuildSessionFactory()
一次。在哪里执行此操作取决于您,有些人在 Global.asax 中的方法
application_start
中构建SessionFactory
,或者在您的情况下有一个sessionFactory< 的静态属性/code> 而不是
StaticSessionManager
类中的session
。我怀疑您的错误是由于您的会话工厂被多次构建所致!
还有一点是,有些人在每个请求开始时打开一个事务
_session.BeginTransaction()
,并在请求结束时commit
或rollback
每个请求。这为您提供了一个工作单元,这意味着您可能会丢失每个方法。所有这些都有待讨论,但我 99% 的代码都使用这种方法,没有任何问题。
From what I can see you are building the session factory if you have a
null
session. You should only callBuildSessionFactory()
once when the application starts.Where you do this is up to you, some people build the
SessionFactory
inside Global.asax in the methodapplication_start
or in your case have a static property forsessionFactory
instead ofsession
in yourStaticSessionManager
class.I suspect your errors are due to the fact that your session factory is being built multiple times!
Another point is that some people open a transaction
_session.BeginTransaction()
at the beginning of each request and eithercommit
orrollback
at the end of each request. This gives you a unit of work which means you can lose theon every method. All of this is open for debate but I use this method for 99% of all my code with no trouble at all.