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

天大地大妈咪最大 提交于 2019-12-01 03:57:01

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.

Depends how many objects you bring into the persistence process (read). If you handle large numbers (or some of the objects are large) then use of clear() can make sense. Each time an object is read it should be put in the L1 cache by the JPA impl.

It sounds like there is something wrong somewhere, in your design. Usually, the entity gets detached once it is outside the scope of entity manager. And thats one of the reason you can't lazy load relations, outside the scope.

As far as my experience is concerned, I seldom used em.clear(), if ever. I used Hibernate implementation, and Toplink Essentials. No experience with OpenJPA, yet.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!