OpenSessionInViewFilter是Spring提供的一个针对Hibernate的一个支持类,其主要意思是在发起一个页面请求时打开Hibernate的Session,一直保持这个Session,直到这个请求结束,具体是通过一个Filter来实现的。
由于Hibernate引入了Lazy Load特性,使得脱离Hibernate的Session周期的对象如果再想通过getter方法取到其关联对象的值,Hibernate会抛出一个LazyLoad的Exception。所以为了解决这个问题,Spring引入了这个Filter,使得Hibernate的Session的生命周期变长。
<!--EndFragment-->
在做ssh项目时——遇到懒加载问题一般都采用2中方式:OpenSessionInViewFilter和
<filter>
<filter-name>
openSessionInView
</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>
openSessionInView
</filter-name>
<url-pattern>
/*
</url-pattern>
</filter-mapping>
<bean name="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate3.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">
<props>
<prop key="login.action">userAcitonBean</prop>
</props>
</property>
</bean>
-->
<frameset rows="80,*">
<frame name="upper_left" src="main2.jsp">
<frame name="lower_left" >
在做项目时刚开始配置了OpenSessionInViewFilter,普通的页面是没有问题的。当用frameset把页面引用的时候出现了问题。出现了懒加载的问题。一开始我以为是OpenSessionInViewFilter失效了。然后又配置了openSessionInViewInterceptor结果还是不行。搞了很久,奇怪了。当没有用frameset的时候都还是可以的(没有用frameset的时候是直接访问main2.jsp的),为什么用了frameset的时候就不行了。考虑这考虑那。最后面在frameset页面写了一个${user.roles}取出所有用户的角色信息。竟然可以了。如果不在frameset页面写${user.roles}就会出现懒加载问题。有点奇怪。
我想问题出现应该是这样的。
1:因为页面过大,当加载frameset的时候,没有及时的加载引用的页面。然后就把session给关了。然后引用main2.jsp的时候session已经关了。所以会出现懒加载问题。
2:如果在frameset页面写${user.roles}。是因为在session没有关闭的情况下加载了内容。所以没有报懒加载的错误。
OpenSessionInView 会遇到的问题:
. 由于使用了OpenSessionInView模式后造成了内存和数据库连接问题
这个问题是我在生产环境中碰到的一个问题。由于使用了OpenSessionInView模式,Session的生命周期变得非常长。虽然解决了Lazy Load的问题,但是带来的问题就是Hibernate的一级缓存,也就是Session级别的缓存的生命周期会变得非常长,那么如果你在你的Service层做大批量的数据操作时,其实这些数据会在缓存中保留一份,这是非常耗费内存的。还有一个数据库连接的问题,存在的原因在于由于数据库的Connection是和Session绑在一起的,所以,Connection也会得不到及时的释放。因而当系统出现业务非常繁忙,而计算量又非常大的时候,往往数据连接池的连接数会不够。这个问题我至今非常头痛,因为有很多客户对数据连接池的数量会有限制,不会给你无限制的增加下去。
4. 使用了OpenSessionInView模式以后取数据的事务问题
在使用了OpenSessionInView以后,其实事务的生命周期比Session的生命周期来得短,就以为着,其实有相当一部分的查询是不被纳入到事务的范围内的,此时是否会读到脏数据?这个问题我至今不敢确认,有经验的朋友请指教一下。
最后提一下OpenSessionInView模式的一些替代方案,可以使用OpenSessionInViewInterceptor来代替这个Filter,此时可以使用Spring的AOP配置,将这个Interceptor配置到你所需要的层次上去。另外就是只能使用最古老的Hibernate.initialize()方法进行初始化了。
<!--EndFragment-->
分享到:
相关推荐
介绍Spring+hibernate opensessioninview问题
为了练手培训,给大家准备的 Open...3.通过 open session in view filter 支持 延迟加载 4.在页面上通过 jstl 很优雅的获取数据 5.通过 spring aop(aspectJ) 声明事务 6.通过formular 映射参数表,指定两个死的变量
OpenSessionInView项目整合jar包
Spring提供的CharacterEncoding和OpenSessionInView功能
在没有使用Spring提供的Open Session In View情况下,因需要在service(or Dao)层里把session关闭,所以lazy loading 为true的话,要在应用层内把关系集合都初始化,如 company.getEmployees(),否则Hibernate抛...
SSH项目整合示例【OpenSessionInView】所用到的jar包 包含Struts + Hibernate + Spring所有jar及其依赖的jar
NULL 博文链接:https://8366.iteye.com/blog/472141
NULL 博文链接:https://likenice.iteye.com/blog/290162
和Spring中OpenSessionInView由于org.springframework.web.struts.ContextLoaderPlugIn中保存同一个对象的名不同导致openSessionInView失效 稍微修改后在struts-config.xml中使用MyContextLoaderPlugIn.jar包中...
NULL 博文链接:https://yanzhenwei.iteye.com/blog/1701164
SSH项目整合示例【OpenSessionInView】源码
java-ee-7-sample Java EE 7 - 带有 WildFly 的示例应用程序 去做 一般任务 添加 gulp 来构建项目 使用 AOC 拦截器而不是过滤器进行 OpenSessionInView 处理 ...懒加载解决方案 递归和树结构 Maven
最新版SSH项目整合 实现了OpenSessionInView。 hibernate5.2.3+struts2.5.2+spring4.3.3
3,在web.xml中配置 spring 的 OpenSessionInView 过滤器(解决抛LazyInitializationException的问题) 1,配置 <!-- 配置 spring 的 OpenSessionInView 过滤器 --> <filter-name>OpenSessionInView ...
spring2.5学习PPT 传智博客 01_全面阐释Spring及其各项功能 ... 30.Spring提供的CharacterEncoding和OpenSessionInView功能 31.使用Spring集成JPA 32.Struts+Spring+JPA集成 33.使用Spring集成Struts2 34.所有源码
..............................................................................................................9 1.3.7Spring 框架加载配置文件:..............................................................