落Nicety的博客

专注于web2.0

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  11 随笔 :: 0 文章 :: 12 评论 :: 0 Trackbacks

由于Spring控制的Hibernate的生命周期只针对数据层和服务层,而未管理到表现层,所以在表现层使用延时加载会出现the owning Session was closed或者no session or session was closed的异常信息。针对这一点,可以通过hibernate filter的方式来解决。

在WEB.xml文件中配置filter.
<!-- hibernate session filter -->
<filter>
    
<filter-name>OpenSessionInViewFilter</filter-name>
    
<filter-class>
        org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    
</filter-class>
</filter>

<filter-mapping>
    
<filter-name>OpenSessionInViewFilter</filter-name>
    
<url-pattern>*.do</url-pattern>
</filter-mapping>
我们的系统架构是struts+spring+hibernate,struts跟spring的整合是在struts-config.xml里加了个plugin
<plug-in
    
className="org.springframework.WEB.struts.ContextLoaderPlugIn">
    
<set-property property="contextConfigLocation"
        value
="/WEB-INF/classes/applicationContext.xml" />
    
</plug-in>

在WEB.xml中配置hibernateFilter 后,还需要在struts-config.xml里把plugin去掉,在WEB.xml里加上如下代码:
<!--Spring ApplicationContext-->
<context-param>
    
<param-name>contextConfigLocation</param-name>
    
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>

<listener>
    
<listener-class>
        org.springframework.web.context.ContextLoaderListener
    
</listener-class>
</listener>

这样配置之后如果没有配置事务,是有问题的。不能进行update和insert操作了。
怎么办呢?只需要在filter中加入一个参数
<init-param>
    <param-name>singleSession</param-name>
    <param-value>true</param-value>
</init-param>
就可以了,当然这样 每次访问dao都会新开个session,对性能的影响还是比较大的。最好的办法当然是配置事务了。
posted on 2008-04-18 10:12 落Nicety 阅读(262) 评论(0)  编辑  收藏 所属分类: 基础

标题  
姓名  
主页
验证码 *  
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2008-05-19 17:50 编辑过
 
 
相关链接:
网站导航: