• Spring配置之OpenSessionInViewFilter


    转自:https://www.cnblogs.com/blogonfly/articles/3991782.html

    参考: 
    OpenSessionInViewFilter作用及配置:http://www.yybean.com/opensessioninviewfilter-role-and-configuration 
    http://blog.csdn.net/fooe84/article/details/680449 

    主要涉及类: 
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter 
    org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor 

    一、作用 
        说明一下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(具体参看SpringSide),功能相同,只是一个在web.xml配置,另一个在application.xml配置而已。 

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

    Xml代码  
    复制代码
    <!--Hibernate Open Session in View Filte-->  
    <filter>  
        <filter-name>hibernateFilter</filter-name>  
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>  
        <!-- singleSession默认为true,若设为false则等于没用OpenSessionInView -->  
        <init-param>  
            <param-name>singleSession</param-name>  
            <param-value>true</param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>hibernateFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  
    复制代码


    2,applicationContext.xml配置openSessionInViewInterceptor 

    Xml代码  
    <bean id="openSessionInViewInterceptor"  
        class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">  
        <property name="sessionFactory" ref="sessionFactory" />  
    </bean>  


    三、注意 
    尽 管Open Session In View看起来还不错,其实副作用不少。看回上面OpenSessionInViewFilter的doFilterInternal方法代码,这个方法实际上是被父类的doFilter调用的,因此,我们可以大约了解的OpenSessionInViewFilter调用流程: 

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

    一切看起来很正确,尤其是在本地开发测试的时候没出现问题,但试想下如果流程中的某一步被阻塞的话,那在这期间connection就一直被占用而不释放。最有可能被阻塞的就是在写Jsp这步,一方面可能是页面内容大,response.write的时间长,另一方面可能是网速慢,服务器与用户间传输时间久。当大量这样的情况出现时,就有连接池连接不足,造成页面假死现象。 

    Open Session In View是个双刃剑,放在公网上内容多流量大的网站请慎用

  • 相关阅读:
    深入理解volatile
    定时任务分布式锁的简单实现
    spring boot在tomcat运行多环境配置分离方案
    java spring boot data jpa和javaagent兼容问题
    RabbitMQ PHP操作类,守护进程及相关测试数据
    Python httpsqs封装类
    Python守护进程(多线程开发)
    python游戏打包exe
    处理谷歌地图marker旋转
    在vue-cli项目中mockjs和vConsole的使用
  • 原文地址:https://www.cnblogs.com/sharpest/p/7723607.html
Copyright © 2020-2023  润新知