[jboss-as7-dev] JTA Synchronization.afterCompletion callback can occur in a background thread but JPA EntityManager must be accessed in single-threaded manner...

Jason Greene jason.greene at redhat.com
Fri Feb 22 16:17:43 EST 2013


On Feb 22, 2013, at 3:13 PM, Scott Marlow <smarlow at 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




More information about the jboss-as7-dev mailing list