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) 2001,
  *
  * Hewlett-Packard Arjuna Labs,
  * Newcastle upon Tyne,
  * Tyne and Wear,
  * UK.
  *
  * $Id: TwoPhaseCoordinator.java 2342 2006-03-30 13:06:17Z  $
  */
 
 package com.arjuna.ats.arjuna.coordinator;
 
 
Adds support for synchronizations to BasicAction. It does not change thread associations either. It also allows any thread to terminate a transaction, even if it is not the transaction that is marked as current for the thread (unlike the BasicAction default).

Author(s):
Mark Little (mark@arjuna.com)
Version:
$Id: TwoPhaseCoordinator.java 2342 2006-03-30 13:06:17Z $
Since:
JTS 3.0.
 
 
 public class TwoPhaseCoordinator extends BasicAction implements Reapable
 {
 
 	public TwoPhaseCoordinator ()
 	{
 	}
 
 	public TwoPhaseCoordinator (Uid id)
 	{
 		super(id);
 	}
 
 	public int start ()
 	{
 		return start(BasicAction.Current());
 	}
 
 	public int start (BasicAction parentAction)
 	{
 		if (parentAction != null)
 		{
 		    if (typeOfAction() == .)
 			parentAction.addChildAction(this);
 		}
 
 		return super.Begin(parentAction);
 	}
 
 	public int end (boolean report_heuristics)
 	{
 		int outcome;
 
 		if (parent() != null)
 		{
 		    parent().removeChildAction(this);
 		}
 
         boolean canEnd = true;
         if(status() != . || TxControl.isBeforeCompletionWhenRollbackOnly())
         {
             canEnd = beforeCompletion();
         }
 
 		if (canEnd)
 		{
 			outcome = super.End(report_heuristics);
 		}
		else
			outcome = super.Abort();
		afterCompletion(outcomereport_heuristics);
		return outcome;
	}
	public int cancel ()
	{
		if (TxStats.enabled())
		if (parent() != null)
		// beforeCompletion();
		int outcome = super.Abort();
		return outcome;
	}
	{
		if (sr == null)
		int result = .;
		// only allow registration for top-level transactions.
		if (parent() != null)
		switch (status())
		{
		// https://jira.jboss.org/jira/browse/JBTM-608
		{
		    synchronized (this)
		    {
		        if ( == null)
		        {
		            // Synchronizations should be stored (or at least iterated) in their natural order
		             = new TreeSet();
		        }
		    }
		    // disallow addition of Synchronizations that would appear
		    // earlier in sequence than any that has already been called
		    // during the pre-commmit phase. This generic support is required for
		    // JTA Synchronization ordering behaviour
		    if(sr instanceof Comparable &&  != null) {
		        Comparable c = (Comparable)sr;
		        if(c.compareTo() != 1) {
		            return .;
		        }
		    }
		    // need to guard against synchs being added while we are performing beforeCompletion processing
		    synchronized () {
		        if (.add(sr))
		        {
		            result = .;
		        }
		    }
		}
		break;
		default:
		    break;
		}
		return result;
	}

Returns:
true if the transaction is running, false otherwise.
	public boolean running ()
	{
	}

Overloads BasicAction.type()
	public String type ()
	{
		return "/StateManager/BasicAction/AtomicAction/TwoPhaseCoordinator";
	}

Get any Throwable that was caught during commit processing but not directly rethrown.

Returns:
the Throwable, if any
	}
	protected TwoPhaseCoordinator (int at)
	{
		super(at);
	}
	protected TwoPhaseCoordinator (Uid uint at)
	{
		super(uat);
	}

Drive beforeCompletion participants.

Returns:
true if successful, false otherwise.
	protected boolean beforeCompletion ()
	{
	    boolean problem = false;
	    synchronized ()
	    {
	        if (!)
	        {
	             = true;
	            /*
	             * If we have a synchronization list then we must be top-level.
	             */
	            if ( != null)
	            {
	                /*
	                 * We must always call afterCompletion() methods, so just catch (and
	                 * log) any exceptions/errors from beforeCompletion() methods.
	                 *
	                 * If one of the Syncs throws an error the Record wrapper returns false
	                 * and we will rollback. Hence we don't then bother to call beforeCompletion
	                 * on the remaining records (it's not done for rollabcks anyhow).
	                 *
	                 * Since Synchronizations may register other Synchronizations, we can't simply
	                 * iterate the collection. Instead we work from an ordered copy, which we periodically
	                 * check for freshness. The addSynchronization method uses _currentRecord to disallow
	                 * adding records in the part of the array we have already traversed, thus all
	                 * Synchronization will be called and the (jta only) rules on ordering of interposed
	                 * Synchronization will be respected.
	                 */
	                int lastIndexProcessed = -1;
	                SynchronizationRecord[] copiedSynchs;
	                // need to guard against synchs being added while we are performing beforeCompletion processing
	                synchronized () {
	                    copiedSynchs = (SynchronizationRecord[]).toArray(new SynchronizationRecord[] {});
	                }
	                while( (lastIndexProcessed < .size()-1) && !problem) {
	                    synchronized () {
	                        // if new Synchronization have been registered, refresh our copy of the collection:
	                        if(copiedSynchs.length != .size()) {
	                            copiedSynchs = (SynchronizationRecord[]).toArray(new SynchronizationRecord[] {});
	                        }
	                    }
	                    lastIndexProcessed = lastIndexProcessed+1;
	                     = copiedSynchs[lastIndexProcessed];
	                    try
	                    {
	                        problem = !.beforeCompletion();
	                        // if something goes wrong, we can't just throw the exception, we need to continue to
	                        // complete the transaction. However, the exception may have interesting information that
	                        // we want later, so we keep a reference to it as well as logging it.
	                    }
	                    catch (Exception ex) {
	                        ..warn_coordinator_TwoPhaseCoordinator_2(.toString(), ex);
	                        if ( == null) {
	                             = ex;
	                        }
	                        problem = true;
	                    }
	                    catch (Error er) {
	                        ..warn_coordinator_TwoPhaseCoordinator_2(.toString(), er);
	                        if ( == null) {
	                             = er;
	                        }
	                        problem = true;
	                    }
	                }
	                if (problem)
	                {
	                    if (!preventCommit()) {
	                        /*
	                         * This should not happen. If it does, continue with commit
	                         * to tidy-up.
	                         */
	                        ..warn_coordinator_TwoPhaseCoordinator_1();
	                    }
	                }
	            }
	        }
	        else
	        {
	            /*
	             * beforeCompletions already called. Assume everything is alright
	             * to proceed to commit. The TM instance will flag the outcome. If
	             * it's rolling back, then we'll get an exception. If it's committing
	             * then we'll be blocked until the commit (assuming we're still the
	             * slower thread).
	             */
	        }
	    }
	    return !problem;
	}

Drive afterCompletion participants.

Parameters:
myStatus the outcome of the transaction (ActionStatus.COMMITTED or ActionStatus.ABORTED).
Returns:
true if successful, false otherwise.
	protected boolean afterCompletion (int myStatus)
	{
	    return afterCompletion(myStatusfalse);
	}

Drive afterCompletion participants.

Parameters:
myStatus the outcome of the transaction (ActionStatus.COMMITTED or ActionStatus.ABORTED).
report_heuristics does the caller want to be informed about heurisitics at the point of invocation?
Returns:
true if successful, false otherwise.
	protected boolean afterCompletion (int myStatusboolean report_heuristics)
	{
		if (myStatus == .) {
            return false;
        }
		boolean problem = false;
		synchronized ()
		{
			{
				 = true;
				if ( != null)
				{
					// afterCompletions should run in reverse order compared to
					// beforeCompletions
					Stack stack = new Stack();
					Iterator iterator = .iterator();
					while(iterator.hasNext()) {
						stack.push(iterator.next());
					}
					iterator = stack.iterator();
					/*
					 * Regardless of failures, we must tell all synchronizations what
					 * happened.
					 */
					while(!stack.isEmpty())
					{
						/*
						 * If the caller doesn't want to be informed of heuristics during completion
						 * then it's possible the application (or admin) may still want to be informed.
						 * So special participants can be registered with the transaction which are
						 * triggered during the Synchronization phase and given the true outcome of
						 * the transaction. We do not dictate a specific implementation for what these
						 * participants do with the information (e.g., OTS allows for the CORBA Notification Service
						 * to be used).
						 */
						if (!report_heuristics)
						{
						    if (record instanceof HeuristicNotification)
						    {
						    }
						}
						try
						{
							if (!record.afterCompletion(myStatus)) {
                                ..warn_coordinator_TwoPhaseCoordinator_4(record.toString());
                                problem = true;
                            }
						}
						catch (Exception ex) {
                            ..warn_coordinator_TwoPhaseCoordinator_4a(record.toString(), ex);
                            problem = true;
                        }
						catch (Error er) {
                            ..warn_coordinator_TwoPhaseCoordinator_4b(record.toString(), er);
                            problem = true;
                        }
					}
                    synchronized () {
                        // nulling _syncs causes concurrency problems, so dispose contents instead:
                        .clear();
                    }
				}
			}
			else
			{
			}
		}
		return !problem;
	}
    {
        java.util.Map<UidStringsynchs = new java.util.HashMap<UidString> ();
        synchronized (this) {
            if ( != null)
            {
                for (Object _synch : )
                {
                    SynchronizationRecord synch = (SynchronizationRecord_synch;
                    synchs.put(synch.get_uid(), synch.toString());
                }
            }
        }
        return synchs;
    }
    private SortedSet _synchs;
	private SynchronizationRecord _currentRecord// the most recently processed Synchronization.
	private Object _syncLock = new Object();
	private boolean _beforeCalled = false;
	private boolean _afterCalled = false;
New to GrepCode? Check out our FAQ X