@hunter129

          天天學習,好好向上!

             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            21 隨筆 :: 5 文章 :: 37 評論 :: 0 Trackbacks

          由于Spring控制的Hibernate的生命周期只針對數(shù)據(jù)層和服務層,而未管理到表現(xiàn)層,所以在表現(xiàn)層使用延時加載會出現(xiàn)the owning Session was closed或者no session or session was closed的異常信息。針對這一點,可以通過hibernate filter的方式來解決。

          在WEB.xml文件中配置filter.
          <!-- hibernate session filter -->
          <filter>
              
          <filter-name>OpenSessionInViewFilter</filter-name>
              
          <filter-class>
                  org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
              
          </filter-class>
          </filter>

          <filter-mapping>
              
          <filter-name>OpenSessionInViewFilter</filter-name>
              
          <url-pattern>*.do</url-pattern>
          </filter-mapping>
          我們的系統(tǒng)架構(gòu)是struts+spring+hibernate,struts跟spring的整合是在struts-config.xml里加了個plugin
          <plug-in
              
          className="org.springframework.WEB.struts.ContextLoaderPlugIn">
              
          <set-property property="contextConfigLocation"
                  value
          ="/WEB-INF/classes/applicationContext.xml" />
              
          </plug-in>

          在WEB.xml中配置hibernateFilter 后,還需要在struts-config.xml里把plugin去掉,在WEB.xml里加上如下代碼:
          <!--Spring ApplicationContext-->
          <context-param>
              
          <param-name>contextConfigLocation</param-name>
              
          <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
          </context-param>

          <listener>
              
          <listener-class>
                  org.springframework.web.context.ContextLoaderListener
              
          </listener-class>
          </listener>

          這樣配置之后如果沒有配置事務,是有問題的。不能進行update和insert操作了。
          怎么辦呢?只需要在filter中加入一個參數(shù)
          <init-param>
              <param-name>singleSession</param-name>
              <param-value>true</param-value>
          </init-param>
          就可以了,當然這樣 每次訪問dao都會新開個session,對性能的影響還是比較大的。最好的辦法當然是配置事務了。
          posted on 2008-04-18 10:12 hunter129 閱讀(1513) 評論(0)  編輯  收藏 所屬分類: 基礎(chǔ)
          主站蜘蛛池模板: 三台县| 丽江市| 安新县| 温泉县| 应城市| 衡阳市| 涞水县| 壶关县| 大名县| 革吉县| 麟游县| 岱山县| 东丽区| 西和县| 威远县| 富裕县| 西乌| 奉新县| 略阳县| 克山县| 新田县| 西宁市| 定兴县| 遂川县| 工布江达县| 保康县| 平谷区| 漠河县| 万年县| 清涧县| 犍为县| 仁寿县| 威远县| 兴文县| 易门县| 梁平县| 华安县| 吉林市| 石柱| 美姑县| 富锦市|