spring 的OpenSessionInViewFilter簡介,opensessioninview

來源:互聯網
上載者:User

spring 的OpenSessionInViewFilter簡介,opensessioninview

假設在你的應用中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。 為什麼這麼做?這主要是為了實現Hibernate的消極式載入功能。基於一個請求
一個hibernate session的原則。

spring中對OpenSessionInViewFilter的描述如下:
它是一個Servlet2.3過濾器,用來把一個Hibernate Session和一次完整的請求過程對應的線程相綁定。目的是為了實現"Open Session in View"的模式。
例如: 它允許在事務提交之後消極式載入顯示所需要的對象。

這個過濾器和 HibernateInterceptor 有點類似:它是通過線程實現的。無論是沒有事務的應用,還是有業務層事務的應用(通過HibernateTransactionManager 或
JtaTransactionManager的方式實現)它都適用。在後一種情況下,事務會自動採用由這個filter綁定的Session來進行相關的操作以及根據實際情況完成提交操作。

警告: 如果在你的應用中,一次請求的過程中使用了單一的一個HIbernate Session,在這種情況下,採用這個filter會產生一些以前沒遇到的問題。特別需要注意的是通過
Hibernate Session重新組織持久化對象之間關係的相關操作需要在請求的最開始進行。以免與已經載入的相同對象發生衝突。

或者,我們可以通過指定"singleSession"="false"的方式把這個過濾器調到延期關閉模式。這樣在一次請求的過程中不會使用一個單一的Session.每一次資料訪問或事務相關
操作都使用屬於它自己的session(有點像不使用Open Session in View).這些session都被註冊成延遲關閉模式,即使是在這一次的請求中它相關操作已經完成。

"一次請求一個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.這是因為它認為這項工作是通過業務層的事務來完成的。而且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);
        }
}


java架構spring中的opensessioninview有什作用

在hibernate中使用load方法時,並未把資料真正擷取時就關閉了session,當我們真正想擷取資料時會迫使load載入資料,而此時 session已關閉,所以就會出現異常。 比較典型的是在MVC模式中,我們在M層調用持久層擷取資料時(持久層用的是load方法載入資料),當這一調用結束時,session隨之關閉,而我們 希望在V層使用這些資料,這時才會迫使load載入資料,我們就希望這時的session是open著得,這就是所謂的Open Session In view 。 我們可以filter來達到此目的, 也可以用interceptor來做。
filter:
?
<!-- Spring的OpenSessionInView實現 此監聽器應該在struts2的監聽器前面-->
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

interceptor:
?
importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;

publicclassXxxDAOextendsHibernateDaoSupport {

publicvoidsave(Xxx transientInstance) {
try{
getHibernateTemplate().save(transientInstance);
}catch(RuntimeException re) {
throwre;
}
}
}

OpenSessionInViewFilter的主要功能是用來把一個Hibernate Session和一次完整的請求過程對應的線程相綁定。Open Session In View在request把session綁定到當前thread期間一直保持hibernate session在open狀態,使session在request的整個期間都可以使用,如在View層裡PO也可以lazy loading資料,如 ${ company.employees }。當View 層邏輯完成後,才會通過Filter的doFilter方法或Interceptor的postHandle方法自動關閉session。

另外,<url-pattern>/*</url-pattern>中最好不要寫/*, 因為這樣的話,訪問所有的url都會為其開一個session,造成效能的下降;...餘下全文>>
 
Hibernate映射關係 no session or session was closed

spring 的OpenSessionInViewFilter簡介
假設在你的應用中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的方法之後sessi......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.