OpenSessionInViewFilter简介和学习

假设在你的应用中Hibernate是通过spring 来管理它的session.如果在你的应用中没有使用OpenSessionInViewFilter或者OpenSessionInViewInterceptorsession会在transaction结束后关闭。
如果你采用了spring的声明式事务模式,它会对你的被代理对象的每一个方法进行事务包装(AOP的方式)。如下:


 

<bean id="txProxyTemplate" abstract="true"
 class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
 <property name="transactionManager" ref="transactionManager"/>
 <property name="transactionAttributes">
 <props>
 <prop key="save*">PROPAGATION_REQUIRED</prop>
 <prop key="remove*">PROPAGATION_REQUIRED</prop>
 <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
 </props>
 </property>
 </bean>

<bean id="manager" parent="txProxyTemplate">
 <property name="target">
 <bean class="org.appfuse.service.impl.BaseManager">
 <property name="dao" ref="dao"/>
 </bean>
 </property>
 </bean>


目标类org.appfuse.service.impl.BaseManager 的 save *方法的事务类型PROPAGATION_REQUIRED remove* 方法的事务类型PROPAGATION_REQUIRED
其他的方法的事务类型是PROPAGATION_REQUIRED,readOnly
所以给你的感觉是调用这个名为managerbean的方法之后session就关掉了。
如果应用中使用了OpenSessionInViewFilter或者OpenSessionInViewInterceptor,所有打开的session会被保存在一个线程变量里。在线程退出前通过
OpenSessionInViewFilter或者OpenSessionInViewInterceptor断开这些session。 为什么这么做?这主要是为了实现Hibernate的延迟加载功能。基于一个请求
一个hibernate session的原则。

web.xml中配置如下:

<!-- Open Session View -->

<filter>

<filter-name>openSessionInViewFilter</filter-name>

<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>

</filter>


<filter-mapping>

<filter-name>openSessionInViewFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值