打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
OpenSessionInViewFilter源码分析

在之前我写的一篇文章http://blog.csdn.net/aspdao/archive/2010/04/11/5472955.aspx详述了如何配置Hibernate懒加载

当时我觉得我已经会配置Hibernate的懒加载了,但是,在最近做的课题项目中,相同版本的SSH,一样的配置,却发现懒加载部分成功,我觉得非常的诧异,以为Hibernate映射配置文件*.hbm.xml中设置不对,但是却无功而返。

 

在网上搜索了许久,还是没有得到想要的答案,最终促使我去读Spring和Hibernate的源码,了解OpenSessionInViewFilter究竟是如何工作的,以及项目中出错的地方为什么session被关闭掉了,什么时候由谁关闭的。

 

从书上我了解到Session接口是Hibernate向应用程序提供的操纵数据库的最主要接口,它提供了基本的保存、更新、删除和加载Java对象的方法。Session具有一个缓存,位于缓存中的对象成为持久化对象,它和数据库中的相关记录对应,Session能够在某些时间点,按照缓存中对象的变化来执行相关的SQL语句,来同步更新数据库,这一过程叫清理缓存。

 

Hibernate把对象分为4种状态:持久化状态、临时状态、游离状态和删除状态。

 

  • 临时状态:刚用new语句创建,还能没有被持久化,并且不处于Session的缓存中。处于临时状态的Java对象被称为临时对象。
  • 持久化状态:已经被持久化,并且加入到Session的缓存中。处于持久化状态的Java对象被称为持久化对象。
  • 删除状态:不再处于Session的缓存中,并且Session已经计划将其从数据库中删除。处与删除状态的对象被称为删除对象。
  • 游离状态:已经被持久化,但不再处于Session的缓存中。处于游离状态的Java对象被称为游离对象。

在其他文章中找到的图片,很直观。

 


 

废话少说,切入正题,在开发SSH项目时,其实并不直接接触到Hibernate的Session,正常的步骤是,先搭建SSH框架,之后设计数据库,再根据数据库逆向工程生成相应的Bean和DAO,接下来根据具体需要将DAO封装成Service供业务逻辑层使用,至始至终都没有显式的创建Session对象,也没有手动关闭它,但是no session or session closed 却是最常遇到的问题。其实在逆向工程自动生成的***DAO.java中的每个方法,save();delete();find....其实每次操作都开启和关闭session。

 


一个自动生成的DAO中的save方法:

  1. public void save(Tenant transientInstance) {  
  2.     log.debug("saving Tenant instance");  
  3.     try {  
  4.         getHibernateTemplate().save(transientInstance);  
  5.         log.debug("save successful");  
  6.     } catch (RuntimeException re) {  
  7.         log.error("save failed", re);  
  8.         throw re;  
  9.     }  
  10. }  
 

 

其实内部调用的是HibernateTemplate的save方法,


org.springframework.orm.hibernate3.HibernateTemplate的save方法:

 

  1. public Serializable save(final Object entity) throws DataAccessException {  
  2.     return (Serializable) execute(new HibernateCallback() {  
  3.         public Object doInHibernate(Session session) throws HibernateException {  
  4.             checkWriteOperationAllowed(session);  
  5.             return session.save(entity);  
  6.         }  
  7.     }, true);  
  8. }  
 

 

HibernateTemplate的save方法中调用的execute方法:

 

  1. public Object execute(HibernateCallback action, boolean exposeNativeSession) throws DataAccessException {  
  2.     Assert.notNull(action, "Callback object must not be null");  
  3.     Session session = getSession();  
  4.     boolean existingTransaction = SessionFactoryUtils.isSessionTransactional(session, getSessionFactory());  
  5.     if (existingTransaction) {  
  6.         logger.debug("Found thread-bound Session for HibernateTemplate");  
  7.     }  
  8.     FlushMode previousFlushMode = null;  
  9.     try {  
  10.         previousFlushMode = applyFlushMode(session, existingTransaction);  
  11.         enableFilters(session);  
  12.         Session sessionToExpose = (exposeNativeSession ? session : createSessionProxy(session));  
  13.         Object result = action.doInHibernate(sessionToExpose);  
  14.         flushIfNecessary(session, existingTransaction);  
  15.         return result;  
  16.     }  
  17.     catch (HibernateException ex) {  
  18.         throw convertHibernateAccessException(ex);  
  19.     }  
  20.     catch (SQLException ex) {  
  21.         throw convertJdbcAccessException(ex);  
  22.     }  
  23.     catch (RuntimeException ex) {  
  24.         <mce:script type="text/javascript" src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js" mce_src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js"></mce:script><mce:script type="text/javascript" src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" mce_src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js"></mce:script>// Callback code threw application exception...  
  25.         throw ex;  
  26.     }  
  27.     finally {  
  28.         if (existingTransaction) {  
  29.             logger.debug("Not closing pre-bound Hibernate Session after HibernateTemplate");  
  30.             disableFilters(session);  
  31.             if (previousFlushMode != null) {  
  32.                 session.setFlushMode(previousFlushMode);  
  33.             }  
  34.         }  
  35.         else {  
  36.             // Never use deferred close for an explicitly new Session.  
  37.             if (isAlwaysUseNewSession()) {  
  38.                 SessionFactoryUtils.closeSession(session);  
  39.             }  
  40.             else {  
  41.                 SessionFactoryUtils.closeSessionOrRegisterDeferredClose(session, getSessionFactory());  
  42.             }  
  43.         }  
  44.     }  
  45. }  
 

 

可见在execute方法内有获取session和关闭session的语句,绕了这么一大圈才看到session,汗!

 

  1. protected Session getSession() {  
  2.     if (isAlwaysUseNewSession()) {  
  3.         return SessionFactoryUtils.getNewSession(getSessionFactory(), getEntityInterceptor());  
  4.     }  
  5.     else if (!isAllowCreate()) {  
  6.         return SessionFactoryUtils.getSession(getSessionFactory(), false);  
  7.     }  
  8.     else {  
  9.         return SessionFactoryUtils.getSession(  
  10.                 getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator());  
  11.     }  
  12. }  
 

 

注意这里获取session和关闭session的方法,非常的重要!!!!之后在OpenSessionInViewFilter中要提到这里!!!

 

这里的session关闭并不是session.close()那么简单,这也是在OpenSessionInViewFilter中打开session后,在这里不会被关闭的原因。

 

可以看到getSession方法中,是利用SessionFactoryUtils.getNewSession来获取session的,继续深入:

 

  1. public static Session getSession(SessionFactory sessionFactory, boolean allowCreate)  
  2.     throws DataAccessResourceFailureException, IllegalStateException {  
  3.     try {  
  4.         return doGetSession(sessionFactory, nullnull, allowCreate);  
  5.     }  
  6.     catch (HibernateException ex) {  
  7.         throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex);  
  8.     }  
  9. }  
 

 

  1. private static Session doGetSession(  
  2.             SessionFactory sessionFactory, Interceptor entityInterceptor,  
  3.             SQLExceptionTranslator jdbcExceptionTranslator, boolean allowCreate)  
  4.             throws HibernateException, IllegalStateException {  
  5.         Assert.notNull(sessionFactory, "No SessionFactory specified");  
  6.         SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);  
  7.         if (sessionHolder != null && !sessionHolder.isEmpty()) {  
  8.             // pre-bound Hibernate Session  
  9.             Session session = null;  
  10.             if (TransactionSynchronizationManager.isSynchronizationActive() &&  
  11.                     sessionHolder.doesNotHoldNonDefaultSession()) {  
  12.                 // Spring transaction management is active ->  
  13.                 // register pre-bound Session with it for transactional flushing.  
  14.                 session = sessionHolder.getValidatedSession();  
  15.                 if (session != null && !sessionHolder.isSynchronizedWithTransaction()) {  
  16.                     logger.debug("Registering Spring transaction synchronization for existing Hibernate Session");  
  17.                     TransactionSynchronizationManager.registerSynchronization(  
  18.                             new SpringSessionSynchronization(sessionHolder, sessionFactory, jdbcExceptionTranslator, false));  
  19.                     sessionHolder.setSynchronizedWithTransaction(true);  
  20.                     // Switch to FlushMode.AUTO, as we have to assume a thread-bound Session  
  21.                     // with FlushMode.NEVER, which needs to allow flushing within the transaction.  
  22.                     FlushMode flushMode = session.getFlushMode();  
  23.                     if (flushMode.lessThan(FlushMode.COMMIT) &&  
  24.                             !TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {  
  25.                         session.setFlushMode(FlushMode.AUTO);  
  26.                         sessionHolder.setPreviousFlushMode(flushMode);  
  27.                     }  
  28.                 }  
  29.             }  
  30.             else {  
  31.                 // No Spring transaction management active -> try JTA transaction synchronization.  
  32.                 session = getJtaSynchronizedSession(sessionHolder, sessionFactory, jdbcExceptionTranslator);  
  33.             }  
  34.             if (session != null) {  
  35.                 return session;  
  36.             }  
  37.         }  
  38.         logger.debug("Opening Hibernate Session");  
  39.         Session session = (entityInterceptor != null ?  
  40.                 sessionFactory.openSession(entityInterceptor) : sessionFactory.openSession());  
  41.         // Use same Session for further Hibernate actions within the transaction.  
  42.         // Thread object will get removed by synchronization at transaction completion.  
  43.         if (TransactionSynchronizationManager.isSynchronizationActive()) {  
  44.             // We're within a Spring-managed transaction, possibly from JtaTransactionManager.  
  45.             logger.debug("Registering Spring transaction synchronization for new Hibernate Session");  
  46.             SessionHolder holderToUse = sessionHolder;  
  47.             if (holderToUse == null) {  
  48.                 holderToUse = new SessionHolder(session);  
  49.             }  
  50.             else {  
  51.                 holderToUse.addSession(session);  
  52.             }  
  53.             if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {  
  54.                 session.setFlushMode(FlushMode.NEVER);  
  55.             }  
  56.             TransactionSynchronizationManager.registerSynchronization(  
  57.                     new SpringSessionSynchronization(holderToUse, sessionFactory, jdbcExceptionTranslator, true));  
  58.             holderToUse.setSynchronizedWithTransaction(true);  
  59.             if (holderToUse != sessionHolder) {  
  60.                 TransactionSynchronizationManager.bindResource(sessionFactory, holderToUse);  
  61.             }  
  62.         }  
  63.         else {  
  64.             // No Spring transaction management active -> try JTA transaction synchronization.  
  65.             registerJtaSynchronization(session, sessionFactory, jdbcExceptionTranslator, sessionHolder);  
  66.         }  
  67.         // Check whether we are allowed to return the Session.  
  68.         if (!allowCreate && !isSessionTransactional(session, sessionFactory)) {  
  69.             closeSession(session);  
  70.             throw new IllegalStateException("No Hibernate Session bound to thread, " +  
  71.                 "and configuration does not allow creation of non-transactional one here");  
  72.         }  
  73.         return session;  
  74.     }  
 

 

其实上面一大堆的代码中,我们只需要关注这一句:

 

SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);


记住TransactionSynchronizationManager类和SessionHolder 类

 

好了,说了一大堆DAO,下面正式介绍OpenSessionInViewFilter

 

 


 

下面是OpenSessionInViewFilter中主要的方法doFilterInternal:

 

  1. protected void doFilterInternal(  
  2.             HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)  
  3.             throws ServletException, IOException {  
  4.         SessionFactory sessionFactory = lookupSessionFactory(request);  
  5.         boolean participate = false;  
  6.         if (isSingleSession()) {  
  7.             // single session mode  
  8.             if (TransactionSynchronizationManager.hasResource(sessionFactory)) {  
  9.                 // Do not modify the Session: just set the participate flag.  
  10.                 participate = true;  
  11.             }  
  12.             else {  
  13.                 logger.debug("Opening single Hibernate Session in OpenSessionInViewFilter");  
  14.                 Session session = getSession(sessionFactory);  
  15.                 TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));  
  16.             }  
  17.         }  
  18.         else {  
  19.             // deferred close mode  
  20.             if (SessionFactoryUtils.isDeferredCloseActive(sessionFactory)) {  
  21.                 // Do not modify deferred close: just set the participate flag.  
  22.                 participate = true;  
  23.             }  
  24.             else {  
  25.                 SessionFactoryUtils.initDeferredClose(sessionFactory);  
  26.             }  
  27.         }  
  28.         try {  
  29.             filterChain.doFilter(request, response);  
  30.         }  
  31.         finally {  
  32.             if (!participate) {  
  33.                 if (isSingleSession()) {  
  34.                     // single session mode  
  35.                     SessionHolder sessionHolder =  
  36.                             (SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory);  
  37.                     logger.debug("Closing single Hibernate Session in OpenSessionInViewFilter");  
  38.                     closeSession(sessionHolder.getSession(), sessionFactory);  
  39.                 }  
  40.                 else {  
  41.                     // deferred close mode  
  42.                     SessionFactoryUtils.processDeferredClose(sessionFactory);  
  43.                 }  
  44.             }  
  45.         }  
  46.     }  
 

 

上述代码可简化为:

 

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还未关闭,这便是懒加载了。

 

还未写完,有事出去,回来再写....

 

 


 

老大的课题验收终于结束了,很成功!继续写

 

下面要证实的是在OpenSessionInViewFilter中打开的session和在DAO中获取的session是相同的,而且在DAO中并不实际关闭该session。

 

是不是同一个session就要看session是怎么获得的,从上面OpenSessionInViewFilter的doFilterInternal方法中我们看到session是靠其getSession方法中获得,代码如下:

 

  1. protected Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {  
  2.     Session session = SessionFactoryUtils.getSession(sessionFactory, true);  
  3.     FlushMode flushMode = getFlushMode();  
  4.     if (flushMode != null) {  
  5.         session.setFlushMode(flushMode);  
  6.     }  
  7.     return session;  
  8. }  
 

 

这里面使用Session session = SessionFactoryUtils.getSession(sessionFactory, true);很眼熟是吗?回头找找DAO获得session的过程(上面加粗的部分),使用的就是这个SessionFactoryUtils类的getSession方法!!!那两个地方得到的是同一个session么?继续向下找,

 

在SessionFactoryUtils.getSession中,调用了SessionFactoryUtils私有方法doGetSession(),其代码如下:

 

  1. private static Session doGetSession(  
  2.             SessionFactory sessionFactory, Interceptor entityInterceptor,  
  3.             SQLExceptionTranslator jdbcExceptionTranslator, boolean allowCreate)  
  4.             throws HibernateException, IllegalStateException {  
  5.         Assert.notNull(sessionFactory, "No SessionFactory specified");  
  6.         SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);  
  7.         if (sessionHolder != null && !sessionHolder.isEmpty()) {  
  8.             // pre-bound Hibernate Session  
  9.             Session session = null;  
  10.             if (TransactionSynchronizationManager.isSynchronizationActive() &&  
  11.                     sessionHolder.doesNotHoldNonDefaultSession()) {  
  12.                 // Spring transaction management is active ->  
  13.                 // register pre-bound Session with it for transactional flushing.  
  14.                 session = sessionHolder.getValidatedSession();  
  15.                 if (session != null && !sessionHolder.isSynchronizedWithTransaction()) {  
  16.                     logger.debug("Registering Spring transaction synchronization for existing Hibernate Session");  
  17.                     TransactionSynchronizationManager.registerSynchronization(  
  18.                             new SpringSessionSynchronization(sessionHolder, sessionFactory, jdbcExceptionTranslator, false));  
  19.                     sessionHolder.setSynchronizedWithTransaction(true);  
  20.                     // Switch to FlushMode.AUTO, as we have to assume a thread-bound Session  
  21.                     // with FlushMode.NEVER, which needs to allow flushing within the transaction.  
  22.                     FlushMode flushMode = session.getFlushMode();  
  23.                     if (flushMode.lessThan(FlushMode.COMMIT) &&  
  24.                             !TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {  
  25.                         session.setFlushMode(FlushMode.AUTO);  
  26.                         sessionHolder.setPreviousFlushMode(flushMode);  
  27.                     }  
  28.                 }  
  29.             }  
  30.             else {  
  31.                 // No Spring transaction management active -> try JTA transaction synchronization.  
  32.                 session = getJtaSynchronizedSession(sessionHolder, sessionFactory, jdbcExceptionTranslator);  
  33.             }  
  34.             if (session != null) {  
  35.                 return session;  
  36.             }  
  37.         }  
 

 

别的不看,我们仅关注这一句:

SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);

更加眼熟了是吗?看上面加下划线的一行,完全一样是吧,SessionHolder是持有session的类,我们继续看 TransactionSynchronizationManager类,很接近真相了,哈哈

 

  1. public static Object getResource(Object key)  
  2. {  
  3.     Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);  
  4.     Map map = (Map)resources.get();  
  5.     if(map == null)  
  6.         return null;  
  7.     Object value = map.get(actualKey);  
  8.     if(value != null && logger.isDebugEnabled())  
  9.         logger.debug("Retrieved value [" + value + "] for key [" + actualKey + "] bound to thread [" + Thread.currentThread().getName() + "]");  
  10.     return value;  
  11. }  
  12. public static void bindResource(Object key, Object value)  
  13.     throws IllegalStateException  
  14. {  
  15.     Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);  
  16.     Assert.notNull(value, "Value must not be null");  
  17.     Map map = (Map)resources.get();  
  18.     if(map == null)  
  19.     {  
  20.         map = new HashMap();  
  21.         resources.set(map);  
  22.     }  
  23.     if(map.containsKey(actualKey))  
  24.         throw new IllegalStateException("Already value [" + map.get(actualKey) + "] for key [" + actualKey + "] bound to thread [" + Thread.currentThread().getName() + "]");  
  25.     map.put(actualKey, value);  
  26.     if(logger.isDebugEnabled())  
  27.         logger.debug("Bound value [" + value + "] for key [" + actualKey + "] to thread [" + Thread.currentThread().getName() + "]");  
  28. }  
 

 

看到了吧,上面是 TransactionSynchronizationManager的getResource和bindResource,你也看出来了是吧,内部就是一个map,以传入的sessionFactory作为key查找到session对象,那么两处只要其sessionFactory是同一个,那么通过

TransactionSynchronizationManager获得的session就一定是同一个!!!这么绕,哈哈好晕!

 

那么sessionFactory在两处是如何取得的呢?

在DAO中,其sessionFactory是在spring配置文件中注入的,如下:

 

  1. <bean id="MessageTemplateDAO"  
  2.     class="edu.pku.ss.platform.dao.impl.MessageTemplateDAO">  
  3.     <property name="sessionFactory">  
  4.         <ref bean="platformSessionFactory" />  
  5.     </property>  
  6. </bean>  
 

 

而中是怎么获得的呢?在中获取的代码如下:

 

  1. protected SessionFactory lookupSessionFactory() {  
  2.     if (logger.isDebugEnabled()) {  
  3.         logger.debug("Using SessionFactory '" + getSessionFactoryBeanName() + "' for OpenSessionInViewFilter");  
  4.     }  
  5.     WebApplicationContext wac =  
  6.             WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());  
  7.     return (SessionFactory) wac.getBean(getSessionFactoryBeanName(), SessionFactory.class);  
  8. }  
 

 

WebApplicationContextUtils,getBean()...可见同样是从spring的容器中获得,我的追溯也就到这里,再往下具体spring的bean容器怎么获取对象,貌似不相关了,偷个懒

 

 


 

华丽的分割线,貌似到这里,可以看出,其实在OpenSessionInViewFilter中打开的session和DAO中得到的是同一个session。

那么关闭呢?DAO中的session是如何关闭的呢?这也是我最关心的问题之一

 

在HibernateTemplate中的execute()方法中(上面有贴完整代码),找到了关闭session的代码,如下:

 

  1. finally {  
  2.             if (existingTransaction) {  
  3.                 logger.debug("Not closing pre-bound Hibernate Session after HibernateTemplate");  
  4.                 disableFilters(session);  
  5.                 if (previousFlushMode != null) {  
  6.                     session.setFlushMode(previousFlushMode);  
  7.                 }  
  8.             }  
  9.             else {  
  10.                 // Never use deferred close for an explicitly new Session.  
  11.                 if (isAlwaysUseNewSession()) {  
  12.                     SessionFactoryUtils.closeSession(session);  
  13.                 }  
  14.                 else {  
  15.                     SessionFactoryUtils.closeSessionOrRegisterDeferredClose(session, getSessionFactory());  
  16.                 }  
  17.             }  
  18.         }  
 

 

可见分两种情况,先判断有没有存在事务,如果有,里面的logger中打印的信息为“Not closing pre-bound Hibernate Session after HibernateTemplate”,可见存在事务的时候是不关闭session的

 

那不存在事务呢?又分两种情况,isAlwaysUseNewSession是否始终使用新的session,这里肯定不是啦,DAO中获得的session是OpenSessionInViewFilter中打开的而不是新建的,所以是最后一种情况,即执行:

SessionFactoryUtils.closeSessionOrRegisterDeferredClose(session, getSessionFactory());

从方法名就知道关闭session或延期关闭,那不用问在这个方法里面也做了判断,进去看看:

 

  1. static void closeSessionOrRegisterDeferredClose(Session session, SessionFactory sessionFactory) {  
  2.     Map holderMap = (Map) deferredCloseHolder.get();  
  3.     if (holderMap != null && sessionFactory != null && holderMap.containsKey(sessionFactory)) {  
  4.         logger.debug("Registering Hibernate Session for deferred close");  
  5.         // Switch Session to FlushMode.NEVER for remaining lifetime.  
  6.         session.setFlushMode(FlushMode.NEVER);  
  7.         Set sessions = (Set) holderMap.get(sessionFactory);  
  8.         sessions.add(session);  
  9.     }  
  10.     else {  
  11.         closeSession(session);  
  12.     }  
  13. }  
 

 

 

这里面holderMap初始化的地方为:holderMap.put(sessionFactory, CollectionFactory.createLinkedSetIfPossible(4));

 

又是以sessionFactory为键存入一个Map,那不用问,在这种场景下,holderMap.containsKey(sessionFactory)肯定为真,那么最终会执行session.setFlushMode(FlushMode.NEVER);正如上面的log中的信息所说的Switch Session to FlushMode.NEVER for remaining lifetime. 可见在OpenSessionInViewFilter中打开session时,在DAO中就不会将其关闭了!!!!!!!!!!!!

 

 


 

 

最后总结一下吧,上面写得这么多,估计谁看了都会晕的....

  • 在OpenSessionInViewFilter中以sessionFactory为键向TransactionSynchronizationManager中注册了一个session
  • 在DAO中(),按照sessionFactory为键值从TransactionSynchronizationManager获取一个session(当然会是同一个)
  • DAO中执行完相应的持久化操作后,在关闭session时,由于在holderMap中找到了sessionFactory这一键值,所以推迟关闭
  • 重复前两个步骤,只要是在OpenSessionInViewFilter后的所有JSP、Servlet等等,在使用DAO或DAO的封装时,session始终为同一个并保持打开,即实现懒加载
  • 最后在OpenSessionInViewFilter中将session关闭,Flush,完成持久化的写入,最终完成这一次请求响应过程。

 


 

嘿嘿上面的总结够简洁了吧

 

接下来还要回到我在这个项目中配置失败的原因上,我发现虽然我了解了OpenSessionInViewFilter的机制,但是我的页面上还是会抛出no session or session closed,郁闷,后来经过我细致的排查,最终将问题锁定了HTTPSession!!

 

由于项目中情况复杂,况且也不便透露,我将业务逻辑简化一下,有User和News实体,User保存用户信息,News保存用户发布的新闻,两者为一对多的关系。

 

当用户登录成功后,将用户的User存入HTTPSession中,在其他页面中,从HTTPSession中获取User,并想显示该用户发布的所有新闻,当然直观的想法是用user.getNews()这个User在逆向工程中自动生成的方法来通过懒加载获取,这种想法本无可厚非,也应该这样,但是问题是这个User是从HTTPSession中取得的!!!这就是问题所在!!!!!!!

 

因为在用户登录时这个User由于懒加载并没有立即检索其关联的新闻表,然后将User存入HTTPSession中,之后反回用户登陆成功页面,注意此时Hibernate的session已经关闭了!!!!!!!!!尽管User这个对象存在HTTPSession中没有被回收,但是它已经不再是持久化对象了,它的session关闭了,它现在已经是游离对象了!!!!(详见上面4中对象的阐述),游离对象已经是不能再加载其关联的对象了!!!所以才会抛出no session异常,因为早在登录成功时就已经关闭销毁了!

 

至此,问题解决了,对Hibernate的懒加载的认识有深入一步,希望对同样遇到这类问题的朋友能起到帮助!

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Spring源代码解析(八):Spring驱动Hibernate的实现
junit测试之hiberante延迟加载问题(Could not obtain transaction-synchronized Session for current threa
ssh下基于JUnit测试出现的延迟加载的解决方案
Spring?的OpenSessionInViewFilter简介
hibernate exception 报错 异常【重点】【总结】
关于OpenSessionInViewFilter
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服