[轉]Spring配置之OpenSessionInViewFilter

來源:互聯網
上載者:User

標籤:

參考:
OpenSessionInViewFilter作用及配置:http://www.yybean.com/opensessioninviewfilter-role-and-configuration
http://blog.csdn.net/fooe84/article/details/680449

主要涉及類:
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor

一、作用
    說明一下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(具體參看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。
1,web.xml配置OpenSessionInViewFilter
Xml代碼  
  1. <!--Hibernate Open Session in View Filte-->  
  2. <filter>  
  3.     <filter-name>hibernateFilter</filter-name>  
  4.     <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>  
  5.     <!-- singleSession預設為true,若設為false則等於沒用OpenSessionInView -->  
  6.     <init-param>  
  7.         <param-name>singleSession</param-name>  
  8.         <param-value>true</param-value>  
  9.     </init-param>  
  10. </filter>  
  11. <filter-mapping>  
  12.     <filter-name>hibernateFilter</filter-name>  
  13.     <url-pattern>/*</url-pattern>  
  14. </filter-mapping>   
<!--Hibernate Open Session in View Filte--><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>/*</url-pattern></filter-mapping>

2,applicationContext.xml配置openSessionInViewInterceptor
Xml代碼  
  1. <bean id="openSessionInViewInterceptor"  
  2.     class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">  
  3.     <property name="sessionFactory" ref="sessionFactory" />  
  4. </bean>  
<bean id="openSessionInViewInterceptor"class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor"><property name="sessionFactory" ref="sessionFactory" /></bean>


三、注意
盡 管Open Session In View看起來還不錯,其實副作用不少。看回上面OpenSessionInViewFilter的doFilterInternal方法代碼,這個方法實際上是被父類的doFilter調用的,因此,我們可以大約瞭解的OpenSessionInViewFilter調用流程:

request(請求)->open session並開始transaction->controller->View(Jsp)->結束transaction並 close session.

一切看起來很正確,尤其是在本地開發測試的時候沒出現問題,但試想下如果流程中的某一步被阻塞的話,那在這期間connection就一直被佔用而不釋放。最有可能被阻塞的就是在寫Jsp這步,一方面可能是頁面內容大,response.write的時間長,另一方面可能是網速慢,伺服器與使用者間傳輸時間久。當大量這樣的情況出現時,就有串連池串連不足,造成頁面假死現象。

Open Session In View是個雙刃劍,放在公網上內容多流量大的網站請慎用

[轉]Spring配置之OpenSessionInViewFilter

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.