spring中延长hibernate中session的生命周期:OpenSessionInViewFilter

在HIBERNATE中,如果我们要访问到某个类的集合属性,那我们一般在类的的映射文件里SET节点启用LAZE=FALSE;
那么在S2SH中怎么解决呢?

1.OpenSessionInViewFilter是Spring提供的一个针对Hibernate的一个支持类,其主要意思是在发起一个页面请求时打开Hibernate的Session,
一直保持这个Session,直到这个请求结束,具体是通过一个Filter来实现的。

2.由于Hibernate引入了Lazy Load特性,使得脱离Hibernate的Session周期的对象如果再想通过getter方法取到其关联对象的值,
Hibernate会抛出一个LazyLoad的Exception。所以为了解决这个问题,Spring引入了这个Filter,使得Hibernate的Session的生命周期变长。

解决方法:
修改WEB.XML !!!
在WEB.XML中加入OpenSessionInView 的FILETER就可以了
具体如下:

  <!-- 解决延迟加载时,session关闭后,jsp访问不到实体数据 (注意:此filter要在Struts2的filter前面)--> 
 <filter> 
	<filter-name>OpenSessionInViewFilter</filter-name> 
	<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> 
	<init-param>
	  <!-- 默认是NEVER,只支持查询,改成AUTO,支持增删改--> 
	  <param-name>flushMode</param-name>  
	  <param-value>AUTO</param-value>  
    </init-param>
	<init-param>  
	<!-- 此项为false时,OpenSessionInViewFilter不起作用,默认是true,不配也行-->     
	  <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>

 

注意事项:

1.在加入这个FILETER的时候,一定一定,千万千万记得放在STRUTS2的核心控制器的位置的上面!切记!
2.如果你的SPRING的配置文件中SESSIONFACTORY的BEAN ID默认不是sessionFactory的话,你的openSessionInView要改成这样.

<filter>
    <filter-name>openSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    <init-param>
        <param-name>sessionFactoryBeanName</param-name>
        <param-value>xxxx</param-value> //也就是你的SPRING配置文件中SESSIONFACTORY的名字
    </init-param>
</filter>
<filter-mapping>
    <filter-name>openSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

3.当你把OPENSESSIONINVIEW配置上后,你就可以让SESSION晚点关闭的.但是又一个新的问题产生了.什么问题呢?自己可以试试,如果这个时候你的SPRING
没有配置事务的话,那么你所有的更新操作都是不允许的.
所以要在SPRING里把需要更新的SERVICE操作都配置事务.
配置如下:
首先在SPRING配置文件中加入XML命名空间:
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
方案地址:
xsi:schemaLocation里加上
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
前提是你配置了AOP的命名空间和方案地址

<!-- 事务配置 -->

<!--Step1.事务管理器配置-->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <!--Step2.交给SPRING事务管理的一些方法.-->
<aop:config>
<aop:pointcut id="productServiceMethods"
            <!-- 指定切面是哪个范围类,比如你项目的SERVICE的所有方法-->
expression="execution(* com.公司名.项目名.service..*.*(..))" />
<!-- 一个通知的集合,这个集合都用上的POINTCUT-->
<aop:advisor advice-ref="txAdvice"
pointcut-ref="productServiceMethods" />
</aop:config>
<!--定义通知集合,以TX开头,是有事务的 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!--所有的以ADD开头的方法名的方法都用事务  REQUIRED 表示,比如方法A有更新操作,A中调用了B方法,那么B到底是重新起一个事务还是用A方法的事务,
REQUIRED标识不起就用当前事务,如果有就用A的,如果没有就起一个-->
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<!--加入只读,说明以GET或者LOAD开头的方法名的方法都是只读事务,说明这个方法内没有UPDATE操作,这样声明下可以提高该查询方法的效率 -->
<tx:method name="get*" propagation="REQUIRED"
read-only="true" />
<tx:method name="load*" propagation="REQUIRED"
read-only="true" />
</tx:attributes>
</tx:advice>


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值