<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    沉睡森林@漂在北京

    本處文章除注明“轉載”外均為原創,轉載請注明出處。

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      152 隨筆 :: 4 文章 :: 114 評論 :: 0 Trackbacks

    看到了關于Spring提供的Open Session In View的一篇文章,很詳細的介紹了相關細節。下面的文章轉載自:
    http://blog.sina.com.cn/s/blog_5dc12c490100crr5.html


    使用Spring提供的Open Session In View而引起Write operations are not allowed in read-only mode (FlushMode.NEVER) 錯誤解決:
        在沒有使用Spring提供的Open Session In View情況下,因需要在service(or Dao)層里把session關閉,所以lazy loading 為true的話,要在應用層內把關系集合都初始化,如 company.getEmployees(),否則Hibernate拋session already closed Exception;    Open Session In View提供了一種簡便的方法,較好地解決了lazy loading問題.
        它有兩種配置方式OpenSessionInViewInterceptor和OpenSessionInViewFilter(具體參看SpringSide),功能相同,只是一個在web.xml配置,另一個在application.xml配置而已。
        Open Session In View在request把session綁定到當前thread期間一直保持hibernate session在open狀態,使session在request的整個期間都可以使用,如在View層里PO也可以lazy loading數據,如 ${ company.employees }。當View 層邏輯完成后,才會通過Filter的doFilter方法或Interceptor的postHandle方法自動關閉session。
     
    OpenSessionInViewInterceptor配置
    <beans>
    <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">
     ..
    </property>
    </bean>
     

     
    </beans>
     
    OpenSessionInViewFilter配置
    <web-app>
     

     
    <filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    </filter-class>
    <!-- singleSession默認為true,若設為false則等于沒用OpenSessionInView -->
    <init-param>
    <param-name>singleSession</param-name>
    <param-value>true</param-value>
    </init-param>
    </filter>
     

     
    <filter-mapping>
    <filter-name>hibernateFilter</filter-name>
    <url-pattern>*.do</url-pattern>
    </filter-mapping>

     
    </web-app>
     
    很多人在使用OpenSessionInView過程中提及一個錯誤:
    org.springframework.dao.InvalidDataAccessApiUsageException: Write operations
    are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into
    FlushMode.AUTO or remove 'readOnly' marker from transaction definition
    看看OpenSessionInViewFilter里的幾個方法
     
    protected void doFilterInternal(HttpServletRequest request,
    HttpServletResponse response,FilterChain filterChain)
    throws ServletException, IOException {
     SessionFactory sessionFactory = lookupSessionFactory();
     logger.debug("Opening Hibernate Session in OpenSessionInViewFilter");
     Session session = getSession(sessionFactory);
     TransactionSynchronizationManager.bindResource(
      sessionFactory, new SessionHolder(session));
     try {
      filterChain.doFilter(request, response);
     }
     finally {
     TransactionSynchronizationManager.unbindResource(sessionFactory);
     logger.debug("Closing Hibernate Session in OpenSessionInViewFilter");
     closeSession(session, sessionFactory);
     }
    }
     
     
    protected Session getSession(SessionFactory sessionFactory)
    throws DataAccessResourceFailureException {
     Session session = SessionFactoryUtils.getSession(sessionFactory, true);
     session.setFlushMode(FlushMode.NEVER);
     return session;
    }

    protected void closeSession(Session session, SessionFactory sessionFactory)
    throws CleanupFailureDataAccessException {
     SessionFactoryUtils.closeSessionIfNecessary(session, sessionFactory);
    }
     
         可以看到OpenSessionInViewFilter在getSession的時候,會把獲取回來的session的flush mode 設為FlushMode.NEVER。然后把該sessionFactory綁定到 TransactionSynchronizationManager,使request的整個過程都使用同一個session,在請求過后再接除該 sessionFactory的綁定,最后closeSessionIfNecessary根據該 session是否已和transaction綁定來決定是否關閉session。在這個過程中,若HibernateTemplate 發現自當前session有不是readOnly的transaction,就會獲取到FlushMode.AUTO Session,使方法擁有寫權限。
     
    public static void closeSessionIfNecessary(Session session, SessionFactory sessionFactory)
    throws CleanupFailureDataAccessException {
     
    if (session == null ||
    TransactionSynchronizationManager.hasResource(sessionFactory)) {
    return;
    }
    logger.debug("Closing Hibernate session");
    try {
    session.close();
    }
    catch (JDBCException ex) {
    // SQLException underneath
    throw new CleanupFailureDataAccessException("Could not close Hibernate session", ex.getSQLException());
    }catch (HibernateException ex) {
    throw new CleanupFailureDataAccessException("Could not close Hibernate session", ex);
    }
    }
     
        也即是,如果有不是readOnly的transaction就可以由Flush.NEVER轉為Flush.AUTO,擁有 insert,update,delete操作權限,如果沒有transaction,并且沒有另外人為地設flush model的話,則doFilter的整個過程都是Flush.NEVER。所以受transaction保護的方法有寫權限,沒受保護的則沒有。
    采用spring的事務聲明,使方法受transaction控制
     
     
    <bean id="baseTransaction"
    class
    ="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
    abstract
    ="true">
    <property name="transactionManager" ref="transactionManager"/>
    <property name="proxyTargetClass" value="true"/>
    <property name="transactionAttributes">
    <props>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="add*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="remove*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>
     
    <bean id="userService" parent="baseTransaction">
     
    <property name="target">
    <bean class="com.phopesoft.security.service.impl.UserServiceImpl"/>
    </property>
    </bean>
     
    對 于上例,則以save,add,update,remove開頭的方法擁有可寫的事務,如果當前有某個方法,如命名為importExcel(),則因沒 有transaction而沒有寫權限,這時若方法內有insert,update,delete操作的話,則需要手動設置flush model為Flush.AUTO,如
    session.setFlushMode(FlushMode.AUTO);
    session.save(user);
    session.flush();
          盡 管Open Session In View看起來還不錯,其實副作用不少??椿厣厦鍻penSessionInViewFilter的doFilterInternal方法代碼,這個方法 實際上是被父類的doFilter調用的,因此,我們可以大約了解的OpenSessionInViewFilter調用流程: request(請求)->open session并開始transaction->controller->View(Jsp)->結束transaction并 close session.
         一切看起來很正確,尤其是在本地開發測試的時候沒出現問題,但試想下如果流程中的某一步被阻塞的話,那在這期間connection就一直被占用而不釋 放。最有可能被阻塞的就是在寫Jsp這步,一方面可能是頁面內容大,response.write的時間長,另一方面可能是網速慢,服務器與用戶間傳輸時 間久。當大量這樣的情況出現時,就有連接池連接不足,造成頁面假死現象。
    Open Session In View是個雙刃劍,放在公網上內容多流量大的網站請慎用。

    posted on 2009-05-21 20:45 王總兵 閱讀(1886) 評論(2)  編輯  收藏 所屬分類: Spring

    評論

    # re: [轉]Hibernate和spring結合的OpenSessionInViewFilter 2009-10-26 21:32 pei
    調用流程: request(請求)->open session并開始transaction->controller->View(Jsp)->結束transaction并 close session.

    好像說的不對。
    我在spring專業編程這本書上看到,是說事務在服務層中結束  回復  更多評論
      

    # re: [轉]Hibernate和spring結合的OpenSessionInViewFilter 2009-10-27 09:08 wangchangbing
    @pei

    你沒有好好的看這個文章,對OpenSessionInViewFilter的認識還不是很到位
    事物在服務層結束是正常的做法,但是我們使用了延遲加載技術
    事物在展示層做完后在關閉  回復  更多評論
      

    主站蜘蛛池模板: 亚洲最大福利视频网站| 亚洲精品国偷自产在线| 亚洲天然素人无码专区| 日本免费大黄在线观看| 久久精品亚洲中文字幕无码网站| 一级特黄aaa大片免费看| 亚洲精品国产精品乱码不卡| 特级毛片免费播放| 老司机亚洲精品影视www| 中国一级特黄高清免费的大片中国一级黄色片 | 亚洲国产情侣一区二区三区| 69国产精品视频免费| 亚洲jjzzjjzz在线播放| 最近中文字幕mv免费高清电影| 亚洲乱理伦片在线观看中字| 国产免费小视频在线观看| 免费又黄又爽又猛大片午夜 | 久久www免费人成看片| 亚洲一线产区二线产区精华| 成人片黄网站色大片免费| 麻豆亚洲AV成人无码久久精品| 亚洲国产婷婷综合在线精品| 久久青草免费91观看| 学生妹亚洲一区二区| 亚洲国产成人久久一区WWW| 成在人线av无码免费高潮喷水| 噜噜噜亚洲色成人网站∨| 99久久这里只精品国产免费| 无码一区二区三区亚洲人妻| 亚洲欧洲日产国码一级毛片| 久久久久久久99精品免费| 亚洲色图激情文学| 久久精品亚洲乱码伦伦中文| 精品国产无限资源免费观看| 精品久久久久久亚洲综合网| 亚洲AV无码乱码在线观看富二代| 国产乱子精品免费视观看片| 免费的黄网站男人的天堂| 日韩亚洲Av人人夜夜澡人人爽| 国产无遮挡色视频免费视频| 久久精品无码专区免费|