我是否必须关闭()每个EntityManager? [英] Do I have to close() every EntityManager?

查看:1245
本文介绍了我是否必须关闭()每个EntityManager?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始将我自己开发的持久性框架迁移到JPA。

I have just started migrating my homegrown persistence framework to JPA.

鉴于持久性框架隐藏了大量的管道,我有兴趣知道是否不关闭EntityManagers会造成资源泄漏,或者框架会为我收集和关闭它们。

Given that the persistence frameworks hide a lot of the plumbing, I'm interested in knowing if NOT closing EntityManagers will create a resource leak, or if the frameworks will collect and close them for me.

我打算在所有地方关闭它们,但我必须这样做吗?

I intend in all places to close them, but do I HAVE to?

目前使用TopLink,只是因为它可以轻松地与NetBeans协同工作,但我很乐意调查其他JPA提供商。

At the moment using TopLink, just because it works with NetBeans easily, but am happy to investigate other JPA providers.

推荐答案

这取决于你如何获得它。

It depends how you obtained it.

如果您使用EntityManagerFactory创建它,无论您使用什么框架,都必须关闭它。

If you created it using EntityManagerFactory you will have to close it no matter what framework you use.

如果使用依赖注入获取它(例如使用EJB和@PersistenceContext注释),则不应手动关闭它(AFAIK会导致RuntimeException)。

If you obtained it using dependency injection (eg using EJB and @PersistenceContext annotation) you should not close it by hand (AFAIK it will cause RuntimeException).

这篇关于我是否必须关闭()每个EntityManager?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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