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,如
?????盡管Open Session In View看起來還不錯,其實副作用不少。看回上面OpenSessionInViewFilter的doFilterInternal方法代碼,這個方法實際上是被父類的doFilter調用的,因此,我們可以大約了解的OpenSessionInViewFilter調用流程: request(請求)->open session并開始transaction->controller->View(Jsp)->結束transaction并close session.
???? 一切看起來很正確,尤其是在本地開發測試的時候沒出現問題,但試想下如果流程中的某一步被阻塞的話,那在這期間connection就一直被占用而不釋放。最有可能被阻塞的就是在寫Jsp這步,一方面可能是頁面內容大,response.write的時間長,另一方面可能是網速慢,服務器與用戶間傳輸時間久。當大量這樣的情況出現時,就有連接池連接不足,造成頁面假死現象。