我应该经常调用EntityManager.clear()来避免内存泄漏吗? [英] Am I supposed to call EntityManager.clear() often to avoid memory leaks?

查看:1856
本文介绍了我应该经常调用EntityManager.clear()来避免内存泄漏吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JPA / OpenJPA的新手,我注意到如果我在持久化实体后没有调用 EntityManager.clear(),我会得到 OutOfMemoryError (我继续在循环中添加新实体)。我不确定这是否是预期的行为,或者只是OpenJPA 1.2.1故障。

I'm new to JPA/OpenJPA and I noticed that if I don't call EntityManager.clear() after i persist entities I get an OutOfMemoryError (I keep adding new entities in a loop). I'm not sure if this is the expected behavior or it's just and OpenJPA 1.2.1 glitch.

那么,我是否需要自己明确分离实体?
如果我不是,那么这是一个很好的做法吗?

So, am I required to explicitly detach the entities myself? If I'm not, it's a good practice anyway?

推荐答案

我对JPA没有多少经验。但这很有用 -

在JPA中你必须要么:

- 为每个事务创建一个新的EntityManager。

- 每次调用后调用clear()事务清除持久化上下文。

I don't have much experience with JPA. However this'll be useful -
In JPA you must either:
- Create a new EntityManager for each transaction.
- Call clear() after each transaction to clear the persistence context.

这篇关于我应该经常调用EntityManager.clear()来避免内存泄漏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆