zoukankan      html  css  js  c++  java
  • OpenSessionInViewFilter 的配置及作用

    Hibernate允许对关联的对象,属性进行延迟加载,但是这个前提是必须保证延迟加载在同一个Hibernate Session范围之内进行。在实际开发中,都是采用分层的模式进行开发的,如果要在action层中使用一个由Service层返回的业务对象,延迟加载关联对象事,因加载领域对象的 Hibernate Session 已经关闭,这些导致延迟加载数据的访问异常

    failed to lazily initialize a collection of role: com.po.Person.schools, no session or session was closed 
    ..........
    提示:延迟加载role集合失败,没有Hibernate Session或Session关闭

    OpenSessionInViewFilter作用就是把一个Hibernate Session和一次完整的请求过程对应的线程相绑定。目的是为了实现"Open Session in View"的模式。例如: 它允许在事务提交之后延迟加载显示所需要的对象

    如下是其官方文献:

    Servlet 2.3 Filter that binds a Hibernate Session to the thread for 
    the entire processing of the request. Intended for the "Open Session in View" pattern, 
    i.e. to allow for lazy loading in web views despite the original transactions 
    already being completed. 
    
    This filter makes Hibernate Sessions available via the current thread, 
    which will be autodetected by transaction managers. 
    It is suitable for service layer transactions via HibernateTransactionManager 
    or JtaTransactionManager as well as for non-transactional execution 
    (if configured appropriately). 
    

    OpenSessionInViewFilter在web.xml中配置如下:

       <filter>
            <filter-name>openSessionInView</filter-name>
            <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
            <init-param>
                <param-name>flushMode</param-name>
                <param-value>AUTO</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>openSessionInView</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
      <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
            <dispatcher>REQUEST</dispatcher>
            <dispatcher>FORWARD</dispatcher>
        </filter-mapping>

    注意事项:

    1.

    NOTE: This filter will by default not flush the Hibernate Session, 
    with the flush mode set to FlushMode.NEVER. It assumes to be used in combination 
    with service layer transactions that care for the flushing: 
    The active transaction manager will temporarily change the flush mode 
    to FlushMode.AUTO during a read-write transaction, 
    with the flush mode reset to FlushMode.NEVER at the end of each transaction. 
    If you intend to use this filter without transactions, 
    consider changing the default flush mode (through the "flushMode" property).

    该文献说OpenSessionInViewFilter默认采用FlushMode.NEVER刷新模式,该模式默认是不刷新Hibernate Session;在service层进行读写事务中它自动转变为FlushMode.AUTO,在事务结束后重置为FlushMode.NEVER

    2.特别注意的是:该过滤器需要配置在struts2的过滤器之前

  • 相关阅读:
    三行Python代码查询IP
    剑指offer面试题29:数组中出现次数超过一半的数字
    【简】题解 AWSL090429 【数塔问题】
    Re.常系数齐次递推
    Re.多项式除法/取模
    【翻译】A simple stone game
    Re.多项式求逆
    Re.FFT
    题解 P4783 【【模板】矩阵求逆】
    关于win10企业版在极域电子教室软件 v4.0 2015 豪华版的全屏控制下如何取得自由
  • 原文地址:https://www.cnblogs.com/liaojie970/p/4885368.html
Copyright © 2011-2022 走看看