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

Mark Little mlittle at redhat.com
Sat Mar 9 05:08:57 EST 2013


On 7 Mar 2013, at 17:00, Scott Marlow <smarlow at redhat.com> wrote:

> I think that the x/open document is pretty clear about "thread of 
> control", in that only the application thread should end the tx.  The 
> JTA specification "thread of control" allows any thread to end the 
> transaction.



It's really a much better idea to read the specifications. Here's what XA says for xa_switch_t xa_commit and xa_rollback operations (XAResource equivalents):

"A transaction manager calls xa_commit() to commit the work associated with ∗xid. Any changes made to resources held during the transaction branch are made permanent. A transaction manager may call this function from any thread of control. All associations for ∗xid must have been ended by using xa_end() with TMSUCCESS set in flags.

A transaction manager calls xa_rollback () to roll back the work performed at a resource manager on behalf of the transaction branch. A branch must be capable of being rolled back until it has successfully committed. Any resources held by the resource manager for the branch are released and those modified are restored to their values at the start of the branch. A transaction manager may call this function from any thread of control."


Let me pull out the specific line:

"A transaction manager may call this function from any thread of control."
 

Now let's look at what XA actually says about threads of control:

"A thread of control (or a thread) is the entity, with all its context, that is currently in control of a processor. A thread of control is an operating-system process: an address space and single thread of control that executes within that address space, and its required system resources. The context may include the process’ locks on shared resources, and the files the process has open. For portability reasons, the notion of thread of control must be common among the AP, TM and RM.

The thread concept is central to the TM’s coordination of RMs. APs call RMs to request work, while TMs call RMs to delineate transaction branches. The way the RM knows that a given work request pertains to a given branch is that the AP and the TM both call it from the same thread of control. For example, an AP thread calls the TM to declare the start of a global transaction. The TM records this fact and informs RMs. After the AP regains control, it uses the native interface of one or more RMs to do work. The RM receives the calls from the AP and TM in the same thread of control.

Certain XA routines, therefore, must be called from a particular thread."

The last line is important, for obvious reasons. If we ignore the async xa methods and xa_open/xa_close, which aren't supported by JTA anyway, what this means is that a thread that calls xa_start must also call xa_end to terminate the association.

BTW it's a very slippery slope to refer to the XA specification when discussing synchronizations, since the first standard in which they were supported was the OTS from the OMG. XA does not support synchronizations as they were never retrofitted.

Mark.

---
Mark Little
mlittle at redhat.com

JBoss, by Red Hat
Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom.
Registered in UK and Wales under Company Registration No. 3798903 Directors: Michael Cunningham (USA), Charlie Peters (USA), Matt Parsons (USA) and Brendan Lane (Ireland).




-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-as7-dev/attachments/20130309/e29c4bed/attachment.html 


More information about the jboss-as7-dev mailing list