Home » activemq-parent-5.3.1-source-release » org.apache » activemq » transaction » [javadoc | source]

    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.activemq.transaction;
   18   
   19   import java.io.IOException;
   20   import java.util.ArrayList;
   21   import java.util.Collections;
   22   import java.util.Iterator;
   23   
   24   import javax.transaction.xa.XAException;
   25   
   26   import org.apache.activemq.command.TransactionId;
   27   
   28   /**
   29    * Keeps track of all the actions the need to be done when a transaction does a
   30    * commit or rollback.
   31    * 
   32    * @version $Revision: 1.5 $
   33    */
   34   public abstract class Transaction {
   35   
   36       public static final byte START_STATE = 0; // can go to: 1,2,3
   37       public static final byte IN_USE_STATE = 1; // can go to: 2,3
   38       public static final byte PREPARED_STATE = 2; // can go to: 3
   39       public static final byte FINISHED_STATE = 3;
   40   
   41       private ArrayList<Synchronization> synchronizations = new ArrayList<Synchronization>();
   42       private byte state = START_STATE;
   43   
   44       public byte getState() {
   45           return state;
   46       }
   47   
   48       public void setState(byte state) {
   49           this.state = state;
   50       }
   51   
   52       public void addSynchronization(Synchronization r) {
   53           synchronizations.add(r);
   54           if (state == START_STATE) {
   55               state = IN_USE_STATE;
   56           }
   57       }
   58   
   59       public void removeSynchronization(Synchronization r) {
   60           synchronizations.remove(r);
   61       }
   62   
   63       public void prePrepare() throws Exception {
   64   
   65           // Is it ok to call prepare now given the state of the
   66           // transaction?
   67           switch (state) {
   68           case START_STATE:
   69           case IN_USE_STATE:
   70               break;
   71           default:
   72               XAException xae = new XAException("Prepare cannot be called now.");
   73               xae.errorCode = XAException.XAER_PROTO;
   74               throw xae;
   75           }
   76   
   77           // // Run the prePrepareTasks
   78           // for (Iterator iter = prePrepareTasks.iterator(); iter.hasNext();) {
   79           // Callback r = (Callback) iter.next();
   80           // r.execute();
   81           // }
   82       }
   83   
   84       protected void fireAfterCommit() throws Exception {
   85           for (Iterator<Synchronization> iter = synchronizations.iterator(); iter.hasNext();) {
   86               Synchronization s = iter.next();
   87               s.afterCommit();
   88           }
   89       }
   90   
   91       public void fireAfterRollback() throws Exception {
   92       	Collections.reverse(synchronizations);
   93           for (Iterator<Synchronization> iter = synchronizations.iterator(); iter.hasNext();) {
   94               Synchronization s = iter.next();
   95               s.afterRollback();
   96           }
   97       }
   98   
   99       public String toString() {
  100           return super.toString() + "[synchronizations=" + synchronizations + "]";
  101       }
  102   
  103       public abstract void commit(boolean onePhase) throws XAException, IOException;
  104   
  105       public abstract void rollback() throws XAException, IOException;
  106   
  107       public abstract int prepare() throws XAException, IOException;
  108   
  109       public abstract TransactionId getTransactionId();
  110   
  111       public boolean isPrepared() {
  112           return getState() == PREPARED_STATE;
  113       }
  114       
  115       public int size() {
  116           return synchronizations.size();
  117       }
  118   }

Home » activemq-parent-5.3.1-source-release » org.apache » activemq » transaction » [javadoc | source]