使用hibernate的时候,程序里面,凡是HibernateSessionFactory.getSession();的地方,我最后都会HibernateSessionFactory.closeSession();
但是,跟踪也看到这个session的closed是true,实际上仍保持者和数据库的连接,从数据库Oracle的会话管理那里可以看到,这个session连接的状态为inative。
为什么会这样呢?在使用hibernate上有什么需要特别注意的地方才能够保证session的及时关闭呢?   
目前项目运作时,总是不能及时释放连接,严重影响了系统运行,请高手指点~~谢谢我也试过session.close(), 每次连接数据库,都会生成一个inactive的session. 服务器越来越慢!!!急需解决这个问题!!!

解决方案 »

  1.   

    HibernateSessionFactory 不知道你怎么写的!
      

  2.   

    HibernateSessionFactory有没有问题?
    是否有些操作忘记了close另外,使用连接池的时候,数据库的连接并不会真正的释放,只是返回到空闲池中,查oracle的session确实是inactive,
    但是也不会出现每次查询都要新增一个连接的情况,如果之前的连接池里有连接的话,就会利用原来存在的,而你说的情况好像是没有正确的close session导致了连接没有返回到连接池
      

  3.   

    HibernateSessionFactory 是hibernate自动生成的.import org.hibernate.HibernateException;
    import org.hibernate.Session;
    import org.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.
         * Location should be on the classpath as Hibernate uses  
         * #resourceAsStream style lookup for its configuration file. 
         * The default classpath location of the hibernate config file is 
         * in the default package. Use #setConfigFile() to update 
         * the location of the configuration file for the current session.   
         */
        private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
    private static final ThreadLocal threadLocal = new ThreadLocal();
        private  static Configuration configuration = new Configuration();    
        private static org.hibernate.SessionFactory sessionFactory;
        private static String configFile = CONFIG_FILE_LOCATION; static {
         try {
    configuration.configure(configFile);
    sessionFactory = configuration.buildSessionFactory();
    } catch (Exception e) {
    System.err.println("%%%% Error Creating SessionFactory %%%%");
    e.printStackTrace();
    }
        }
        private HibernateSessionFactory() {
        }

    /**
         * Returns the ThreadLocal Session instance.  Lazy initialize
         * the <code>SessionFactory</code> if needed.
         *
         *  @return Session
         *  @throws HibernateException
         */
        public static Session getSession() throws HibernateException {
            Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen()) {
    if (sessionFactory == null) {
    rebuildSessionFactory();
    }
    session = (sessionFactory != null) ? sessionFactory.openSession()
    : null;
    threadLocal.set(session);
    }        return session;
        } /**
         *  Rebuild hibernate session factory
         *
         */
    public static void rebuildSessionFactory() {
    try {
    configuration.configure(configFile);
    sessionFactory = configuration.buildSessionFactory();
    } catch (Exception e) {
    System.err
    .println("%%%% Error Creating SessionFactory %%%%");
    e.printStackTrace();
    }
    } /**
         *  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) {
                session.close();
            }
        } /**
         *  return session factory
         *
         */
    public static org.hibernate.SessionFactory getSessionFactory() {
    return sessionFactory;
    } /**
         *  return session factory
         *
         * session factory will be rebuilded in the next call
         */
    public static void setConfigFile(String configFile) {
    HibernateSessionFactory.configFile = configFile;
    sessionFactory = null;
    } /**
         *  return hibernate configuration
         *
         */
    public static Configuration getConfiguration() {
    return configuration;
    }}
      

  4.   

    我已经检查了所有的操作,最后都带上了HibernateSessionFactory.closeSession();
    每次连接都会产生一个inactive session, 已经确认过.你说的情况好像是没有正确的close session导致了连接没有返回到连接池-----------如何真正返回到连接池?可否通过什么方式查看是否返回连接池?
      

  5.   

    hibernate.cfg.xml 你要发布一下!!估计你配置连接池有问题!里面有一个连接失效时间。最大连接最小连接数。出了问题你应该找DBA,让他把数据库驻留连接开启。
      

  6.   

    HibernateSessionFactory有没有问题? 
    是否有些操作忘记了close ?
    HibernateSessionFactory操作起来正常.没有忘记任何HibernateSessionFactory.closeSession()操作.
    我是每次提交,每次都查看oracle里面的session. 即使使用session.close()也是同样每次产生INACTIVE SESSION.
      

  7.   

    hibernate.cfg.xml :<?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
              "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
              "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools.                   -->
    <hibernate-configuration>
    <session-factory>
    <property name="show_sql">true</property>
    <property name="myeclipse.connection.profile">Ca3_Pisar</property>
    <property name="connection.url">jdbc:oracle:thin:@100.110.101.212:1521:prswddew
    </property>
    <property name="connection.username">calparis</property>
    <property name="connection.password">calparis</property>
    <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
    <mapping resource="com/seagate/calib/bean/hbm/SrResource.hbm.xml" />
    <mapping resource="com/seagate/calib/bean/hbm/RStatus.hbm.xml" />
    <mapping resource="com/seagate/calib/bean/hbm/SrResLocation.hbm.xml" />
    <mapping resource="com/seagate/calib/bean/hbm/SrResGroup.hbm.xml" />
    </session-factory>
    </hibernate-configuration>
      

  8.   

    Session session=HibernateSessionFactory.getSession());
    session.close(); 
      

  9.   

    连接池的配置在哪里进行? 程序里, Weblogic server console上面,还是oracle 里面? 如何配置?兄弟帮我!
      

  10.   

    每次我打开一个session,后我都调用了session.close(),还是查到INACTIVE SESSION。是怎么回事???
      

  11.   

    把sessionfactory close 掉就没有inactive session.感觉这个太不可能啊.是不是这样会没有好的效率.
      

  12.   


    从配置文件上看,你还没有配置连接池。使用的是Hibernate默认连接池。找个免费产品干吧!其次数据CUD要关闭事务,及时提交才能保证oracle inactive session及时清理。inactive session--管理是DBA的事情,那个配置就费神了。虽然处于不活动状态但是也占CPU呀!
      

  13.   

    <session-factory>
    <property name="connection.driver_class">
    oracle.jdbc.driver.OracleDriver
    </property>
    <property name="connection.url">
    jdbc:oracle:thin:@localhost:1521:orcl
    </property>
    <property name="connection.username">ckts_hn</property>
    <property name="connection.password">ckts_hn</property>
    <!-- C3P0连接池设定-->
    <property name="hibernate.c3p0.max_size">20</property>
    <property name="hibernate.c3p0.min_size">2</property>
    <property name="hibernate.c3p0.timeout">60</property>
    <property name="hibernate.c3p0.max_statements">100</property>
    <property name="hibernate.c3p0.idle_test_period">60</property>
    <property name="hibernate.c3p0.acquire_increment">2</property> <property name="dialect">
    org.hibernate.dialect.OracleDialect
    </property>
    <property name="show_sql">true</property>
    。。
      

  14.   

    不是每次执行一个方法就产生一个,确定的说每次HibernateSessionFactory.getSession();之后,数据库那边就会产生一个.
      

  15.   

    public static void closeSession() throws HibernateException { 
            Session session = (Session) threadLocal.get(); 
            threadLocal.set(null);         if (session != null) { 
                session.close(); 
            } 
        } 
      

  16.   

    每次都执行sessionfactory.close()之后,就没有session出现在oracle了.
      

  17.   

    设置了C3P0连接池之后,是不是session到了timeout时间就自动消失? 是不是不用再HibernateSessionFactory.closeSession();?
      

  18.   

    用了连接池之后,session更多,而且超过了最大设置的!为什么时间到了还不消失?
      

  19.   

    连接池为什么不独立出来,为何要把它和SessionFactory放在一起,你试试把连接池独立出来
      

  20.   

    finally里面session.close(); 保证一定执行了session.close()
      

  21.   

    执行了session.close()有时会报错的.