1 18 package org.apache.activemq.transaction; 19 20 import java.io.IOException ; 21 22 import javax.transaction.xa.XAException ; 23 24 import org.apache.activemq.broker.ConnectionContext; 25 import org.apache.activemq.command.LocalTransactionId; 26 import org.apache.activemq.command.TransactionId; 27 import org.apache.activemq.store.TransactionStore; 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 34 public class LocalTransaction extends Transaction { 35 36 private static final Log log = LogFactory.getLog(LocalTransaction.class); 37 38 private final TransactionStore transactionStore; 39 private final LocalTransactionId xid; 40 private final ConnectionContext context; 41 42 public LocalTransaction(TransactionStore transactionStore, LocalTransactionId xid, ConnectionContext context) { 43 this.transactionStore = transactionStore; 44 this.xid = xid; 45 this.context = context; 46 } 47 48 public void commit(boolean onePhase) throws XAException , IOException { 49 try { 51 prePrepare(); 52 } 53 catch (XAException e) { 54 throw e; 55 } 56 catch (Throwable e) { 57 log.warn("COMMIT FAILED: ", e); 58 rollback(); 59 XAException xae = new XAException ("COMMIT FAILED: Transaction rolled back."); 61 xae.errorCode = XAException.XA_RBOTHER; 62 xae.initCause(e); 63 throw xae; 64 } 65 66 setState(Transaction.FINISHED_STATE); 67 context.getTransactions().remove(xid); 68 transactionStore.commit(getTransactionId(), false); 69 70 try { 71 fireAfterCommit(); 72 } 73 catch (Throwable e) { 74 log.warn("POST COMMIT FAILED: ", e); 77 XAException xae = new XAException ("POST COMMIT FAILED"); 78 xae.errorCode = XAException.XAER_RMERR; 79 xae.initCause(e); 80 throw xae; 81 } 82 } 83 84 public void rollback() throws XAException , IOException { 85 86 setState(Transaction.FINISHED_STATE); 87 context.getTransactions().remove(xid); 88 transactionStore.rollback(getTransactionId()); 89 90 try { 91 fireAfterRollback(); 92 } 93 catch (Throwable e) { 94 log.warn("POST ROLLBACK FAILED: ", e); 95 XAException xae = new XAException ("POST ROLLBACK FAILED"); 96 xae.errorCode = XAException.XAER_RMERR; 97 xae.initCause(e); 98 throw xae; 99 } 100 } 101 102 public int prepare() throws XAException { 103 XAException xae = new XAException ("Prepare not implemented on Local Transactions."); 104 xae.errorCode = XAException.XAER_RMERR; 105 throw xae; 106 } 107 108 public TransactionId getTransactionId() { 109 return xid; 110 } 111 112 } 113 | Popular Tags |