OpenSessionInViewFilter与OpenSessionInViewInterceptor

原文地址:http://www.cnblogs.com/90zyh/archive/2013/05/30/3109253.html

在没有使用Spring提供的Open Session In View情况下,事务提交时会在service(or Dao)层里把session关闭,在lazy loading 为true的话,这样就只能使用get()方法,若使用load()方法,Hibernate抛session already closed Exception;   

    Open Session In View提供了一种简便的方法,较好地解决了lazy loading问题.  

    说明一下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,功能相同,只是一个在web.xml配置,另一个在spring的xml文件中配置而已。OpenSessionInViewFilter多用于非spring MVC框架,OpenSessionInViewInterceptor多用于Spring MVC框架。

   Open Session In View在request把session绑定到当前thread期间一直保持hibernate session在open状态,使session在request的整个期间都可以使用,如在View层里PO也可以lazy loading数据,如 ${ company.employees }。当View 层逻辑完成后,才会通过Filter的doFilter方法或Interceptor的postHandle方法自动关闭session。

   

对于OpenSessionInViewFilter,需要在web.xml文件中配置。但一定要放在Struts   filter的前面

OpenSessionInViewFilter调用流程: 

request(请求)->open session并开始transaction->controller->View(Jsp)->结束transaction并 close session.

配置:

复制代码
<filter>  
  <description>处理Hibernate的懒加载问题description>  
  <filter-name>OpenSessionInViewFilter<filter-name>  
  <filter-class>  
   org.springframework.orm.hibernate4.support.OpenSessionInViewFilter   
 </filter-class>  
  <init-param>  
    <description>  
       默认情况下,这个Filter会在Spring的bean池中找一个叫做sessionFactory的bean。如果使用了其他名字的SessionFactory,则应该在这里   
       指定这个名字。   
    </description>  
    <param-name>sessionFactoryBeanName</param-name>  
    <param-value>指定的名字</param-value>  
  </init-param>  
  <init-param>  
    <description></description>  
    <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>  
复制代码

其中的sessionFactoryBeanName是你的SessionFactory的实例的名字。这个名字是你在Spring的Bean定义文件中声明的SessionFactory的实例的bean名字。默认这个Filter会使用“sessionFactory”这个值,如果你的SessionFactory确实是这个名字的话,可以不用指明它的名字。但是如果你向我一样,给SessionFactory使用了其他名字,就得在这里声明了。

OpenSessionInViewInterceptor,就纯粹是Spring IOC容器管理的东西了。

    首先,你需要在Spring配置文件中声明一个OpenSessionInViewInterceptor的实例

复制代码
<beans>    
<bean name="openSessionInViewInterceptor" class="org.springframework.orm.hibernate4.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">    
...    
</property>    
</bean> ... </beans>   
复制代码

 

但它也不是万能的,由于需要连接时间比较长,对于数据量大、访问量大的站点一定要慎用!!!

 **************************************************************************************************************

P.S Spring3.1集成Hibernate4不再需要HibernateDaoSupport和HibernateTemplate了,直接使用原生API即可。如果BasicDao 继承HibernateDaoSupport,使用是可能报

org.springframework.dao.InvalidDataAccessApiUsageException: 
Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.


另一篇参考文章:http://xuliangyong.iteye.com/blog/144818

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值