NHibernate的没有会话或会话关闭

问题描述:

错误NHibernate的没有会话或会话关闭

初始化[Core.Model.Account#2586] -failed懒洋洋地初始化角色的集合:Core.Model.Account.Roles,没有会话或会话被关闭

当我尝试使用NHibernate在我的web应用程序中登录时出现。我不明白的是如何关闭连接,因为当我调试到调用方法时,我可以打开使用的NHibernate会话的isOpen属性。

但是,只要延迟加载启动,显然NHibernate认为连接已关闭。这怎么会发生?

我按照建议在BeginRequest中启动我的NHibernate会话,并在EndRequest中关闭它们。

Account.cs(项目/命名空间/组装Core

public class Account 
{ 
    // ... 

    public virtual ISet<Role> Roles 
    { 
     get; 
     set; 
    } 

    // ... 

    public virtual bool HasPermission(Permission permission_) 
    { 
     foreach (Role role in Roles) { 
      if (role.Permissions.Contains(Permission.ALL) || role.Permissions.Contains(permission_)) { 
       return true; 
      } 
     } 
     return false; 
    } 
} 

Account.hbm.xml(项目/命名空间/组装Core

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> 
    <class name="Core.Model.Account,Core" table="accounts"> 
     <!-- ... --> 
     <set name="Roles" table="account_roles" lazy="true"> 
      <key column="account"/> 
      <many-to-many class="Core.Model.Role" column="role" /> 
     </set> 
    </class> 
</hibernate-mapping> 

全球.asax(在项目/命名空间/程序集Web

public AccountRepository Accounts; 
private static ISessionFactory _sessionFactory; 
private void Application_Start(object sender_, EventArgs e_) 
{ 
    _sessionFactory = new Configuration().Configure().BuildSessionFactory(); 
} 


private void Application_BeginRequest(object sender_, EventArgs e_) 
{ 
    ISession session = _sessionFactory.OpenSession(); 
    Accounts.SetSession(session); 
    HttpContext.Current.Items.Add("HibernateSession", session); 
} 

private void Application_EndRequest(object sender_, EventArgs e_) 
{ 
    ISession session = HttpContext.Current.Items["HibernateSession"] as ISession; 
    if (session != null && session.IsOpen) { 
     session.Close(); 
     HttpContext.Current.Items.Remove("HibernateSession"); 
    } 
} 

DecoratedSession.cs(在项目/命名空间/组件Web

private static HttpSessionState _session 
{ 
    get 
    { 
     return HttpContext.Current.Session; 
    } 
} 

public static T Get<T>(string key_) 
{ 
    if (_session == null || _session[key_] == null) { 
     return default(T); 
    } 
    return (T)_session[key_]; 
} 

public static void Set<T>(string key_, T value_) 
{ 
    _session[key_] = value_; 
} 

public static Account Account 
{ 
    get 
    { 
     return Get<Account>("Account"); 
    } 
    set 
    { 
     Set<Account>("Account", value); 
    } 
} 

login.aspx的(在项目/命名空间/组件Web

protected void loginButton_Click(object sender, System.EventArgs e) 
{ 
    DecoratedSession.Account = Global.Firewall.Login(tb_user.Text, tb_user.Text); 
    Response.Redirect("main.aspx", true); 
} 

主.aspx(在项目/命名空间/程序集Web

protected void Page_Load(object sender, System.EventArgs e) 
{ 
    // Error thrown here, when accessing the "roles" set 
    if (DecoratedSession.Account.HasPermission(Permission.FULL_ACCESS)) 
    { 
     // ... 
    } 
} 

我甚至可以在会话双头呆日志文件看,然后出现异常,然后将其关闭。

Global: 15:20:08,669 DEBUG Global - Opened session [529000] for request to [/main.aspx] 
Global: 15:20:08,670 DEBUG Global - STARTREQUEST Session id: [529000] 
main: 15:20:08,682 DEBUG main - MAIN Session id: [529000] 
NHibernate.LazyInitializationException: 15:20:08,685 ERROR NHibernate.LazyInitializationException - Initializing[Core.Model.Account#2586]-failed to lazily initialize a collection of role: Core.Model.Account.Roles, no session or session was closed 
NHibernate.LazyInitializationException: Initializing[Core.Model.Account#2586]-failed to lazily initialize a collection of role: Core.Model.Account.Roles, no session or session was closed 
Global: 15:20:14,160 DEBUG Global - Closed session [529000] for request to [/main.aspx] 

我该如何调试?

的位置点是(我没有深入到上面的代码,但基于没有我的经验)相关静态 VS HTTP请求生命周期。

虽然有打开的会话,被关闭指责为本届会议的对象来自以前的一些(与另一个网络请求)

检查一些细节在这里:

解决方案我在介绍某种原型模式时看到:

[Serializable] 
public class Account, ICloneable, ... 
{ 
    ... 
    public override object Clone() 
    { 
     var entity = base.Clone() as Account; 
     entity.Roles = ... (clone roles) 
     ... 
     return entity; 
    } 
    ... 

而对于认证加载帐户(用户,角色)的时候,我们应该叫Clone(),以确保所有重要特性(多到一,一到多)加载,并随后准备使用。该克隆可以缓存在静态环境中

+0

但是,这很难成为如何处理NHibernate的“登录”会话的正确方式,对吧?我在另一个网络应用中做了完全相同的事情,它在那里工作得很好...... –

+1

老实说,我的所有安全性总是被加载一次,克隆和缓存。它后来与当前用户进行比较... –

+0

好的,感谢您的输入。我想我弄明白了为什么它可以在我的其他应用程序中工作:在一个有效的工作中,在我从数据库中获得'Account'后,立即访问'Roles/Permissions'字段,因此强制它加载到会话中。我不会在另一个应用程序上这样做,因此错误...您能解释您如何“克隆和缓存”安全细节?我想我也需要这样做。 –