On 3/6/2013 2:16 PM, Scott Marlow wrote:
 requires that at Transaction completion, the container closes (or
returns to cache), the transaction-scoped
persistence context. What is supposed to happen when the JTA
transaction completes in a different thread than the
application thread? For example, if a background thread calls the
Synchronization.afterCompletion() because the tx
timeout period has been exceeded (as some Transaction Managers may
do), its not exactly thread-safe to call
EntityManager.close() (see ). Specifically, the application could
be in the middle of a persist or some other
EntityManager method, when EntityManager.close() is called.
The team here tells me that this should not be happening, and that the
transaction managers they are
familiar with will just mark the transaction for rollback rather than
rolling it back at the point
Nevertheless, if the container were working with a TM where a timeout
did result in immediate
rollback and invocation of afterCompletion, the container should note
this, and at the point at
which the transaction would normally be completed then do the actual
close as it normally would.
What do your transaction manager and container do?
Related to the above, if a JTA transaction rollback occurs in a
background thread , how are the managed entities
expected to be detached without violating the EntityManager
There may be vendor specific solutions but shouldn't we (JPA spec eg)
account for the interaction of thread-unsafe
persistence contexts and the JTA Synchronization.afterCompletion that
may be invoked in non-application (background)
 7.9.1 Container Responsibilities - After the JTA transaction has
completed (either by transaction commit or
rollback), the container closes the entity manager calling
 7.2 Obtaining an EntityManager - An entity manager must not be
shared among multiple concurrently executing threads,
as the entity manager and persistence context are not required to be
threadsafe. Entity managers must only be accessed
in a single-threaded manner.
 7.9.2 Provider Responsibilities - When the JTA transaction rolls
back, the provider must detach all managed entities
if the persistence context is of type SynchronizationType.SYNCHRONIZED
or has otherwise been joined to the transaction.
[jsr338-experts] Re: transaction-scoped persistence context being closed at JTA transaction completion time from non-application thread ...