gpt4 book ai didi

java - CreateEntityManagerFactory 的规模正在不断增长。是不是内存泄漏了?

转载 作者:行者123 更新时间:2023-12-01 16:01:46 25 4
gpt4 key购买 nike

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

当从异步 EJB 调用中调用此类时,是否会泄漏内存?

所以我想到将 EntityManager 和 EntityManagerFactory 设为静态类成员。这能解决问题吗?

这里需要一些帮助。当我运行 JProfiler 时。它说这个区域是一个热点。尤其是 createEntityManagerFactory。

感谢任何修复此泄漏的帮助。

最佳答案

我认为您没有关闭 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());
}

您不应该将实体经理保留在某个字段中。这是一种“使用一次然后扔掉”的物体。不过,您可以保留 EMF 引用。

您多久创建一次 SoapMessageProcessor 实例?

您是否使用任何类型的依赖注入(inject)框架?这将使您的生活变得更加简单。

关于java - CreateEntityManagerFactory 的规模正在不断增长。是不是内存泄漏了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3681122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com