arjuna logoarjuna strap line


print this page
email this page

From an architectural point of view of JTA, the bank client is considered as an application program able to manage transactions via the javax.transaction.UserTransaction interface. The following portion of code illustrates how a JTA transaction is started and terminated when the client asks to transfer money from one account to another. This also describes what are JBossTS packages that need to be used in order to obtain appropriate objects instances (such UserTransaction).

Note: The code below is a simplified view of the BankClient.java program. Only the transfer operation is illustrated; other operations manage transactions in the same way. (see for details the BankClient.java)

package com.arjuna.demo.jta.localbank;
public class BankClient
{
   private Bank _bank;
   // This operation is used to make a transfer 
   //from an account to another account
   private void makeTransfer()
   {
     System.out.print("Take money from : ");
     String name_supplier = input();
   
     System.out.print("Put money to : ");
     String name_consumer = input();
   
     System.out.print("Transfer amount : ");
     String amount = input();
   
     float famount = 0;
     try
      {
        famount = new Float( amount ).floatValue();
      }
     catch ( java.lang.Exception ex )
      {
        System.out.println("Invalid float number, abort operation...");
        return;
      }
   
     try
      {
       //the following instruction asks a specific ArjunaTA 
       //class to obtain a UserTransaction instance
       javax.transaction.UserTransaction userTran = 
                     com.arjuna.ats.jta.UserTransaction.userTransaction();
       System.out.println("Beginning a User transaction to get balance");
       userTran.begin();
    
       Account supplier = _bank.get_account( name_supplier );
       Account consumer = _bank.get_account( name_consumer );
       supplier.debit( famount );
       consumer.credit( famount );
    
       userTran.commit( );
      }
     catch (Exception e)
      {
       System.err.println("ERROR - "+e);
      }
   }
   ......
}

The Bank object has mainly two operations: creating an account, which is added in the account list, and returning an Account object. No transactional instruction is performed by the Bank object

package com.arjuna.demo.jta.localbank;
public class Bank {
   private java.util.Hashtable _accounts;
   
   public Bank()
   {
     _accounts = new java.util.Hashtable();
   }
   
   public Account create_account( String name )
   {
     Account acc = new Account(name);
     _accounts.put( name, acc );
      return acc;
   }
   
   public Account get_account(String name)
   throws NotExistingAccount
   {
     Account acc = ( Account ) _accounts.get( name );
     if ( acc == null )
       throw new NotExistingAccount("The Account requested does not exist");
     return acc;
   }
}

The Account object provides mainly three methods balance, credit and withdraw. However, in order to provide the transactional behaviour, rather than to modify the current account directly (according to credit or withdraw) this task is delegated to an AccountResource object that is able, according to the transaction outcome, to set the account value either to its initial state or its final state.
The AccountResource object is in fact an object that implements the javax.transactions.XAResource, then able to participate to the transaction commitment. For this aim, the Account object has to register or enlist the AccountResource object as a participant after having obtaining the reference of the javax.transaction.Transaction object via the javax.transaction.TransactionManager object

package com.arjuna.demo.jta.localbank;

public class Account
{
   float _balance;
   AccountResource accRes = null;
   
   public Account(String name)
   {
     _name = name;
     _balance = 0;
   }
   
   public float balance()
   {
     return getXAResource().balance();;
   }
   
   public void credit( float value )
   {
     getXAResource().credit( value );
   }
   
   public void debit( float value )
   {
     getXAResource().debit( value );
   }
   
   public AccountResource getXAResource()
   {
   
     try
     {
       javax.transaction.TransactionManager transactionManager = 
         com.arjuna.ats.jta.TransactionManager.transactionManager();
       javax.transaction.Transaction currentTrans = 
          transactionManager.getTransaction();
   
       if (accRes == null) {
         currentTrans.enlistResource( 
            accRes = new AccountResource(this, _name) );
       }
   
       currentTrans.delistResource( accRes, XAResource.TMSUCCESS );
    
     }
     catch (Exception e)
     {
       System.err.println("ERROR - "+e);
     }
     return accRes;
   }
   ...
}

The AccountResource class that implements the javax.transaxtion.XAResource interface provides similar methods as the Account class (credit, withdraw and balance) but also all methods specified by the javax.transaxtion.XAResource. The following portion of code describes how the methods prepare, commit and rollback are implemented.

public class AccountResource implements XAResource
{
   public AccountResource(Account account, String name )
   {
     _name = name;
     _account = account;
     _initial_balance = account._balance;
     _current_balance = _initial_balance;
   }
   
   public float balance()
   {
     return _current_balance;
   }
   
   public void credit( float value )
   {
     _current_balance += value;
   }
   
   public void debit( float value )
   {
     _current_balance -= value;
   }
   
   public void commit(Xid id, boolean onePhase) throws XAException
   {
     //The value of the associated Account object is modified      
     _account._balance = _current_balance;
   }
   
   public int prepare(Xid xid) throws XAException
   {
     if ( _initial_balance == _current_balance ) //account not modified
        return (XA_RDONLY);
     if ( _current_balance < 0 )
        throw new XAException(XAException.XA_RBINTEGRITY);
        //If the integrity of the account is corrupted then vote rollback
     return (XA_OK); //return OK 
   }
   

public void rollback(Xid xid) throws XAException
   {
     //Nothing is done
   }
   
   private float _initial_balance;
   private float _current_balance;
   private Account _account;
   
   }
 }
Sample Application Source Code

Full source code for the banking application with JTA is included to provide you with a starting point for experimentation.

Copyright 2002-2005 Arjuna Technologies All Rights Reserved.
info@arjuna.com +44 191 243 0676