Should I often call EntityManager.clear () to avoid memory leaks?

I'm new to JPA / OpenJPA, and I noticed that if I don't call EntityManager.clear() after I leave the objects, I get OutOfMemoryError (I keep adding new entities to the loop). I'm not sure if this is the expected behavior, or just, and the problem is with OpenJPA 1.2.1.

So, do I need to explicitly separate objects from myself? If I'm not, is that good practice anyway?

+8
java memory-leaks jpa openjpa entitymanager
Feb 11 2018-10-11T00
source share
3 answers

I do not have much experience with JPA. However, it will be useful -
In JPA, you must: - Create a new EntityManager for each transaction.
- Call clear () after each transaction to clear the persistence context.

+6
Feb 11 '10 at 10:01
source share

Depends on the number of objects that you enter in the read process. If you are processing large numbers (or some of them are large), then using clear () may make sense. Each time an object is read, it must be placed in the L1 cache using JPA impl.

+5
Feb 11 '10 at
source share

There seems to be something wrong with your design. Typically, an object is separated when it goes beyond the object manager. And this is one of the reasons that you cannot lazy load relationships, out of scope.

As for my experience, I rarely used em.clear (), if ever. I used the implementation of Hibernate and Toplink Essentials. No experience with OpenJPA yet.

+3
Feb 11 '10 at
source share



All Articles