1 18 package org.apache.activemq.ra; 19 20 import javax.jms.JMSException ; 21 import javax.transaction.xa.XAException ; 22 import javax.transaction.xa.XAResource ; 23 import javax.transaction.xa.Xid ; 24 25 import org.apache.activemq.TransactionContext; 26 import org.apache.activemq.command.TransactionId; 27 import org.apache.activemq.transaction.Synchronization; 28 29 35 public class ManagedTransactionContext extends TransactionContext { 36 37 private final TransactionContext sharedContext; 38 boolean useSharedTxContext=false; 39 40 public ManagedTransactionContext(TransactionContext sharedContext) { 41 super(sharedContext.getConnection()); 42 this.sharedContext = sharedContext; 43 setLocalTransactionEventListener(sharedContext.getLocalTransactionEventListener()); 44 } 45 46 public void setUseSharedTxContext(boolean enable) throws JMSException { 47 if( isInLocalTransaction() || isInXATransaction() ) 48 throw new JMSException ("The resource is allready being used in transaction context."); 49 useSharedTxContext = enable; 50 } 51 52 public void begin() throws JMSException { 53 if( useSharedTxContext ) 54 sharedContext.begin(); 55 else 56 super.begin(); 57 } 58 public void commit() throws JMSException { 59 if( useSharedTxContext ) 60 sharedContext.commit(); 61 else 62 super.commit(); 63 } 64 65 public void commit(Xid xid, boolean onePhase) throws XAException { 66 if( useSharedTxContext ) 67 sharedContext.commit(xid, onePhase); 68 else 69 super.commit(xid, onePhase); 70 } 71 72 public void end(Xid xid, int flags) throws XAException { 73 if( useSharedTxContext ) 74 sharedContext.end(xid, flags); 75 else 76 super.end(xid, flags); 77 } 78 79 public void forget(Xid xid) throws XAException { 80 if( useSharedTxContext ) 81 sharedContext.forget(xid); 82 else 83 super.forget(xid); 84 } 85 86 public TransactionId getTransactionId() { 87 if( useSharedTxContext ) 88 return sharedContext.getTransactionId(); 89 else 90 return super.getTransactionId(); 91 } 92 93 public int getTransactionTimeout() throws XAException { 94 if( useSharedTxContext ) 95 return sharedContext.getTransactionTimeout(); 96 else 97 return super.getTransactionTimeout(); 98 } 99 100 public boolean isInLocalTransaction() { 101 if( useSharedTxContext ) 102 return sharedContext.isInLocalTransaction(); 103 else 104 return super.isInLocalTransaction(); 105 } 106 107 public boolean isInXATransaction() { 108 if( useSharedTxContext ) 109 return sharedContext.isInXATransaction(); 110 else 111 return super.isInXATransaction(); 112 } 113 114 public boolean isSameRM(XAResource xaResource) throws XAException { 115 if( useSharedTxContext ) 116 return sharedContext.isSameRM(xaResource); 117 else 118 return super.isSameRM(xaResource); 119 } 120 121 public int prepare(Xid xid) throws XAException { 122 if( useSharedTxContext ) 123 return sharedContext.prepare(xid); 124 else 125 return super.prepare(xid); 126 } 127 128 public Xid [] recover(int flag) throws XAException { 129 if( useSharedTxContext ) 130 return sharedContext.recover(flag); 131 else 132 return super.recover(flag); 133 } 134 135 public void rollback() throws JMSException { 136 if( useSharedTxContext ) 137 sharedContext.rollback(); 138 else 139 super.rollback(); 140 } 141 142 public void rollback(Xid xid) throws XAException { 143 if( useSharedTxContext ) 144 sharedContext.rollback(xid); 145 else 146 super.rollback(xid); 147 } 148 149 public boolean setTransactionTimeout(int seconds) throws XAException { 150 if( useSharedTxContext ) 151 return sharedContext.setTransactionTimeout(seconds); 152 else 153 return super.setTransactionTimeout(seconds); 154 } 155 156 public void start(Xid xid, int flags) throws XAException { 157 if( useSharedTxContext ) 158 sharedContext.start(xid, flags); 159 else 160 super.start(xid, flags); 161 } 162 163 public void addSynchronization(Synchronization s) { 164 if( useSharedTxContext ) 165 sharedContext.addSynchronization(s); 166 else 167 super.addSynchronization(s); 168 } 169 } 170 | Popular Tags |