简体   繁体   English

CreateEntityManagerFactory的大小正在增长。 内存泄漏了吗?

[英]CreateEntityManagerFactory is growing in size . Is it leaking memory?

public class SoapMessageProcessor {

private EntityManager em;
private static final Logger logger = Logger.getLogger(SoapMessageProcessor.class);

public SoapMessageProcessor() {
    try {
        EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("Auditing");
        em = emFactory.createEntityManager();
    } catch (Exception ex) {
        throw new RuntimeException(ex.getMessage());
    }
}

Will this leak memory, when this class is being invoked from a asynchronous EJB call? 从异步EJB调用中调用此类时,该泄漏内存是否会发生?

So I thought of making the EntityManager and EntityManagerFactory static class members. 所以我想到了使EntityManager和EntityManagerFactory成为静态类成员。 Will that solve the issue? 这样可以解决问题吗?

Some help needed here. 这里需要一些帮助。 When I ran JProfiler. 当我运行JProfiler时。 It says that this area is a Hot spot.Especially the createEntityManagerFactory. 它表示该区域是一个热点。特别是createEntityManagerFactory。

Any help in fixing this leak is appreciated. 感谢您提供任何解决此泄漏的帮助。

I don't think you close either the EMF or the EM. 我认为您不会关闭EMF或EM。

try {
        EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("Auditing");
        em = emFactory.createEntityManager();
        // do whatever you need to do
        em.close();
        emFactory.close();
} catch (Exception ex) {
        throw new RuntimeException(ex.getMessage());
}

You shouldn't keep an entity manager in a field. 您不应该在字段中保留实体经理。 It's kind of a "use once and then throw away" kind of object. 这是一种“使用一次然后丢弃”的对象。 You can, however, keep an EMF reference. 但是,您可以保留EMF参考。

How often are you creating SoapMessageProcessor instances? 您多久创建一次SoapMessageProcessor实例?

Are you using any kind of dependency injection framework? 您是否在使用任何种类的依赖注入框架? It'll make your life much simpler. 它会让您的生活变得更加简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM