在spring+hibernate的時候,控制臺提示“unclosed connection,forgot to call close() on your session?”
          或者是[org.hibernate.jdbc.ConnectionManager] - <finalizing with closed connection>
          那是因為有可能是你自己手動創建了session,比如:

          private static final SessionFactory sessionFactory;

              static {
                  try {
                      // Create the SessionFactory from hibernate.cfg.xml
                      sessionFactory = new Configuration().configure().buildSessionFactory();
                  } catch (Throwable ex) {
                      // Make sure you log the exception, as it might be swallowed
                      System.err.println("Initial SessionFactory creation failed." + ex);
                      throw new ExceptionInInitializerError(ex);
                  }
              }
          然后調用:

          Session session = sessionFactory.openSession();
          Query query = session.createQuery("from Login");

          類似這樣的單獨使用hibernate時的用法,是需要手工去關閉session的。沒有關閉的話就會收到那樣的警告。

          所以最好是使用spring管理的session,和OpenSessionInViewFilter,比如:
          Session session =this.getSession();
          然后在web.xml里加入
            <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>/*</url-pattern>
          </filter-mapping>

          其中在spring里注冊的sessionfactory必須是id="sessionFactory",如果是id="SessionFactory"或者其他,會提示sessionFactory沒有注冊的錯誤,因為OpenSessionInViewFilter里注冊的名必須為sessionFactory。

          posted on 2008-01-10 18:41 lzj520 閱讀(4831) 評論(5)  編輯  收藏 所屬分類: Spring個人學習日記Hibernate
          主站蜘蛛池模板: 宣化县| 称多县| 云霄县| 白银市| 乐业县| 白河县| 灵璧县| 镇雄县| 纳雍县| 马关县| 高安市| 横山县| 临澧县| 巴彦淖尔市| 辽阳市| 仲巴县| 汝阳县| 阿拉善左旗| 平邑县| 阳城县| 新兴县| 碌曲县| 凤台县| 宣汉县| 建宁县| 邛崃市| 甘德县| 伽师县| 海原县| 临猗县| 万安县| 曲靖市| 新巴尔虎左旗| 东莞市| 鹿邑县| 汉川市| 莲花县| 浮梁县| 宝山区| 舒兰市| 外汇|