3

Continue solving this problem, i've found couple of 'org.hibernate.impl.SessionFactoryImpl' memory leaks using MAT:

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.
...

Detail: 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;
  }
}

part of 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();
  }

part of Bean.java

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

What I'm doing wrong? How to correctly use session factory? Could you help me?

Community
  • 1
  • 1
gaffcz
  • 3,469
  • 14
  • 68
  • 108

3 Answers3

7

It will be better if you can create a Class HibernateSession which will handle open, close, and rollback transaction.

You should put session.close() in finally statement and then assign null to session and transaction just to make sure that they will be garbage collected.

  • Thank you, Mi Mee, your advise led me to right way, I've reduced DaoSF and rewrited DaoCrud and problem is solved. So i accept your answer. Thanks :) – gaffcz Jul 19 '11 at 11:09
3

You're creating and initializing a new SessionFactory every time you need a session. Your getSessionFactory method should check if you already have one instead of always creating a new one.

Thomas
  • 87,414
  • 12
  • 119
  • 157
2

First of all you should close connection and session in finally statement. Maybe there are some exceptions and session is not closed? Second, hibernate had bugs connected with no closing sessions. Look at this thread: OutofMemory: SessionImpl cached even after close and flush?.

zacheusz
  • 8,750
  • 3
  • 36
  • 60
  • Thanks, zacheusz, but sessions seems to be closed and each access to database keeps one sessionFactory alive..:/ – gaffcz Jul 19 '11 at 07:51
  • so look at my link to thread at Hibernate forum, and as I said in your another question IMHO it may be not a real memmory leak – zacheusz Jul 19 '11 at 08:09
  • 1
    Finally, it wasn't hibernate problem but only of mine. It was my misunderstanding of session factory, i've reduced DaoSF and rewrite DaoCrud and it works properly now... In any case, thank you :) – gaffcz Jul 19 '11 at 11:32
  • So it wasn't memmory leak, but extensive memmory usage :) – zacheusz Jul 19 '11 at 11:34