1 18 package org.apache.activemq.transaction; 19 20 import java.io.IOException ; 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 24 import javax.transaction.xa.XAException ; 25 26 import org.apache.activemq.command.TransactionId; 27 28 34 public abstract class Transaction { 35 36 static final public byte START_STATE = 0; static final public byte IN_USE_STATE = 1; static final public byte PREPARED_STATE = 2; static final public byte FINISHED_STATE = 3; 40 41 private ArrayList synchronizations = new ArrayList (); 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 prePrepare() throws Exception { 60 61 switch (state) { 64 case START_STATE: 65 case IN_USE_STATE: 66 break; 67 default: 68 XAException xae = new XAException ("Prepare cannot be called now."); 69 xae.errorCode = XAException.XAER_PROTO; 70 throw xae; 71 } 72 73 } 79 80 protected void fireAfterCommit() throws Exception { 81 for (Iterator iter = synchronizations.iterator(); iter.hasNext();) { 82 Synchronization s = (Synchronization) iter.next(); 83 s.afterCommit(); 84 } 85 } 86 87 public void fireAfterRollback() throws Exception { 88 for (Iterator iter = synchronizations.iterator(); iter.hasNext();) { 89 Synchronization s = (Synchronization) iter.next(); 90 s.afterRollback(); 91 } 92 } 93 94 public String toString() { 95 return super.toString() + "[synchronizations=" + synchronizations +"]"; 96 } 97 98 abstract public void commit(boolean onePhase) throws XAException , IOException ; 99 abstract public void rollback() throws XAException , IOException ; 100 abstract public int prepare() throws XAException , IOException ; 101 102 abstract public TransactionId getTransactionId(); 103 104 public boolean isPrepared() { 105 return getState()==PREPARED_STATE; 106 } 107 } 108 | Popular Tags |