在spring+hibernate的時(shí)候,控制臺提示“unclosed connection,forgot to call close() on your session?”
          或者是[org.hibernate.jdbc.ConnectionManager] - <finalizing with closed connection>
          那是因?yàn)橛锌赡苁悄阕约菏謩觿?chuàng)建了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);
                  }
              }
          然后調(diào)用:

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

          類似這樣的單獨(dú)使用hibernate時(shí)的用法,是需要手工去關(guān)閉session的。沒有關(guān)閉的話就會收到那樣的警告。

          所以最好是使用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沒有注冊的錯(cuò)誤,因?yàn)镺penSessionInViewFilter里注冊的名必須為sessionFactory。

          posted on 2008-01-10 18:41 lzj520 閱讀(4838) 評論(5)  編輯  收藏 所屬分類: Spring個(gè)人學(xué)習(xí)日記Hibernate
          主站蜘蛛池模板: 广德县| 伊宁县| 灵山县| 信宜市| 永泰县| 宁夏| 昌乐县| 黎城县| 贵州省| 江阴市| 固原市| 南阳市| 双柏县| 乡宁县| 南丰县| 宝兴县| 郁南县| 普兰店市| 贵港市| 南漳县| 商水县| 塔河县| 白城市| 临朐县| 凤台县| 寻甸| 陆良县| 奇台县| 姚安县| 抚顺县| 泽普县| 高雄市| 武宣县| 兴隆县| 平阳县| 富平县| 镇宁| 县级市| 宁都县| 垫江县| 武功县|