Ordinary hut

          人間一福地,勝似天仙宮
          posts - 61, comments - 50, trackbacks - 0, articles - 1
          getHibernateTemplate().getSessionFactory().getCurrentSession()的意思是得到當前線程 綁定的session,而當前線程綁定的session是通過當前的事務產生的,如果你沒有配置事務的話,當前線程threadlocal中就不存在 session,這樣就出現no session錯誤。

          而execute的回調方法,看源碼HibernateTemplate中寫道
              public Object execute(HibernateCallback action, boolean exposeNativeSession) throws DataAccessException {
                  Assert.notNull(action, "Callback object must not be null");

              
              Session session = getSession();
              
              boolean existingTransaction = (!isAlwaysUseNewSession() &&
              
              (!isAllowCreate() || SessionFactoryUtils.isSessionTransactional(session, getSessionFactory())));
          其中getSession,代碼如下
              protected Session getSession() {
                  
          if (isAlwaysUseNewSession()) {
                      
          return SessionFactoryUtils.getNewSession(getSessionFactory(), getEntityInterceptor());
                  }
                  
          else if (isAllowCreate()) {
                      
          return SessionFactoryUtils.getSession(
                      getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator());
                  }
                  
          else {
                      
          try {
                         
          return getSessionFactory().getCurrentSession();
                      }
                      
          catch (HibernateException ex) {
                          
          throw new DataAccessResourceFailureException("Could not obtain current Hibernate Session", ex);
                      }
                  }
              }
          其中默認private boolean alwaysUseNewSession = false,所以代碼會走到else if (isAllowCreate())
          注意這里:else if (isAllowCreate()),其中在HibernateTemplate類中默認private boolean allowCreate = true;
          意思說如果當前線程中的session不存在的話,是否允許創建,而默認是允許的,通過函數名字就很清楚,接下來是創建當前線程中的session的代碼,所以在沒有事務的狀態下,用execute回調方法,就不會出現上述問題。

          Feedback

          # re: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactiona  回復  更多評論   

          2011-09-09 14:17 by yiyi
          org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
          at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
          at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:700)
          at org.springside.modules.orm.hibernate.SimpleHibernateDao.getSession(SimpleHibernateDao.java:90)
          at org.springside.modules.orm.hibernate.SimpleHibernateDao.createCriteria(SimpleHibernateDao.java:284)
          at org.springside.modules.orm.hibernate.SimpleHibernateDao.findUniqueBy(SimpleHibernateDao.java:165)
          at com.wisedu.mcp.services.SysLogService.getAppByService(SysLogService.java:128)
          at com.wisedu.mcp.services.SysLogService.appAccess(SysLogService.java:163)
          at com.wisedu.mcp.services.SysLogService$$FastClassByCGLIB$$33723ece.invoke(<generated>)
          at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
          at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:617)
          at com.wisedu.mcp.services.SysLogService$$EnhancerByCGLIB$$24e4ac56.appAccess(<generated>)
          at com.wisedu.mcp.filter.CommonFilter.doFilter(CommonFilter.java:90)
          at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
          at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
          at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
          at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
          at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
          at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:440)
          at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
          at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
          at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
          at org.mortbay.jetty.Server.handle(Server.java:326)
          at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
          at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:926)
          at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549)
          at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
          at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
          at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
          at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

          # re: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactiona  回復  更多評論   

          2013-04-28 13:56 by lemon3135
          錯誤原因:
          通過定義好的sessionFactory的getCurrentSession()方法獲取當前線程中綁定的session,而當前線程綁定的session是通過當前的事務產生的,產生如上的錯誤是因為沒有配置事務。當前線程中沒創建session,則出現以上信息。
          主站蜘蛛池模板: 微山县| 广昌县| 浦东新区| 大名县| 尤溪县| 余姚市| 永城市| 建始县| 聂荣县| 武夷山市| 连南| 镇远县| 车险| 竹山县| 昂仁县| 巴南区| 韩城市| 全州县| 水城县| 郓城县| 西林县| 芦溪县| 游戏| 芒康县| 增城市| 临安市| 图木舒克市| 广灵县| 台江县| 上高县| 瑞金市| 平塘县| 洪泽县| 延川县| 青浦区| 文成县| 垦利县| 秭归县| 沂源县| 大悟县| 上林县|