8.1.4 Single Session Across Multiple Transactions

Because modifications in the transient layer are not transactionalized, the possibility exists for some implementations to allow a Session to be shared across transactions. This possibility arises because in JTA, an XAResource may be successively associated with different global transactions and in many implementations the natural mapping will be to make the Session implement the XAResource. The following code snippet illustrates how an XAResource may be shared across two global transactions:

// Associate the resource (our Session) with a global
// transaction xid1
res.start(xid1, TMNOFLAGS);

// Do something with res, on behalf of xid1
// ...


// Suspend work on this transaction
res.end(xid1, TMSUSPEND);

// Associate (the same!) resource with another
// global transaction xid2
res.start(xid2, TMNOFLAGS);

// Do something with res, on behalf of xid2
// ...

// End work
res.end(xid2, TMSUCCESS);

// Resume work with former transaction
res.start(xid1, TMRESUME);

// Commit work recorded when associated with xid2
res.commit(xid2, true);


In cases where the XAResource corresponds to a Session (that is, probably most implementations), items that have been obtained in the context of xid1 would still be valid when the Session is effectively associated with xid2. In other words, all transactions working on the same Session would share the transient items obtained through that Session.

In those implementations that adopt a copy-on-read approach to transient storage (see 7.1.3.4 Seeing Changes Made by Other Sessions) this will mean that the transient layer reflects an unchanged item's state in the transaction
context in which the item was acquired
. As soon as an item is refreshed or changed, the session will then reflect the state of that item in the transaction context within which that refresh or change took place.

Some implementers may choose to circumvent any problems with shared transient items by undoing changes inside the transient layer when a session is disassociated from a global transaction. This is however, outside the scope of this specification.