我必须关闭()每个 EntityManager 吗?

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

考虑到持久性框架隐藏了很多管道,我很想知道不关闭 EntityManager 是否会造成资源泄漏,或者框架是否会为我收集并关闭它们。

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

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

61986 次浏览

You should.

Frameworks have no idea how you intend to use the EM, so they cannot close it (except, may be, on finalization, which is not guaranteed). Yes, not closing them would create a resource leak.

The idea is the same as "always close java.sql.Connection" (despite some data sources have settings to close them automatically by inactivity) or "always close Hibernate session".

Besides, if you plan to write portable code, you shouldn't rely on specific JPA provider "being smart" -- other may fail to close the EM in time.

It depends how you obtained it.

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

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).

I have obtained EntityManager using @PersistenceContext annotation in my repository. I can see that after the connectionpools reaches its maxPoolSize it does not get cleaned up.

However if I create EntityManager using EntityManagerFactory and call entitymanager.close() then connections are getting cleaned up. I am using c3p0 as connectionpool library.

Justo to give my 5 cents you must remember to close your EntityManagerFactory. I was just using it to create my EntityManager and it opened and kept opend a new conection pool every time.