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"的模式。例如: 它允許在事務提交之後延遲加載顯示所需要的對象。
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>
原理:
protected void doFilterInternal(
HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
SessionFactory sessionFactory = lookupSessionFactory(request);
boolean participate = false;
if (isSingleSession()) {
// single session mode
if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
// Do not modify the Session: just set the participate flag.
participate = true;
}
else {
logger.debug("Opening single Hibernate Session in OpenSessionInViewFilter");
Session session = getSession(sessionFactory);
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
}
}
else {
// deferred close mode
if (SessionFactoryUtils.isDeferredCloseActive(sessionFactory)) {
// Do not modify deferred close: just set the participate flag.
participate = true;
}
else {
SessionFactoryUtils.initDeferredClose(sessionFactory);
}
}
try {
filterChain.doFilter(request, response);
}
finally {
if (!participate) {
if (isSingleSession()) {
// single session mode
SessionHolder sessionHolder =
(SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory);
logger.debug("Closing single Hibernate Session in OpenSessionInViewFilter");
closeSession(sessionHolder.getSession(), sessionFactory);
}
else {
// deferred close mode
SessionFactoryUtils.processDeferredClose(sessionFactory);
}
}
}
}
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
/**
* 從spring的上下文中取得SessionFactory對象
* WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
* return (SessionFactory) wac.getBean(getSessionFactoryBeanName(),SessionFactory.class);
* getSessionFactoryBeanName()方法默認返回"sessionFactory"字符串,在spring配置文件中可要注意了,別寫錯了.
*/
SessionFactory sessionFactory = lookupSessionFactory(request);
boolean participate = false;// 標識過濾器結束時是否進行關閉session等後續處理
if (isSingleSession()) {//單session模式
//判斷能否在當前線程中取得sessionFactory對象對應的session
if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
//當能夠找到session的時候證明會有相關類處理session的收尾工作,這個過濾器不能進行關閉session操作,否則會出現重複關閉的情況.
participate = true;//但我並沒有想出正常使用的情況下什麼時候會出現這種情況.
} else {
Session session = getSession(sessionFactory);//當前線程取不到session的時候通過sessionFactory創建,下面還會詳細介紹此方法
//將session綁定到當前的線程中,SessionHolder是session的一層封裝,裏面有個存放session的map,而且是線程同步的Collections.synchronizedMap(new HashMap(1));
//但是單session模式下一個SessionHolder對應一個session,核心方法是getValidatedSession 取得一個open狀態下的session,並且取出後從map中移出.
TransactionSynchronizationManager.bindResource(sessionFactory,
; new SessionHolder(session));
}
} else {//這段是非單session模式的處理情況,沒有研究.但粗略看上去,大概思想是一樣的
if (SessionFactoryUtils.isDeferredCloseActive(sessionFactory)) {
participate = true;
} else {
SessionFactoryUtils.initDeferredClose(sessionFactory);
}
}
try {
//將session綁定到了當前線程後,就該處理請求了
filterChain.doFilter(request, response);
}finally {
if (!participate) {
if (isSingleSession()) {
//當請求結束時,對於單session模式,這時候要取消session的綁定,因爲web容器(Tomcat等)的線程是採用線程池機制的,線程使用過後並不會銷燬.
SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager
.unbindResource(sessionFactory);
//取消綁定只是取消session對象和線程對象之間的引用,還沒有關閉session,不關閉session相對於不關閉數據庫連接,所以這裏要關閉session
closeSession(sessionHolder.getSession(), sessionFactory);
} else {
//非單session模式,沒有研究.
SessionFactoryUtils.processDeferredClose(sessionFactory);
}
}
}
}
步驟:
1. 獲取session,打開session
2. filterChain.doFilter(request, response);
3. 關閉session
在2中可能執行了若干的Servlet、JSP、Action等等,最終處理完渲染完頁面之後,再進入OpenSessionInViewFilter的3關閉session現在只要保證在2中不論是Servlet、JSP還是Action中執行DAO時獲取的session都是1中打開的同一個session,並且在DAO關閉session時並不實際關閉,留到OpenSessionInViewFilter的3中再最終關閉,就實現了懶加載了,因爲只要是在OpenSessionInViewFilter過濾的範圍內,session都處於打開,比如在一個Servlet中查到一個Bean,這時他的關聯實體並沒有加載,當這個Servlet重定向到一個JSP,在其中得到這個Bean後直接訪問之前沒加載的那些關聯實體,會實時的加載這個關聯實體,因爲session還未關閉,這便是懶加載了。