[英]How to release multiple org.hibernate.impl.SessionFactoryImpl
Continue solving this problem , i've found couple of 'org.hibernate.impl.SessionFactoryImpl' memory leaks using MAT :继续解决这个问题,我发现几个 'org.hibernate.impl.SessionFactoryImpl' memory 使用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:细节:
DaoSF.java道SF.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: 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部分 Bean.java
public void save() {
try {
mydao.save(item);
}
catch (Exception e) {...}
}
}
What I'm doing wrong?我做错了什么? How to correctly use session factory?如何正确使用session厂? Could you help me?你可以帮帮我吗?
It will be better if you can create a Class HibernateSession which will handle open, close, and rollback transaction.如果您可以创建一个 Class HibernateSession 来处理打开、关闭和回滚事务,那就更好了。
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.您应该将 session.close() 放在 finally 语句中,然后将 null 分配给 session 和事务,以确保它们将被垃圾收集。
You're creating and initializing a new SessionFactory every time you need a session.每次需要 session 时,您都在创建和初始化一个新的 SessionFactory。 Your getSessionFactory
method should check if you already have one instead of always creating a new one.您的getSessionFactory
方法应该检查您是否已经拥有一个,而不是总是创建一个新的。
First of all you should close connection and session in finally statement.首先,您应该在 finally 语句中关闭连接和 session。 Maybe there are some exceptions and session is not closed?也许有一些例外,session 没有关闭? Second, hibernate had bugs connected with no closing sessions.其次,hibernate 存在与没有关闭会话相关的错误。 Look at this thread: OutofMemory: SessionImpl cached even after close and flush?看看这个线程: OutofMemory: SessionImpl 缓存即使在关闭和刷新之后? . .
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.