@hunter129

          天天學習,好好向上!

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

          由于Spring控制的Hibernate的生命周期只針對數據層和服務層,而未管理到表現層,所以在表現層使用延時加載會出現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>
          我們的系統架構是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中加入一個參數
          <init-param>
              <param-name>singleSession</param-name>
              <param-value>true</param-value>
          </init-param>
          就可以了,當然這樣 每次訪問dao都會新開個session,對性能的影響還是比較大的。最好的辦法當然是配置事務了。
          posted on 2008-04-18 10:12 hunter129 閱讀(1518) 評論(0)  編輯  收藏 所屬分類: 基礎
          主站蜘蛛池模板: 晋州市| 上栗县| 遂昌县| 榆林市| 广宗县| 金阳县| 宜章县| 通渭县| 黄浦区| 龙州县| 龙陵县| 施甸县| 仪陇县| 泸溪县| 汕尾市| 遂溪县| 铅山县| 荣成市| 河北区| 绥滨县| 渭源县| 茌平县| 视频| 札达县| 衡东县| 长春市| 靖西县| 昭通市| 延津县| 太原市| 汉寿县| 襄汾县| 高州市| 阿拉尔市| 嘉鱼县| 夹江县| 卓尼县| 棋牌| 云霄县| 乐平市| 通化县|