FileDocCategorySizeDatePackage
TxPolicy.javaAPI DocJBoss 4.2.18169Fri Jul 13 21:02:30 BST 2007org.jboss.aspects.tx

TxPolicy

public class TxPolicy extends Object
TxSupport.java encapsulates the transaction handling possibilities from the ejb spec. The Tx interceptors call the clientInvoke and serverInvoke methods on the subclass determined by the method's transaction support.

Created: Sun Feb 2 23:25:09 2003

author
David Jencks
author
Bill Burke

Fields Summary
protected static final Logger
log
public static int
MAX_RETRIES
public static Random
random
Constructors Summary
Methods Summary
public voidendTransaction(javax.transaction.TransactionManager tm, javax.transaction.Transaction tx)
The endTransaction method ends a transaction and translates any exceptions into TransactionRolledBack[Local]Exception or SystemException.

param
invocation an Invocation value
param
tm a TransactionManager value
param
tx a Transaction value
throws
javax.transaction.TransactionRolledbackException if an error occurs
throws
javax.transaction.SystemException if an error occurs

      try
      {
         if (tx != tm.getTransaction())
         {
            throw new IllegalStateException("Wrong tx on thread: expected " + tx + ", actual " + tm.getTransaction());
         }

         if (tx.getStatus() == Status.STATUS_MARKED_ROLLBACK)
         {
            tm.rollback();
         }
         else
         {
            // Commit tx
            // This will happen if
            // a) everything goes well
            // b) app. exception was thrown
            tm.commit();
         }
      }
      catch (RollbackException e)
      {
         handleEndTransactionException(e);
      }
      catch (HeuristicMixedException e)
      {
         handleEndTransactionException(e);
      }
      catch (HeuristicRollbackException e)
      {
         handleEndTransactionException(e);
      }
      catch (SystemException e)
      {
         handleEndTransactionException(e);
      }
   
public voidhandleEndTransactionException(java.lang.Exception e)

      throw new RuntimeException(e);
   
public voidhandleExceptionInOurTx(org.jboss.aop.joinpoint.Invocation invocation, java.lang.Throwable t, javax.transaction.Transaction tx)

      // if this is an ApplicationException, just rethrow it
      rethrowApplicationException(invocation, t);
      setRollbackOnly(tx);
      throw t;
   
public voidhandleInCallerTx(org.jboss.aop.joinpoint.Invocation invocation, java.lang.Throwable t, javax.transaction.Transaction tx)

      rethrowApplicationException(invocation, t);
      setRollbackOnly(tx);
      throw t;
   
public java.lang.ObjectinvokeInCallerTx(org.jboss.aop.joinpoint.Invocation invocation, javax.transaction.Transaction tx)

      try
      {
         return invocation.invokeNext();
      }
      catch (Throwable t)
      {
         handleInCallerTx(invocation, t, tx);
      }
      throw new RuntimeException("UNREACHABLE");
   
public java.lang.ObjectinvokeInNoTx(org.jboss.aop.joinpoint.Invocation invocation)

      return invocation.invokeNext();
   
public java.lang.ObjectinvokeInOurTx(org.jboss.aop.joinpoint.Invocation invocation, javax.transaction.TransactionManager tm)

      for (int i = 0; i < MAX_RETRIES; i++)
      {
         tm.begin();
         Transaction tx = tm.getTransaction();
         try
         {
            try
            {
               return invocation.invokeNext();
            }
            catch (Throwable t)
            {
               handleExceptionInOurTx(invocation, t, tx);
            }
            finally
            {
               endTransaction(tm, tx);
            }
         }
         catch (Exception ex)
         {
            ApplicationDeadlockException deadlock = ApplicationDeadlockException.isADE(ex);
            if (deadlock != null)
            {
               if (!deadlock.retryable() ||
                   i + 1 >= MAX_RETRIES)
               {
                  throw deadlock;
               }
               log.warn(deadlock.getMessage() + " retrying " + (i + 1));

               Thread.sleep(random.nextInt(1 + i), random.nextInt(1000));
            }
            else
            {
               throw ex;
            }
         }
      }
      throw new RuntimeException("UNREACHABLE");
   
public voidrethrowApplicationException(org.jboss.aop.joinpoint.Invocation inv, java.lang.Throwable e)
The rethrowApplicationException method determines if the supplied Throwable is an application exception and rethrows it if it is.

param
e a Throwable value
throws
Exception if an error occurs

      Object applicationExceptions = inv.getMetaData("transaction", "application-exceptions");
      if (applicationExceptions == null) return;
      Class[] applicationExceptionsList;
      if (applicationExceptions instanceof String)
      {
         ArrayList tmpList = new ArrayList();
         String aes = (String) applicationExceptions;
         aes = aes.trim();
         StringTokenizer tokenizer = new StringTokenizer(aes, ",");
         while (tokenizer.hasMoreTokens())
         {
            String token = tokenizer.nextToken().trim();
            Class excClass = Thread.currentThread().getContextClassLoader().loadClass(token);
            tmpList.add(excClass);
         }
         applicationExceptionsList = (Class[]) tmpList.toArray(new Class[tmpList.size()]);
      }
      else
      {
         applicationExceptionsList = (Class[]) applicationExceptions;
      }
      for (int i = 0; i < applicationExceptionsList.length; i++)
      {
         if (applicationExceptionsList[i].isInstance(e)) throw e;
      }

   
public voidsetRollbackOnly(javax.transaction.Transaction tx)
The setRollbackOnly method calls setRollbackOnly() on the invocation's transaction and logs any exceptions than may occur.

param
invocation an Invocation value

      try
      {
         tx.setRollbackOnly();
      }
      catch (SystemException ex)
      {
         log.error("SystemException while setting transaction " +
                   "for rollback only", ex);
      }
      catch (IllegalStateException ex)
      {
         log.error("IllegalStateException while setting transaction " +
                   "for rollback only", ex);
      }
   
public voidthrowMandatory(org.jboss.aop.joinpoint.Invocation invocation)


      
   
      throw new RuntimeException("Transaction is mandatory.");