zoukankan      html  css  js  c++  java
  • OpenSessionInViewFilter与OpenSessionInViewInterceptor

        在没有使用Spring提供的Open Session In View情况下,事务提交时会在service(or Dao)层里把session关闭,在lazy loading 为true的话,这样就只能使用get()方法,若使用load()方法,Hibernate抛session already closed Exception;   

        Open Session In View提供了一种简便的方法,较好地解决了lazy loading问题.  

        说明一下Open Session in View的作用,就是允许在每次的整个request的过程中使用同一个hibernate session,可以在这个request任何时期lazy loading数据。 
    如果是singleSession=false的话,就不会在每次的整个request的过程中使用同一个hibernate session,而是每个数据访问都会产生各自的seesion,等于没有Open Session in View. 
    OpenSessionInViewFilter默认是不会对session 进行flush的,并且flush mode 是 never 

    spring的OpenSessionInViewFilter过滤器,主要是为了实现Hibernate的延迟加载功能,基于一个请求一个hibernate session的原则。   

        它有两种配置方式OpenSessionInViewInterceptor和OpenSessionInViewFilter,功能相同,只是一个在web.xml配置,另一个在spring的xml文件中配置而已。OpenSessionInViewFilter多用于非spring MVC框架,OpenSessionInViewInterceptor多用于Spring MVC框架。

       Open Session In View在request把session绑定到当前thread期间一直保持hibernate session在open状态,使session在request的整个期间都可以使用,如在View层里PO也可以lazy loading数据,如 ${ company.employees }。当View 层逻辑完成后,才会通过Filter的doFilter方法或Interceptor的postHandle方法自动关闭session。

       

    对于OpenSessionInViewFilter,需要在web.xml文件中配置。但一定要放在Struts   filter的前面

    OpenSessionInViewFilter调用流程: 

    request(请求)->open session并开始transaction->controller->View(Jsp)->结束transaction并 close session.

    配置:

    <filter>  
      <description>处理Hibernate的懒加载问题description>  
      <filter-name>OpenSessionInViewFilter<filter-name>  
      <filter-class>  
       org.springframework.orm.hibernate4.support.OpenSessionInViewFilter   
     </filter-class>  
      <init-param>  
        <description>  
           默认情况下,这个Filter会在Spring的bean池中找一个叫做sessionFactory的bean。如果使用了其他名字的SessionFactory,则应该在这里   
           指定这个名字。   
        </description>  
        <param-name>sessionFactoryBeanName</param-name>  
        <param-value>指定的名字</param-value>  
      </init-param>  
      <init-param>  
        <description></description>  
        <param-name>singleSession</param-name>  
        <param-value>true</param-value>  
      </init-param>  
    </filter>  
    <filter-mapping>  
      <filter-name>OpenSessionInViewFilter</filter-name>  
      <url-pattern>/*</url-pattern> 
    </filter-mapping>  

    其中的sessionFactoryBeanName是你的SessionFactory的实例的名字。这个名字是你在Spring的Bean定义文件中声明的SessionFactory的实例的bean名字。默认这个Filter会使用“sessionFactory”这个值,如果你的SessionFactory确实是这个名字的话,可以不用指明它的名字。但是如果你向我一样,给SessionFactory使用了其他名字,就得在这里声明了。

    OpenSessionInViewInterceptor,就纯粹是Spring IOC容器管理的东西了。

        首先,你需要在Spring配置文件中声明一个OpenSessionInViewInterceptor的实例

    <beans>    
    <bean name="openSessionInViewInterceptor" class="org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor">    
    <property name="sessionFactory">    
    <ref bean="sessionFactory"/>    
    </property>    
    </bean>    
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">    
    <property name="interceptors">    
    <list>    
    <ref bean="openSessionInViewInterceptor"/>    
    </list>    
    </property>    
    <property name="mappings">    
    ...    
    </property>    
    </bean> ... </beans>   

    但它也不是万能的,由于需要连接时间比较长,对于数据量大、访问量大的站点一定要慎用!!!

  • 相关阅读:
    从寻找资源的习惯上谈如何获得好的代码及控件(使用Koders查找)
    数据仓库的一些基本知识2
    淘宝API开发系列商家的绑定
    淘宝API开发系列开篇概述
    读取实体类的属性的备注作为表头的应用
    设置Windows服务允许进行桌面交互,实现屏幕监控
    C#进行Visio二次开发之组合形状操作
    吉日嘎啦通用权限管理系统解读及重构升华高度封装的编辑窗体
    WinForm界面开发之模块化分合
    CLR中的内存管理
  • 原文地址:https://www.cnblogs.com/90zyh/p/3109253.html
Copyright © 2011-2022 走看看