Example JTA Transactional Resource Implementation

I am trying to understand the basic principles of creating a remote / network resource compatible with JTA, and I am amazed at how little documentation / blogs / articles there are on this subject.

Let's say I wrote my own special server, the server "IAmYourFaja" or " IAYF ". And let me say that I wrote / implemented my own TCP protocol for interacting with this server called IAYFCP (IAYF Comms Protocol). Finally, I wrote a Java client library to access and send messages to a remote IAYF server through IAYFCP. Still with me?

Now I have a use case when I need to execute the following distributed transaction:

  • Insert a record into a relational / JDBC database; then
  • Leave a message on my IAYF server; then
  • Click Message to JMS Browser

I need all of them to be executed so that if any one component fails at any time, I can drop them all back and not have any changed state in these network resources.

The ultimate goal would be to run the following code (pseudocode here):

// "JTA Example"
DistributedTransaction dTrans = getTransaction();
DataSource jdbcDataSource = getDataSource();
IayfClient iayfClient = getIayfClient();
JmsClient jmsClient = getJmsClient();
try {
    dTrans.begin();

    // 1. Insert a record in a relational/JDBC database
    insertRecord(jdbcDataSource, "INSERT INTO widgets ( fizz, buzz ) VALUES ( 35, true )");

    // 2. Fire a message to my IAYF server
    iayfClient.fireMessage(IayfMessages.LukeIamYourFaja, 12);

    // 3. Push a message to a JMS broker
    jmsClient.publishMessage("Noooooooooo! (then jumps off ledge and Vader goes off to the bar)");

    // If we get here then all 3 networked resources are ready/capable of committing, so do it, do it now!
    dTrans.commit();
} catch(Throwable t) {
    // Something went wrong, roll back all 3.
    dTrans.rollback();
}

So, the JDBC driver and the JMS library that I use are already compatible with JTA. This means that in order to make this code possible, I need to make my IAYF client library also compatible with JTA. The problem is that I don’t understand which JTA interfaces I need to implement:

So a few questions:

  • What I need to implement an interface (and why) XAResource, UserTransactionor both?
  • , , JTA IAYF / ? - , , "JTA Example" , ?
  • . Java EE , , , - Java EE/JTA- , - Bitronix Atomikos, ?
+4
1

UserTransaction - , ( ) . DistributedTransaction UserTransaction. DistributedTransaction - .

, XAResource - . , , . , . WebLogic: XAR-

, XAResource IAYFCP IAYF. , IAYFCP .

3 JBossTS XAResource.

, Bitronix, Atomikos JBossTS JTA , JTA. , XAResource, XAResource "Hello, world" , , start/prepare/commit/rollback.

+4

All Articles