On Feb 22, 2013, at 3:13 PM, Scott Marlow <smarlow(a)redhat.com> wrote:
I'm closing the entity manager because its a JPA spec (7.9.1) requirement for the
container to close it:
"After the JTA transaction has completed (either by transaction commit or rollback),
the container closes the entity manager calling EntityManager.close.
"
We don't have to interpret it literally. When the transaction rolls back (either via
exception or getStatus) take the exact same action that EntityManager.close would take.
If I don't close it, the GC might be able to clean it up for us but that depends on
whether there are any strong references to it. The transaction will get GCd, so the
transaction reference to the EntityManager will disappear. This has potential to create a
memory leak.
No need for GC. We *know* the outcome of the transaction, and we control the
implementation of the entity manager, therefore we can close it in the application
thread.
--
Jason T. Greene
JBoss AS Lead / EAP Platform Architect
JBoss, a division of Red Hat