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
2,applicationContext.xml配置openSessionInViewInterceptor
三、注意
尽 管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的更多相关文章
- [转]Spring配置之OpenSessionInViewFilter
参考: OpenSessionInViewFilter作用及配置:http://www.yybean.com/opensessioninviewfilter-role-and-configuratio ...
- Spring配置c3p0数据源时出错报:java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector
今天在使用Spring配置c3p0数据源时,使用的数据库是mysql,服务器是tomcat,运行时报了一个 java.lang.NoClassDefFoundError: com/mchange/v2 ...
- Spring配置汇总
现在主流的JavaWeb应用几乎都会用到Spring,以下是Spring的配置,以及结合Web的SpringMVC配置的汇总. jar包的引入 与Web项目集成 Spring配置文件 SpringMV ...
- spring配置属性的两种方式
spring配置属性有两种方式,第一种方式通过context命名空间中的property-placeholder标签 <context:property-placeholder location ...
- 解决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 ...
- spring配置详解
1.前言 公司老项目的后台,均是基于spring框架搭建,其中还用到了log4j.jar等开源架包.在新项目中,则是spring和hibernate框架均有使用,利用了hibernate框架,来实现持 ...
- memcached 学习 1—— memcached+spring配置
memcached 学习目录: memcached 学习 1—— memcached+spring配置 这几天自己搭建项目环境,解决问题如下: 有关常见的配置这里没有列出,中间遇到的搭建问题比较顺利g ...
- 解决spring配置中的bean类型的问题:BeanNotOfRequiredTypeException
解决spring配置中的bean类型的问题:BeanNotOfRequiredTypeException这个问题出现的原因:一般在使用annotation的方式注入spring的bean 出现的,具体 ...
- spring配置中,properties文件以及xml文件配置问题
spring方便我们的项目快速搭建,功能强大,自然也会是体系复杂! 这里说下配置文件properties管理的问题. 一些不涉及到代码逻辑,仅仅只是配置数据,可以放在xxxx.properties文件 ...
随机推荐
- Django models 常用字段类型
1.CharField字符串字段,存较短的字符串,长文本要用TextField.必须的参数:max_length 字符的最大长度2.TextField容量很大的文本字段.admin中用 <tex ...
- 关于Membership和身份认证的记录
在今天写好的code中测试环节,当我用webconfig中的测试数据库就是ok的,但是更替为正式的就不行了: 报错的类是MemberShip,那就关系到身份认证的环节了 找了几个链接,记录下 1.身份 ...
- C#程序集版本控制文件属性祥解
using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices ...
- 虚拟DOM介绍
[转自]:https://www.jianshu.com/p/616999666920 为什么需要虚拟DOM 先介绍浏览器加载一个HTML文件需要做哪些事,帮助我们理解为什么我们需要虚拟DOM.web ...
- turn.js中文API 写一个翻页效果的参数详细解释
$('.flipbook').turn({ width: 922, height: 600, elevation: 50, gradients: true, a ...
- TCP协议的三次握手、四次挥手
TCP三次握手 TCP的连接的建立需要发送三个包,一次称为三次握手(Three-way Handshake). 三次握手的目的是连接服务器指定端口,建立TCP连接,并同步连接双方的序列号和确认号并交换 ...
- BZOJ 3510: 首都 LCT + multiset维护子树信息 + 树的重心
Code: #include<bits/stdc++.h> #define maxn 200000 #define inf 1000000000 using namespace std; ...
- JavaScript for循环元素取下标问题
<ul> <li>fg</li> <li>gd</li> <li>gds</li> <li>ghe< ...
- 亚马逊免费服务器搭建Discuz!论坛过程(二)
1: 登录服务器 在实例页面,点击连接连接,按照如下步骤,即可登录服务器. 登录成功之后,如下所示.你就可以随意玩耍了. 2: 系统优化 证书登录当然安全,但是不太方便并且麻烦,本人还是习惯用户名密 ...
- python爬虫05 | 年轻人,不会正则表达式你睡得着觉?有点出息没有?
现在 你已经会使用 python 模拟浏览器 进行一些 Http 的请求了 那么请求完之后 服务器返回给我们一堆源代码 我们可不是啥都要的啊 我们是有原则的 我们想要的东西 怎么能一股脑的啥都往自己兜 ...