参考:
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

  1. <!--Hibernate Open Session in View Filte-->
  2. <filter>
  3. <filter-name>hibernateFilter</filter-name>
  4. <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
  5. <!-- singleSession默认为true,若设为false则等于没用OpenSessionInView -->
  6. <init-param>
  7. <param-name>singleSession</param-name>
  8. <param-value>true</param-value>
  9. </init-param>
  10. </filter>
  11. <filter-mapping>
  12. <filter-name>hibernateFilter</filter-name>
  13. <url-pattern>/*</url-pattern>
  14. </filter-mapping>
	<!--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

  1. <bean id="openSessionInViewInterceptor"
  2. class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
  3. <property name="sessionFactory" ref="sessionFactory" />
  4. </bean>
	<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是个双刃剑,放在公网上内容多流量大的网站请慎用

[转]Spring配置之OpenSessionInViewFilter的更多相关文章

  1. Spring配置之OpenSessionInViewFilter

    转自:https://www.cnblogs.com/blogonfly/articles/3991782.html 参考: OpenSessionInViewFilter作用及配置:http://w ...

  2. Spring配置c3p0数据源时出错报:java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector

    今天在使用Spring配置c3p0数据源时,使用的数据库是mysql,服务器是tomcat,运行时报了一个 java.lang.NoClassDefFoundError: com/mchange/v2 ...

  3. Spring配置汇总

    现在主流的JavaWeb应用几乎都会用到Spring,以下是Spring的配置,以及结合Web的SpringMVC配置的汇总. jar包的引入 与Web项目集成 Spring配置文件 SpringMV ...

  4. spring配置属性的两种方式

    spring配置属性有两种方式,第一种方式通过context命名空间中的property-placeholder标签 <context:property-placeholder location ...

  5. 解决eclipse spring配置报错:cvc-elt.1: Cannot find the declaration of element

    解决eclipse spring配置报错:cvc-elt.1: Cannot find the declaration of element 'beans'.Referenced file conta ...

  6. spring配置详解

    1.前言 公司老项目的后台,均是基于spring框架搭建,其中还用到了log4j.jar等开源架包.在新项目中,则是spring和hibernate框架均有使用,利用了hibernate框架,来实现持 ...

  7. memcached 学习 1—— memcached+spring配置

    memcached 学习目录: memcached 学习 1—— memcached+spring配置 这几天自己搭建项目环境,解决问题如下: 有关常见的配置这里没有列出,中间遇到的搭建问题比较顺利g ...

  8. 解决spring配置中的bean类型的问题:BeanNotOfRequiredTypeException

    解决spring配置中的bean类型的问题:BeanNotOfRequiredTypeException这个问题出现的原因:一般在使用annotation的方式注入spring的bean 出现的,具体 ...

  9. spring配置中,properties文件以及xml文件配置问题

    spring方便我们的项目快速搭建,功能强大,自然也会是体系复杂! 这里说下配置文件properties管理的问题. 一些不涉及到代码逻辑,仅仅只是配置数据,可以放在xxxx.properties文件 ...

随机推荐

  1. 命令行创建KVM虚拟机

    qemu命令创建虚拟机: qemu-img create -f qcow2 /home/ubuntu.img 20G   qemu-system-x86_64 -m 2048 -enable-kvm ...

  2. python实战===国内很简单实用的一些开源的api以及开源项目

    原创 2017年03月25日 15:40:59 标签: api / 开源项目 / app / 免费接口   声明 以下所有 API 均由产品公司自身提供,本人皆从网络获取.获取与共享之行为或有侵犯产品 ...

  3. Kotlin 学习使用之旅(二)

    为什么从二开始呢?再此之前已经有了一篇了,那是刚知道kotlin的时候草(chao)来(chao)的并且学习一篇, 这次是自己在项目中正式使用并且遇到的一些问题记录,供kotlin新入门的童鞋参考,避 ...

  4. squid 代理服务

    squid代理服务分为两种方式: 一.正向代理(用在企业的办公环境中,员工上网需要通过Squid代理来上网) 客户端发送请求到代理服务器,代理服务器去向真正的服务器请求结果,并将结果返回给客户端 二. ...

  5. JavaScript自定义事件,动态添加属性

    根据事件的不同,可用的自定义方法也不同. document.createEvent('Event'); 实现主要有4个步骤: 1.创建事件. 2.初始化事件(三个参数:事件名,是否起泡,是否取消默认触 ...

  6. python_day2学习笔记

    基本数据类型 1.数字 int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2147483647 在64位系统上,整数的位数为64位, ...

  7. linux命令(7):ipcs/ipcrm命令

    ipcs作用 :查看消息队列(ipcs –q).共享内存(ipcs –m).信号灯(ipcs -s) ipcrm作用 :删除消息队列.共享内存.信号灯 ipcrm使用方式: ipcrm [ -M ke ...

  8. 当你用element-ui遇到需要在el-table-column上v-for时,这篇文章你能用的上,也就是你需要二级循环

    好链接就要丢过去 https://blog.csdn.net/qq_28929589/article/details/79445354

  9. python math模块

    import math math. ceil:取大于等于x的最小的整数值,如果x是一个整数,则返回x copysign:把y的正负号加到x前面,可以使用0 cos:求x的余弦,x必须是弧度 degre ...

  10. redis之(十一)redis实现缓存的功能

    [一]redis实现缓存的原理 --->利用键的失效时间设置实现缓存技术 --->由于redis的内存有限,可以在redis的配置文件里设置maxmemory的参数.来限制redis最大可 ...