Once in a while a problem pops up with how a middleware stack supports XA. The problem I'm discussing here is the one of how to exactly enlist and delist
isSameRM()returning true. The XA spec is vague on this very topic, so this warrants some exploration.
First of all, what do I mean with "enlist and delist
Enlisting and delisting
When using an XA capable system, the connection needs to be enlisted in the transaction before it is used. A typical sequence on the XAResource, let's call it r1, is:
With a second resource in the mix, the call sequence may become:
The transaction is now escalated to a full two-phase transaction. But if r1 and r2 represent the same resource manager, a shortcut can be taken: one resource can "piggy back ride" on the other resource. The isSameRM() method can be used to check if this should be attempted. If it returns true, the sequence may become:
The transaction now again is a single-phase transaction, and the performance difference with the two-phase commit case is usually very significant.
This sequence doesn't work for a number of popular systems, e.g. MQSeries and Oracle. Unfortunately the XA specification, but that specification is not quite clear at all about what resource managers should be able to do, so there is to some extent some trial and error involved.
Trial and error
How should isSameRM() be used then? By testing a number of different systems, it turns out that for some systems, there can be only one enlisted XAResource active in the transaction. If a second XAResource joins the transaction, the first one should be deactivated. Here is an example:
|isSameRM()||Test: multiple active||Test: single active 1||Test: single active 2|
|JMQ||as of 4.4||yes||no: throws on line 7||yes|
|WebSphereMQ 6||yes||no: blocks on line 4||no: blocks on line 7||yes|
|Derby 10.5.3.0||yes||no: blocks on line 4||yes||yes|
|Oracle 184.108.40.206||yes||no: blocks on line 4||yes||yes|
|MySQL 5.1||yes||no: throws on line 4||no: throws on line 5||no: throws on line 7|
As can be seen, the SINGLE ACTIVE 2 code sequence works best.
As a side note, MySQL is showing surprising behavior: TMJOIN and TMSUSPEND are not supported (as documented in the MySQL documentation), so why does it bother to return true on isSameRM()? Behavior like this makes it difficult to write portable code: a container now has to provide a wrapper around the DataSource that corrects for this. It would have been much better if it simply had returned false on isSameRM().
How does this relate to what application code can do in for instance a Java EE container? That's the topic of a different blog post.