Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags.
   * See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   * This copyrighted material is made available to anyone wishing to use,
   * modify, copy, or redistribute it subject to the terms and conditions
   * of the GNU Lesser General Public License, v. 2.1.
  * This program is distributed in the hope that it will be useful, but WITHOUT A
  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
  * You should have received a copy of the GNU Lesser General Public License,
  * v.2.1 along with this distribution; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  * MA  02110-1301, USA.
  *
  * (C) 2005-2006,
  * @author JBoss Inc.
  */
 /*
  * Copyright (C) 2005,
  *
  * Arjuna Technologies Ltd,
  * Newcastle upon Tyne,
  * Tyne and Wear,
  * UK.
  *
  * $Id: XATerminatorImple.java 2342 2006-03-30 13:06:17Z  $
  */
 
 package com.arjuna.ats.internal.jta.transaction.arjunacore.jca;
 
 
 import  javax.transaction.HeuristicCommitException;
 import  javax.transaction.HeuristicMixedException;
 import  javax.transaction.HeuristicRollbackException;
 import  javax.transaction.RollbackException;
 import  javax.transaction.SystemException;
 
The XATerminator implementation.

Author(s):
mcl
 
 
 public class XATerminatorImple implements javax.resource.spi.XATerminator, XATerminatorExtensions
 {
    
Commit the transaction identified and hence any inflow-associated work.

Parameters:
xid the transaction to commit
onePhase whether or not this is a one-phase commit (should only be true if there is a single resource associated with the transaction).
Throws:
XAException thrown if there are any errors, including if the transaction cannot commit and has to roll back.
 
 
     public void commit (Xid xidboolean onePhasethrows XAException
     {
         try
         {
             SubordinateTransaction tx = SubordinationManager
                     .getTransactionImporter().getImportedTransaction(xid);
 
             if (tx == null)
                 throw new XAException(.);
 
             if (tx.activated())
             {
                 if (onePhase)
                     tx.doOnePhaseCommit();
                 else
                     tx.doCommit();
                SubordinationManager.getTransactionImporter()
                        .removeImportedTransaction(xid);
            }
            else
                throw new XAException(.);
        }
        catch (RollbackException e)
        {
            SubordinationManager.getTransactionImporter()
                    .removeImportedTransaction(xid);
            XAException xaException = new XAException(.);
            xaException.initCause(e);
            throw xaException;
        }
        catch (XAException ex)
        {
            // resource hasn't had a chance to recover yet
            if (ex.errorCode != .)
            {
                SubordinationManager.getTransactionImporter()
                        .removeImportedTransaction(xid);
            }
            throw ex;
        }
        catch (HeuristicRollbackException ex)
        {
            XAException xaException = new XAException(.);
            xaException.initCause(ex);
            throw xaException;
        }
        catch (HeuristicMixedException ex)
        {
            XAException xaException = new XAException(.);
            xaException.initCause(ex);
            throw xaException;
        }
        catch (final HeuristicCommitException ex)
        {
            XAException xaException = new XAException(.);
            xaException.initCause(ex);
            throw xaException;
        }
        catch (final IllegalStateException ex)
        {
            SubordinationManager.getTransactionImporter()
                    .removeImportedTransaction(xid);
            XAException xaException = new XAException(.);
            xaException.initCause(ex);
            throw xaException;
        }
        catch (SystemException ex)
        {
            SubordinationManager.getTransactionImporter()
                    .removeImportedTransaction(xid);
            XAException xaException = new XAException(.);
            xaException.initCause(ex);
            throw xaException;
        }
    }

    
If the transaction subordinate generated a heuristic, then this operation will be called later once that heuristic has been resolved.

Parameters:
xid the transaction.
Throws:
XAException if any error happens.
    public void forget (Xid xidthrows XAException
    {
        try
        {
            SubordinateTransaction tx = SubordinationManager
                    .getTransactionImporter().getImportedTransaction(xid);
            if (tx == null)
                throw new XAException(.);
            tx.doForget();
        }
        catch (Exception ex)
        {
            XAException xaException = new XAException(.);
            xaException.initCause(ex);
            throw xaException;
        }
        finally
        {
            SubordinationManager.getTransactionImporter()
                    .removeImportedTransaction(xid);
        }
    }

    
Prepare the imported transaction.

Parameters:
xid the transaction to prepare.
Returns:
either XAResource.XA_OK if the transaction prepared, or XAResource.XA_RDONLY if it was a read-only transaction (and in which case, a second phase message is not expected/required.)
Throws:
XAException thrown if any error occurs, including if the transaction has rolled back.
    public int prepare (Xid xidthrows XAException
    {
    	// JBTM-927 this can happen if the transaction has been rolled back by the TransactionReaper
		try {
			tx = SubordinationManager.getTransactionImporter().getImportedTransaction(xid);
catch (XAException xae) {
			if (xae.errorCode == .) {
			}
			throw xae;
		}
        try
        {
            if (tx == null)
                throw new XAException(.);
            switch (tx.doPrepare())
            {
            case .:
                SubordinationManager.getTransactionImporter()
                        .removeImportedTransaction(xid);
                return .;
            case .:
                // the JCA API spec limits what we can do in terms of reporting
                // problems.
                // try to use the exception code and cause to provide info
                // whilst
                // remaining API compliant. JBTM-427.
                Exception initCause = null;
                int xaExceptionCode = .;
                try
                {
                    tx.doRollback();
                }
                catch (HeuristicCommitException e)
                {
                    initCause = e;
                    xaExceptionCode = .;
                }
                catch (HeuristicMixedException e)
                {
                    initCause = e;
                    xaExceptionCode = .;
                }
                catch (SystemException e)
                {
                    initCause = e;
                    xaExceptionCode = .;
                }
                catch (final HeuristicRollbackException e)
                {
                    initCause = e;
                    xaExceptionCode = .;
                }
                SubordinationManager.getTransactionImporter()
                        .removeImportedTransaction(xid);
                XAException xaException = new XAException(xaExceptionCode);
                if (initCause != null)
                {
                    xaException.initCause(initCause);
                }
                throw xaException;
            case .:
                return .;
            case .:
                throw new XAException(.);
            default:
                throw new XAException(.);
            }
        }
        catch (XAException ex)
        {
            throw ex;
        }
    }

    
Return a list of indoubt transactions. This may include those transactions that are currently in-flight and running 2PC and do not need recovery invoked on them.

Parameters:
flag either XAResource.TMSTARTRSCAN to indicate the start of a recovery scan, or XAResource.TMENDRSCAN to indicate the end of the recovery scan.
Returns:
a list of potentially indoubt transactions or null.
Throws:
XAException thrown if any error occurs.
    public Xid[] recover (int flagthrows XAException
    {
        /*
         * Requires going through the objectstore for the states of imported
         * transactions. Our own crash recovery takes care of transactions
         * imported via CORBA, Web Services etc.
         */
        switch (flag)
        {
        case .// check the object store
            if ()
                throw new XAException(.);
            else
                 = true;
            break;
        case .// null op for us
            if ()
                 = false;
            else
                throw new XAException(.);
            return null;
        case .:
            if ()
                break;
        default:
            throw new XAException(.);
        }
        // if we are here, then check the object store
        return doRecover(nullnull);
    }
    
    
Return a list of indoubt transactions. This may include those transactions that are currently in-flight and running 2PC and do not need recovery invoked on them.

Parameters:
nodeName Only recover transactions for this node (unless set to NodeNameXAResourceOrphanFilter.RECOVER_ALL_NODES)
Returns:
a list of potentially indoubt transactions or null.
Throws:
XAException thrown if any error occurs.
    public Xid[] doRecover (Xid xidString parentNodeNamethrows XAException
    {
        /*
         * Requires going through the objectstore for the states of imported
         * transactions. Our own crash recovery takes care of transactions
         * imported via CORBA, Web Services etc.
         */
        Xid[] indoubt = null;
        try
        {
            RecoveryStore recoveryStore = StoreManager.getRecoveryStore();
            InputObjectState states = new InputObjectState();
            // only look in the JCA section of the object store
            if (recoveryStore.allObjUids(SubordinateAtomicAction.getType(), states)
                    && (states.notempty()))
            {
                Stack<Xidvalues = new Stack<Xid>();
                boolean finished = false;
                do
                {
                    Uid uid = null;
                    try
                    {
                        uid = UidHelper.unpackFrom(states);
                    }
                    catch (IOException ex)
                    {
                        ex.printStackTrace();
                        finished = true;
                    }
                    if (uid.notEquals(Uid.nullUid()))
                    {
						if (parentNodeName != null) {
							XidImple loadedXid = (XidImplesaa.getXid();
							if (loadedXid.getFormatId() == .) {
								String loadedXidSubordinateNodeName = XATxConverter.getSubordinateNodeName(loadedXid.getXID());
								if (TxControl.getXANodeName().equals(loadedXidSubordinateNodeName)) {
									if (parentNodeName.equals(saa.getParentNodeName())) {
										if (..isDebugEnabled()) {
											..debug("Found record for " + saa);
										}
//										TransactionImple tx = (TransactionImple) SubordinationManager.getTransactionImporter().recoverTransaction(uid);
										values.push(loadedXid);
									}
								}
							}
else if (xid == null) {
							if (tx != null)
								values.push(tx.baseXid());
else {
							XidImple loadedXid = (XidImplesaa.getXid();
							if (loadedXid.getFormatId() == .) {
								String loadedXidSubordinateNodeName = XATxConverter.getSubordinateNodeName(loadedXid.getXID());
								if (XATxConverter.getSubordinateNodeName(new XidImple(xid).getXID()).equals(loadedXidSubordinateNodeName)) {
									if (Arrays.equals(loadedXid.getGlobalTransactionId(), xid.getGlobalTransactionId())) {
										if (..isDebugEnabled()) {
											..debug("Found record for " + saa);
										}
										values.push(loadedXid);
									}
								}
							}
						}
                    }
                    else
                        finished = true;
                }
                while (!finished);
                
                if (values.size() > 0)
                {
                    int index = 0;
                    indoubt = new Xid[values.size()];
                    while (!values.empty())
                    {
                        indoubt[index] = values.pop();
                        index++;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        return indoubt;
    }

    
Rollback the imported transaction subordinate.

Parameters:
xid the transaction to roll back.
Throws:
XAException thrown if there are any errors.
    public void rollback (Xid xidthrows XAException
    {
		// JBTM-927 this can happen if the transaction has been rolled back by
		// the TransactionReaper
		try {
			tx = SubordinationManager.getTransactionImporter().getImportedTransaction(xid);
catch (XAException xae) {
			if (xae.errorCode == .) {
				// do nothing as already rolled back
				return;
			}
			throw xae;
		}
        try
        {
            if (tx == null)
                throw new XAException(.);
            if (tx.activated())
            {
                tx.doRollback();
                SubordinationManager.getTransactionImporter()
                        .removeImportedTransaction(xid);
            }
            else
                throw new XAException(.);
        }
        catch (XAException ex)
        {
            // resource hasn't had a chance to recover yet
            if (ex.errorCode != .)
            {
                SubordinationManager.getTransactionImporter()
                        .removeImportedTransaction(xid);
            }
            throw ex;
        }
        catch (final HeuristicRollbackException ex)
        {
            XAException xaException = new XAException(.);
            xaException.initCause(ex);
            throw xaException;
        }
        catch (HeuristicCommitException ex)
        {
            XAException xaException = new XAException(.);
            xaException.initCause(ex);
            throw xaException;
        }
        catch (HeuristicMixedException ex)
        {
            XAException xaException = new XAException(.);
            xaException.initCause(ex);
            throw xaException;
        }
        catch (final IllegalStateException ex)
        {
            SubordinationManager.getTransactionImporter()
                    .removeImportedTransaction(xid);
            XAException xaException = new XAException(.);
            xaException.initCause(ex);
            throw xaException;
        }
        catch (SystemException ex)
        {
            SubordinationManager.getTransactionImporter()
                    .removeImportedTransaction(xid);
            throw new XAException(.);
        }
    }
    
    public boolean beforeCompletion (Xid xidthrows javax.transaction.SystemException
    {
        try
        {
            SubordinateTransaction tx = SubordinationManager
                    .getTransactionImporter().getImportedTransaction(xid);
            if (tx == null)
                throw new UnexpectedConditionException();
           return tx.doBeforeCompletion();
        }
        catch (final Exception ex)
        {
            UnexpectedConditionException e = new UnexpectedConditionException();
            
            e.initCause(ex);
            
            throw e;
        }
    }
    private boolean _recoveryStarted = false;
New to GrepCode? Check out our FAQ X