hibernate管理连接是连接池方式,因此虽然是close方法,但只是把连接交给连接池,并不是真的关闭。你只要保证在finally段里调用了Session.close()方法就可以。或者,可以参考spring对hibernate的封装。网络上也有一些简单的封装方式。

解决方案 »

  1.   

    关注,小第也遇到这样的问题,每次我打开一个 session,后我都调用了 session.close(),可是从jboss返回的信息,总说没有关闭连接。是怎么回事袄???
      

  2.   

    cm4ever 你好,谢谢你的解答 :)问题是现在数据库的连接不断累积,最后甚至超过了数据库允许的最大连接数,让系统无法正常运行啊。而且,我的connection.pool.size只定了为1而已,即使是返回到连接池也不应该还保留那么多连接啊各位高手,请多多指点,你的举手之劳,可以帮助很多人,谢谢
      

  3.   

    我也用了C3P0,还有dbcp连接池,程序出问题,还不如不用正常。
    这种情况怎么解决啊?
      

  4.   

    配了C3P0后,连接问题解决了,但是却经常出这样的异常:Problem with checked-in Statement, discarding.
    java.lang.NullPointerException
    at oracle.jdbc.dbaccess.DBData.clearItem(DBData.java:431)
    at oracle.jdbc.dbaccess.DBDataSetImpl.clearItem(DBDataSetImpl.java:3528)
    …………不知道是哪里出了问题?
      

  5.   

    完整的:Problem with checked-in Statement, discarding.
    java.lang.NullPointerException
    at oracle.jdbc.dbaccess.DBData.clearItem(DBData.java:431)
    at oracle.jdbc.dbaccess.DBDataSetImpl.clearItem(DBDataSetImpl.java:3528)
    at oracle.jdbc.driver.OraclePreparedStatement.clearParameters(OraclePreparedStatement.java:3401)
    at com.mchange.v2.c3p0.stmt.GooGooStatementCache.refreshStatement(GooGooStatementCache.java:460)
    at com.mchange.v2.c3p0.stmt.GooGooStatementCache.checkinStatement(GooGooStatementCache.java:139)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnection$1WrapperStatementHelper.doClose(C3P0PooledConnection.java:511)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnection$2.close(C3P0PooledConnection.java:570)
    at net.sf.hibernate.impl.BatcherImpl.closePreparedStatement(BatcherImpl.java:270)
    at net.sf.hibernate.impl.BatcherImpl.closeStatement(BatcherImpl.java:140)
    at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:129)
    at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2421)
    at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2371)
    at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2240)
      

  6.   

    你关闭session后,设该session=null试试。另外,一般推荐用threadlocal来管理session
      

  7.   

    有啊,我是用threadlocal来管理的public static final ThreadLocal session = new ThreadLocal();
    public static Session currentSession() throws HibernateException
    {
    Session s = (Session) session.get();
    //    Open a new Session, if this Thread has none yet
    if (s == null) {
    s = sessionFactory.openSession();
    session.set(s);
    }
    return s;
    }
    public static void closeSession() throws HibernateException {
    Session s = (Session) session.get();
    session.set(null);
    if (s != null)
    s.close();
    }
      

  8.   

    我也遇到同样的问题,用c3p0显示Problem with checked-in Statement, discarding.错误。
    用dbcp显示数据库打开游标数超过最大值,
    我用的是oracle是不是要用tomcat的连接池才能解决问题?
      

  9.   

    不仅要session.close(),还要SessionFactory.close().前几天不知道,程序运行一会儿,开了200多个session,汗!!1
      

  10.   

    SessionFactory.close()掉,那下次查询还要重新建SessionFactory?
    好像不应该这样吧?
      

  11.   


    myclipse生成的例子:package com.mywebapp.hibernate;import net.sf.hibernate.HibernateException;
    import net.sf.hibernate.Session;
    import net.sf.hibernate.cfg.Configuration;/**
     * Configures and provides access to Hibernate sessions, tied to the
     * current thread of execution.  Follows the Thread Local Session
     * pattern, see {@link http://hibernate.org/42.html}.
     */
    public class HibernateSessionFactory {    /** 
         * Location of hibernate.cfg.xml file.
         * NOTICE: Location should be on the classpath as Hibernate uses
         * #resourceAsStream style lookup for its configuration file. That
         * is place the config file in a Java package - the default location
         * is the default Java package.<br><br>
         * Examples: <br>
         * <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml". 
         * CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code> 
         */
        private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";    /** Holds a single instance of Session */
        private static final ThreadLocal threadLocal = new ThreadLocal();    /** The single instance of hibernate configuration */
        private static final Configuration cfg = new Configuration();    /** The single instance of hibernate SessionFactory */
        private static net.sf.hibernate.SessionFactory sessionFactory;    /**
         * Returns the ThreadLocal Session instance.  Lazy initialize
         * the <code>SessionFactory</code> if needed.
         *
         *  @return Session
         *  @throws HibernateException
         */
        public static Session currentSession() throws HibernateException {
            Session session = (Session) threadLocal.get();        if (session == null) {
             System.out.println("开始创建session");
                if (sessionFactory == null) {
                    try {
                        cfg.configure(CONFIG_FILE_LOCATION);
                        sessionFactory = cfg.buildSessionFactory();
                    }
                    catch (Exception e) {
                        System.err.println("%%%% Error Creating SessionFactory %%%%");
                        e.printStackTrace();
                    }
                }
                session = sessionFactory.openSession();
                threadLocal.set(session);
            }        return session;
        }    /**
         *  Close the single hibernate session instance.
         *
         *  @throws HibernateException
         */
        public static void closeSession() throws HibernateException {
            Session session = (Session) threadLocal.get();
            threadLocal.set(null);        if (session != null) {
             System.out.println("关闭session");
                session.close();
            }
        }    /**
         * Default constructor.
         */
        private HibernateSessionFactory() {
        }}
      

  12.   

    我也是啊,怎么回事?
    还要关sessionFactory?
      

  13.   

    我跟踪了一下,发现异常发生在tran.coommit()的时候objSession.save(object);
    tran.commit();异常如下Problem with checked-in Statement, discarding.
    java.lang.NullPointerException
    at oracle.jdbc.dbaccess.DBData.clearItem(DBData.java:431)
    at oracle.jdbc.dbaccess.DBDataSetImpl.clearItem(DBDataSetImpl.java:3528)
    at oracle.jdbc.driver.OraclePreparedStatement.clearParameters(OraclePreparedStatement.java:3401)
    at com.mchange.v2.c3p0.stmt.GooGooStatementCache.refreshStatement(GooGooStatementCache.java:460)
    at com.mchange.v2.c3p0.stmt.GooGooStatementCache.checkinStatement(GooGooStatementCache.java:139)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnection$1WrapperStatementHelper.doClose(C3P0PooledConnection.java:511)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnection$2.close(C3P0PooledConnection.java:570)
    at net.sf.hibernate.impl.BatcherImpl.closePreparedStatement(BatcherImpl.java:270)
    at net.sf.hibernate.impl.BatcherImpl.closeStatement(BatcherImpl.java:140)
    at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:129)
    at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2421)
    at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2371)
    at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2240)
     
      

  14.   

    用e3p0间接池在insert和update数据库的时候就会报Problem with checked-in Statement, discarding
    不知道因为什么?
      

  15.   

    现在只能怀疑是hibernate自带的cp30有bug了,不知道应该去哪里可以下载个新的回来?
      

  16.   

    我换新的版本了,e3p0和dbcp都换高版本了,都不行。
    用tomcat的连接池就没问题,但不知道程序运行时间长以后会不会出现资源耗尽的情况。
      

  17.   

    兄弟,我也遇到相同的问题,怎么解决?我是继承HibernateDaoSuport,然后this.getSession(),最后releaseSession(),查看了下Session还是没释放呀!