SSH中懒加载异常的解决办法

 

web.xml 的配置文件

    <!-- 配置Spring解决懒加载问题的过滤器 -->
    <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>*.action</url-pattern>
    </filter-mapping>
  
    <!-- 配置Struts2的主过滤器 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

注意  :   一定要配置在struts过滤器的上面, 否则不起作用

 

使用了OpenSessionInView模式可能造成内存和数据库连接问题
由于使用了OpenSessionInView模式,Session的生命周期变长。虽然解决了 Lazy Load的问题,但是带来的问题就是Hibernate的一级缓存,也就是Session级别的缓存的生命周期会变得非常长,如果在Service层做大批量的数据操作时,其实这些数据会在缓存中保留一份,这是非常耗费内存的。还有一个数据库连接的问题,存在的原因在于由于数据库的 Connection是和Session绑在一起的,所以,Connection也会得不到及时的释放。因而当系统出现业务非常繁忙,而计算量又非常大的时候,数据连接池的连接数会不够

posted @ 2014-12-31 18:32  99°的开水  阅读(1193)  评论(0编辑  收藏  举报