简体   繁体   English

如何释放多个org.hibernate.impl.SessionFactoryImpl

[英]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.

相关问题 org.hibernate.impl.SessionFactoryImpl对象的正常大小是多少? - What would be a normal size of org.hibernate.impl.SessionFactoryImpl objects ? com.ibm.websphere.servlet.error.ServletErrorReport:java.lang.NoClassDefFoundError:org.hibernate.impl.SessionFactoryImpl - com.ibm.websphere.servlet.error.ServletErrorReport: java.lang.NoClassDefFoundError: org.hibernate.impl.SessionFactoryImpl java.lang.IllegalStateException:使用AOP的key [org.hibernate.impl.SessionFactoryImpl]没有值 - java.lang.IllegalStateException: No value for key [org.hibernate.impl.SessionFactoryImpl] with AOP Memory 由于 org.hibernate.internal.SessionFactoryImpl 难以追踪而泄漏 - Memory leak due to org.hibernate.internal.SessionFactoryImpl hard to trace 绑定到[main]的键[org.hibernate.internal.SessionFactoryImpl]没有值 - No value for key [org.hibernate.internal.SessionFactoryImpl] bound to thread [main] org.hibernate.internal.SessionFactoryImpl可能导致内存泄漏 - Possible Memory Leak due to org.hibernate.internal.SessionFactoryImpl 更正应用程序的类路径,使其包含一个兼容版本的 org.hibernate.internal.SessionFactoryImpl - Correct the classpath of your application so that it contains a single, compatible version of org.hibernate.internal.SessionFactoryImpl 执行“org.hibernate.internal.SessionFactoryImpl()”构造函数时发生“AbstractMethodError”错误 - The "AbstractMethodError" error occurred while executing the "org.hibernate.internal.SessionFactoryImpl()" constructor 升级到Hibernate 5时,SessionFactoryImpl中的AbstractMethodError - AbstractMethodError in SessionFactoryImpl when upgrading to Hibernate 5 org.hibernate.internal.SessionFactoryImpl抛出的AbstractMethodError。 <init> 从Spring Boot 1.3升级到1.4 RC1时 - AbstractMethodError thrown by org.hibernate.internal.SessionFactoryImpl.<init> when upgrading from Spring Boot 1.3 to 1.4 RC1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM