OpenSessionInViewFilter的作用
Spring為我們解決Hibernate的Session的關閉與開啟問題。
Hibernate 允許對關聯對象、屬性進行消極式載入,但是必須保證消極式載入的操作限於同一個 Hibernate Session 範圍之內進行。如果 Service 層返回一個啟用了消極式載入功能的領域對象給 Web 層,當 Web 層訪問到那些需要消極式載入的資料時,由於載入領域對象的 Hibernate Session 已經關閉,這些導致消極式載入資料的訪問異常
(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和一次完整的請求過程對應的線程相綁定。目的是為了實現"Open Session in View"的模式。例如: 它允許在事務提交之後消極式載入顯示所需要的對象。
而Spring為我們提供的OpenSessionInViewFilter過濾器為我們很好的解決了這個問題。OpenSessionInViewFilter的主要功能是用來把一個Hibernate Session和一次完整的請求過程對應的線程相綁定。目的是為了實現"Open Session in View"的模式。例如: 它允許在事務提交之後消極式載入顯示所需要的對象。
OpenSessionInViewFilter 過濾器將 Hibernate Session 綁定到請求線程中,它將自動被 Spring 的交易管理員探測到。所以 OpenSessionInViewFilter 適用於 Service 層使用HibernateTransactionManager 或 JtaTransactionManager 進行交易管理的環境,也可以用於非事務唯讀資料操作中。
<filter>
<filter-name>Spring OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<!--
指定org.springframework.orm.hibernate3.LocalSessionFactoryBean在spring設定檔中的名稱,預設值為sessionFactory
如果LocalSessionFactoryBean在spring中的名稱不是sessionFactory,該參數一定要指定,否則會出現找不到sessionFactory的例外
-->
<param-name>sessionFactoryBean</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Spring OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/sunsea08/archive/2009/09/12/4545186.aspx
-------------------------------------------------錯誤問題解決1 ----------------------------------------------------------------------------------------
web.xml原始配置:
<!-- 過濾spring中對於hibernate的session關閉管理 -->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
自己寫的serviceImpl.java檔案中的儲存更新方法(我所出現問題的位置是:多行提交的方法),在運行時總報錯。如下:
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
後來在網上狂搜解決方案,將web.xml檔案改為如下:
<!-- 過濾spring中對於hibernate的session關閉管理 -->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>false</param-value>
</init-param>
</filter>
上面的異常解決了,但又報出新的異常,如下:
org.hibernate.HibernateException: Illegal attempt to associate a collection
with two open sessions
解決這個問題的辦法就是要把singleSession的值改為true
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
我無奈了,這兩個錯誤就好像是相互的,只能解決一個。。。
從網上搜。。還真我出現的這咱情況。。結果如下:
說明一下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
代碼:
protected Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
Session session = SessionFactoryUtils.getSession(sessionFactory, true);
session.setFlushMode(FlushMode.NEVER);
return session;
}
看getSession的方式就知道,把flush mode 設為FlushMode.NEVER,這樣就算是commit的時候也不會session flush,
如果想在完成request過程中更新資料的話, 那就需要先把flush model設為FlushMode.AUTO,再在更新完資料後flush.
OpenSessionInView預設的FlushMode為
代碼:
FlushMode.NEVER
可以採用在寫儲存更新刪除代碼的時候手動更改FlushMode
代碼:
this.getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
session.setFlushMode(FlushMode.AUTO);
session.save(user);
session.flush();
return null;
}
});
但是這樣做太繁瑣了,第二種方式是採用spring的事務聲明
代碼:
<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="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>
太巧了,我們的架構就採用了這位前輩所說的第二種方案,但是為什麼我把設定檔改成他說的樣式還是不行呢?
鬱悶中驚奇發現,不是我所有的多行提交都出問題,而只是個別的。經過一翻考慮後,確定自己寫的方法體沒有
問題了,那麼就是方法名了,才發現,還真是方法名的問題。
原來自己寫的serviceImpl.java檔案的方法名要用“load”“save”“add”“update”“remove”這些詞開頭,這個就好像是通過這個bean-service.xml檔案管理方法名一樣,超出這個範圍了,hibernate自身的作用就發揮不出來了。
-----------------------------------------------------錯誤問題解決2------------------------------------------------------------------------------
同時使用ContextLoaderListener和ContextLoaderPlugIn,不要在ContextLoaderPlugIn裡面加入applicationContext.xml,只要加入action-servlet.xml,OpenSessionInView可生效。
因為ContextLoaderListener儲存的對象的是key WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE!而ContextLoaderPlugIn儲存的對象的是key是attrName,這個attrName和WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE的值是不一樣的,而OpenSessionInViewFilter是從WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE擷取spring配置資訊的,所以如果將applicationContext.xml以外掛程式的形式配置,則OpenSessionInViewFilter擷取不到spring的配置資訊,OpenSessionInViewFilter可能會失效。