Proposed enhancement to the JTA API


 

In order to support various frameworks, we have often needed the
ability to register a callback onto a Transaction in order to allow
these frameworks to track when a transaction is associated to or
dissociated from a thread. We accomplish this today using internal
APIs. After some internal discussions it was decided that this should
be brought to the spec group for discussion.

I would like to request that the JTA API be enhanced to provide a
simple thread association callback, like this:

/**
* A transaction-to-thread association listener.
*/
public interface AssociationListener {
/**
* Inform the listener that a transaction's thread association has changed.
*
* @param transaction the transaction whose association has
changed (not {@code null})
* @param associated {@code true} if the association was created,
or {@code false}
* if the association was removed
*/
void associationChanged(Transaction transaction, boolean associated);
}

The association callback is called with a value of 'true' as the last
action of a transaction resume or begin, and with a value of 'false'
as the first action of a transaction suspend, or of a commit or
rollback that happens by way of the TransactionManager interface.

Here are a few ideas for the registration portion of the API.

Option 1: Enhance javax.transaction.Transaction

public interface Transaction {
// [...]

/**
* Register a transaction association listener.
*
* @param listener the listener to register (must not be {@code null})
*/
void registerAssociationListener(AssociationListener listener);
}

Option 2/3: Enhance
javax.transaction.TransactionSynchronizationRegistry or
javax.transaction.TransactionManager

For example:

public interface TransactionSynchronizationRegistry {
// [...]

/**
* Register a transaction association listener on to a transaction.
*
* @param transaction the transaction to register to (must not be
{@code null})
* @param listener the listener to register (must not be {@code null})
*/
void registerAssociationListener(Transaction transaction,
AssociationListener listener);
}

Any thoughts on the feasibility of this enhancement for JTA 1.3?
--
- DML

Join jta-spec@javaee.groups.io to automatically receive all group messages.