日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

OpenSessionInViewFilter作用及配置

發(fā)布時間:2024/4/17 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 OpenSessionInViewFilter作用及配置 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

http://smesoft.blog.51cto.com/2740835/878389

一、作用

Spring為我們解決Hibernate的Session的關閉與開啟問題。? Hibernate 允許對關聯(lián)對象、屬性進行延遲加載,但是必須保證延遲加載的操作限于同一個 Hibernate Session 范圍之內進行。如果 Service 層返回一個啟用了延遲加載功能的領域對象給 Web 層,當 Web 層訪問到那些需要延遲加載的數(shù)據(jù)時,由于加載領域對象的 Hibernate Session 已經(jīng)關閉,這些導致延遲加載數(shù)據(jù)的訪問異常 (eg: org.hibernate.LazyInitializationException:(LazyInitializationException.java:42)? - failed to lazily initialize a collection of role: cn.easyjava.bean.product.ProductType.childtypes, no session or session was closed? org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: cn.easyjava.bean.product.ProductType.childtypes, no session or session was closed) 用來把一個Hibernate Session和一次完整的請求過程對應的線程相綁定。目的是為了實現(xiàn)"Open Session in View"的模式。例如: 它允許在事務提交之后延遲加載顯示所需要的對象。 而Spring為我們提供的OpenSessionInViewFilter過濾器為我們很好的解決了這個問題。OpenSessionInViewFilter的主要功能是用來把一個Hibernate Session和一次完整的請求過程對應的線程相綁定。目的是為了實現(xiàn)"Open Session in View"的模式。例如: 它允許在事務提交之后延遲加載顯示所需要的對象。? OpenSessionInViewFilter 過濾器將 Hibernate Session 綁定到請求線程中,它將自動被 Spring 的事務管理器探測到。所以 OpenSessionInViewFilter 適用于 Service 層使用HibernateTransactionManager 或 JtaTransactionManager 進行事務管理的環(huán)境,也可以用于非事務只讀的數(shù)據(jù)操作中。 二、配置 它有兩種配置方式OpenSessionInViewInterceptor和OpenSessionInViewFilter(具體參看SpringSide),功能相同,只是一個在web.xml配置,另一個在application.xml配置而已。 Open Session In View在request把session綁定到當前thread期間一直保持hibernate session在open狀態(tài),使session在request的整個期間都可以使用,如在View層里PO也可以lazy loading數(shù)據(jù),如 ${ 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根據(jù)該 session是否已和transaction綁定來決定是否關閉session。在這個過程中,若HibernateTemplate 發(fā)現(xiàn)自當前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看起來還不錯,其實副作用不少。看回上面OpenSessionInViewFilter的doFilterInternal方法代碼,這個方法實際上是被父類的doFilter調用的,因此,我們可以大約了解的OpenSessionInViewFilter調用流程: request(請求)->open session并開始transaction->controller->View(Jsp)->結束transaction并 close session. 一切看起來很正確,尤其是在本地開發(fā)測試的時候沒出現(xiàn)問題,但試想下如果流程中的某一步被阻塞的話,那在這期間connection就一直被占用而不釋放。最有可能被阻塞的就是在寫Jsp這步,一方面可能是頁面內容大,response.write的時間長,另一方面可能是網(wǎng)速慢,服務器與用戶間傳輸時間久。當大量這樣的情況出現(xiàn)時,就有連接池連接不足,造成頁面假死現(xiàn)象。 Open Session In View是個雙刃劍,放在公網(wǎng)上內容多流量大的網(wǎng)站請慎用 -------------------------------------------------------------------------- 假設在你的應用中Hibernate是通過spring 來管理它的session.如果在你的應用中沒有使用OpenSessionInViewFilter或者OpenSessionInViewInterceptor。session會在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。 所以給你的感覺是調用這個名為“manager”的bean的方法之后session就關掉了。 如果應用中使用了OpenSessionInViewFilter或者OpenSessionInViewInterceptor,所有打開的session會被保存在一個線程變量里。在線程退出前通過 OpenSessionInViewFilter或者OpenSessionInViewInterceptor斷開這些session。 為什么這么做?這主要是為了實現(xiàn)Hibernate的延遲加載功能。基于一個請求 一個hibernate session的原則。 spring中對OpenSessionInViewFilter的描述如下: 它是一個Servlet2.3過濾器,用來把一個Hibernate Session和一次完整的請求過程對應的線程相綁定。目的是為了實現(xiàn)"Open Session in View"的模式。 例如: 它允許在事務提交之后延遲加載顯示所需要的對象。 這個過濾器和 HibernateInterceptor 有點類似:它是通過線程實現(xiàn)的。無論是沒有事務的應用,還是有業(yè)務層事務的應用(通過HibernateTransactionManager 或 JtaTransactionManager的方式實現(xiàn))它都適用。在后一種情況下,事務會自動采用由這個filter綁定的Session來進行相關的操作以及根據(jù)實際情況完成提交操作。 警告: 如果在你的應用中,一次請求的過程中使用了單一的一個HIbernate Session,在這種情況下,采用這個filter會產(chǎn)生一些以前沒遇到的問題。特別需要注意的是通過 Hibernate Session重新組織持久化對象之間關系的相關操作需要在請求的最開始進行。以免與已經(jīng)加載的相同對象發(fā)生沖突。 或者,我們可以通過指定"singleSession"="false"的方式把這個過濾器調到延期關閉模式。這樣在一次請求的過程中不會使用一個單一的Session.每一次數(shù)據(jù)訪問或事務相關 操作都使用屬于它自己的session(有點像不使用Open Session in View).這些session都被注冊成延遲關閉模式,即使是在這一次的請求中它相關操作已經(jīng)完成。 "一次請求一個session" 對于一級緩存而言很有效,但是這樣可以帶來副作用。例如在saveOrUpdate的時候或事物回滾之后,雖然它和“no Open Session in View”同樣安全。 但是它卻允許延遲加載。 它會在spring的web應用的上下文根中查找Session工廠。它也支持通過在web.xml中定義的“SessionFactoryBeanName”的init-param元素 指定的Session工廠對應的bean的 名字來查找session工廠。默認的bean的名字是"sessionFactory".他通過每一次請求查找一次SessionFactory的方式來避免由初始化順序引起的問題(當使用ContextLoaderServlet 來集成spring的時候 ,spring 的應用上下文是在這個filter 之后才被初始化的)。 默認的情況下,這個filter 不會同步Hibernate Session.這是因為它認為這項工作是通過業(yè)務層的事務來完成的。而且HibernateAccessors 的FlushMode為FLUSH_EAGER.如果你 想讓這個filter在請求完成以后同步session.你需要覆蓋它的closeSession方法,在這個方法中在調用父類的關閉session操作之前同步session.此外你需要覆蓋它的getSession() 方法。返回一個session它的FlushMode 不是默認的FlushMode.NEVER。需要注意的是getSession()和closeSession()方法只有在single session的模式中才被調用。 在myfaces的wiki里提供了OpenSessionInViewFilter的一個子類如下: public class OpenSessionInViewFilter extends org.springframework.orm.hibernate3.support.OpenSessionInViewFilter { /** * we do a different flushmode than in the codebase * here */ protected Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException { Session session = SessionFactoryUtils.getSession(sessionFactory, true); session.setFlushMode(FlushMode.COMMIT); return session; } /** * we do an explicit flush here just in case * we do not have an automated flush */ protected void closeSession(Session session, SessionFactory factory) { session.flush(); super.closeSession(session, factory); } }

?

總結

以上是生活随笔為你收集整理的OpenSessionInViewFilter作用及配置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。