如何发布多个org.hibernate.impl.SessionFactoryImpl

时间:2011-07-19 07:27:06

标签: java hibernate memory-leaks garbage-collection

继续解决this problem,我使用MAT发现了几个'org.hibernate.impl.SessionFactoryImpl'内存泄漏:

54 instances of "org.hibernate.impl.SessionFactoryImpl", loaded by "org.apache.catalina.loader.WebappClassLoader @ 0xbb00fb0" occupy 33 962 536 (64,40%) bytes. 

Biggest instances:

org.hibernate.impl.SessionFactoryImpl @ 0x3f026c0 - 652 664 (1,24%) bytes. 
org.hibernate.impl.SessionFactoryImpl @ 0x49018f8 - 652 664 (1,24%) bytes. 
org.hibernate.impl.SessionFactoryImpl @ 0x7b0e2b8 - 652 664 (1,24%) bytes. 
org.hibernate.impl.SessionFactoryImpl @ 0x7d65e60 - 652 664 (1,24%) bytes.
...

详情: enter image description here

DaoSF.java

public final class DaoSF implements Serializable
{
  private static final long serialVersionUID = 1L;
  private static SessionFactory sessionFactory;
  private static Session hibSession;

  private synchronized static void initSessionFactory() {
    Configuration config = new Configuration();
    config.configure("hibernate.cfg.xml"); 
    sessionFactory = config.buildSessionFactory();
    hibSession = sessionFactory.getCurrentSession();
  }

  public static SessionFactory getSessionFactory() {
    initSessionFactory();
    return sessionFactory;
  }

  public static Session getSession(){ 
    return hibSession;
  }
}

DaoCrud.java的一部分:

  public void save(Object dataItem) throws Exception 
  { 
    session = DaoSF.getSessionFactory().openSession();

    Transaction tx = session.beginTransaction();  
    session.save(dataItem);  
    session.flush();
    tx.commit();  

    if (session != null) session.close();
  }

Bean.java的一部分

 public void save() {
   try {
     mydao.save(item);
   }
   catch (Exception e) {...}
   }  
 }

我做错了什么?如何正确使用会话工厂?你能帮帮我吗?

3 个答案:

答案 0 :(得分:7)

如果你可以创建一个HibernateSession类来处理打开,关闭和回滚事务会更好。

您应该将session.close()放在finally语句中,然后将null分配给会话和事务,以确保它们将被垃圾回收。

答案 1 :(得分:3)

每次需要会话时,您都在创建并初始化一个新的SessionFactory。您的getSessionFactory方法应检查您是否已有方法,而不是始终创建新方法。

答案 2 :(得分:2)

首先,您应该在finally语句中关闭连接和会话。也许有一些例外,会话没有关闭?其次,hibernate有没有关闭会话的bug。看看这个帖子:OutofMemory: SessionImpl cached even after close and flush?

相关问题