在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
          主站蜘蛛池模板: 清原| 南丰县| 彰化县| 华安县| 屏边| 巴青县| 电白县| 彩票| 嫩江县| 石棉县| 屏边| 女性| 永和县| 星座| 家居| 呈贡县| 拉萨市| 浦北县| 巩义市| 平度市| 永靖县| 汾西县| 富民县| 滨海县| 庆阳市| 通许县| 镇雄县| 安泽县| 富顺县| 涟水县| 年辖:市辖区| 正定县| 延安市| 长武县| 晋州市| 分宜县| 平舆县| 元谋县| 噶尔县| 怀集县| 东源县|