读书人

hibernate延迟加载session已封闭解决过

发布时间: 2012-11-20 09:55:43 作者: rapoo

hibernate延迟加载session已关闭解决过程
1.为了在jsp页面采用bean.property获取集合的数据,在表中配置 lazy="false"
2.发现连接数据库查询时会查询许多关联表,于是将 lazy="false" 改成 lazy="true".
3.页面取值时报错 session已关闭。org.apache.jasper.JasperException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role

4.在web.xml中添加
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
继续报错。failed to lazily initialize a collection of role
5.查询原因,struts没有交由spring管理。
6.改造struts-config.xml,和applicationContext.xml。将action改成
type="org.springframework.web.struts.DelegatingActionProxy"
添加<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="classpath:config/spring/applicationContext.xml" />
</plug-in>
7.重启,运行 继续报 failed to lazily initialize a collection of role
。SESSION已关闭。
8.网上查询 ,可能是 spring配置被加载了两次。web.xml一次,struts-config一次。
9.删除 struts-config.xml中的 <plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="classpath:config/spring/applicationContext.xml" />
</plug-in>
10.重启。报错,failed to lazily initialize a collection of role
11.跳转之前加上打印
//System.out.println("获得延迟加载的数据:"+myPaper.getPapercontents().size());
12.仔细看了下还是未能解决
13.找到问题所在,因为 action 到 jsp 中间 加了个中间页面 导致 session 没了
14.问题解决。在frame或者中转jsp 中不能直接打开jsp ,需要通过访问action的方式跳转
15.总结。OpenSessionInViewFilter 会在一个request的请求全过程打开hibernate session.(从进入action 到 页面展示结束)。如再打开一个jsp 将会报session已关闭。

1 楼 sdylag 2011-08-16 13.找到问题所在,因为 action 到 jsp 中间 加了个中间页面 导致 session 没了


有木有有啊?

不太详细。。。期待精彩。。。

读书人网 >软件架构设计

热点推荐