[转]Web项目下NHibernate的Session管理的解决方案
2007-11-07 10:47
141 查看
NHibernate的Session的管理一直是个问题,在系统开发中
如果有lazy="true",如果不对Session进行管理,会抛出以下错误:
CODE:
Failed to lazily initialize a collection - no session
[Copy to clipboard]
在Web项目下的解决方案,就是在Application_BeginRequest方法中打开Session并放入HttpContext,在Application_EndRequest方法中关闭Sesssion就可以了。
还好,NHibernate1.2已经提供了相关的支持,如下:
在web.config中加入以下代码
CODE:
<httpModules>
<add name="CurrentSessionModule" type="NHibernate.Example.Web.CurrentSessionModule" />
</httpModules>
[Copy to clipboard]
程序如下:
CODE:
using System;
using System.Web;
using NHibernate.Context;
namespace NHibernate.Example.Web
{
public class CurrentSessionModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(Application_BeginRequest);
context.EndRequest += new EventHandler(Application_EndRequest);
}
public void Dispose()
{
}
private void Application_BeginRequest(object sender, EventArgs e)
{
ManagedWebSessionContext.Bind(HttpContext.Current, ExampleApplication.SessionFactory.OpenSession());
}
private void Application_EndRequest(object sender, EventArgs e)
{
ISession session = ManagedWebSessionContext.Unbind(HttpContext.Current, ExampleApplication.SessionFactory);
if(session !=null)
{
if (session.Transaction.IsActive)
{
session.Transaction.Rollback();
}
if (session != null)
{
session.Close();
}
}
}
}
}
如果有lazy="true",如果不对Session进行管理,会抛出以下错误:
CODE:
Failed to lazily initialize a collection - no session
[Copy to clipboard]
在Web项目下的解决方案,就是在Application_BeginRequest方法中打开Session并放入HttpContext,在Application_EndRequest方法中关闭Sesssion就可以了。
还好,NHibernate1.2已经提供了相关的支持,如下:
在web.config中加入以下代码
CODE:
<httpModules>
<add name="CurrentSessionModule" type="NHibernate.Example.Web.CurrentSessionModule" />
</httpModules>
[Copy to clipboard]
程序如下:
CODE:
using System;
using System.Web;
using NHibernate.Context;
namespace NHibernate.Example.Web
{
public class CurrentSessionModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(Application_BeginRequest);
context.EndRequest += new EventHandler(Application_EndRequest);
}
public void Dispose()
{
}
private void Application_BeginRequest(object sender, EventArgs e)
{
ManagedWebSessionContext.Bind(HttpContext.Current, ExampleApplication.SessionFactory.OpenSession());
}
private void Application_EndRequest(object sender, EventArgs e)
{
ISession session = ManagedWebSessionContext.Unbind(HttpContext.Current, ExampleApplication.SessionFactory);
if(session !=null)
{
if (session.Transaction.IsActive)
{
session.Transaction.Rollback();
}
if (session != null)
{
session.Close();
}
}
}
}
}
相关文章推荐
- Web项目下NHibernate的Session管理的解决方案 【转】
- Web项目下NHibernate的Session管理的解决方案
- Maven Web项目配置Mybatis出现SqlSessionFactory错误的解决方案
- 解决方案:SSM+vue+webpack+vue-resource+vue-route路由项目验证session过期跳转至登陆界面
- web项目权限管理解决方案及shiro框架
- Javaweb项目session超时解决方案
- Asp.Net大型项目实践(4)-用NHibernate保存和查询我们的业务领域对象之NHibernate的Session管理与初始化(附源码)
- Asp.Net大型项目实践(4)-用NHibernate保存和查询我们的业务领域对象之NHibernate的Session管理与初始化(附源码)
- Maven Web项目配置Mybatis出现SqlSessionFactory错误的解决方案
- “无法从web服务器获取项目文件”解决方案
- 15款提高工作效率的 Web 项目管理工具
- web项目如何配置404页面和500页面的解决方案
- 创建Maven项目时提示web.xml is missing and <failOnMissingWebXml> is set to true错误解决方案
- IntelliJ IDEA 12创建Maven管理的Java Web项目(图解)
- Maven管理的Spring Web项目集成JUnit单元测试
- Web项目经理手册之需求变更管理
- Eclipse导入Web项目后代码不报错但项目报错(左上角有红叉)解决方案
- 在同一个tomcat下,不同的web项目共享session
- 微软项目管理解决方案解析
- JavaWeb开发中的Session管理