1 46 47 package org.mr.ra; 48 49 import java.util.Collection ; 50 51 import javax.jms.JMSException ; 52 import javax.transaction.xa.XAException ; 53 import javax.transaction.xa.XAResource ; 54 import javax.transaction.xa.Xid ; 55 56 import org.apache.commons.logging.Log; 57 import org.apache.commons.logging.LogFactory; 58 import org.mr.api.jms.MantaSession; 59 import org.mr.api.jms.MantaXADescriptor; 60 import org.mr.api.jms.TransactionContext; 61 62 66 public class RATransactionContext extends TransactionContext { 67 68 private static final Log log = LogFactory.getLog(RATransactionContext.class); 69 private final TransactionContext sharedContext; 70 boolean useSharedTxContext=false; 71 72 public RATransactionContext(TransactionContext sharedContext) { 74 super(); 75 this.sharedContext = sharedContext; 76 setLocalTransactionEventListener(sharedContext.getLocalTransactionEventListener()); 77 } 78 79 public void setUseSharedTxContext(boolean enable) throws JMSException { 80 if (enable && (isInLocalTransaction() || isInXATransaction())) 81 throw new JMSException ("The resource is already being used in transaction context."); 82 useSharedTxContext = enable; 83 } 84 85 public void addSession(MantaSession session) { 86 super.addSession(session); 87 sharedContext.addSession(session); 88 } 89 public void removeSession(MantaSession session) { 90 super.removeSession(session); 91 sharedContext.removeSession(session); 92 } 93 94 public void begin() throws JMSException { 95 if (useSharedTxContext) 96 sharedContext.begin(); 97 else 98 super.begin(); 99 } 100 public void commit() throws JMSException { 101 if (useSharedTxContext) 102 sharedContext.commit(); 103 else 104 super.commit(); 105 } 106 107 public void rollback() throws JMSException { 108 if (useSharedTxContext) 109 sharedContext.rollback(); 110 else 111 super.rollback(); 112 } 113 114 public void commit(Xid xid, boolean onePhase) throws XAException { 115 if (useSharedTxContext) 116 sharedContext.commit(xid, onePhase); 117 else 118 super.commit(xid, onePhase); 119 } 120 121 public void end(Xid xid, int flags) throws XAException { 122 if (useSharedTxContext) 123 sharedContext.end(xid, flags); 124 else 125 super.end(xid, flags); 126 } 127 128 public void forget(Xid xid) throws XAException { 129 if (useSharedTxContext) 130 sharedContext.forget(xid); 131 else 132 super.forget(xid); 133 } 134 135 public Object getTransactionId() { 136 if (useSharedTxContext) 137 return sharedContext.getTransactionId(); 138 else 139 return super.getTransactionId(); 140 } 141 142 public int getTransactionTimeout() throws XAException { 143 if (useSharedTxContext) 144 return sharedContext.getTransactionTimeout(); 145 else 146 return super.getTransactionTimeout(); 147 } 148 149 public boolean isInLocalTransaction() { 150 if (useSharedTxContext) 151 return sharedContext.isInLocalTransaction(); 152 else 153 return super.isInLocalTransaction(); 154 } 155 156 public boolean isInXATransaction() { 157 if (useSharedTxContext) 158 return sharedContext.isInXATransaction(); 159 else 160 return super.isInXATransaction(); 161 } 162 163 public boolean isSameRM(XAResource xaResource) throws XAException { 164 if (useSharedTxContext) 165 return sharedContext.isSameRM(xaResource); 166 else 167 return super.isSameRM(xaResource); 168 } 169 170 public int prepare(Xid xid) throws XAException { 171 if (useSharedTxContext) 172 return sharedContext.prepare(xid); 173 else 174 return super.prepare(xid); 175 } 176 177 public Xid [] recover(int flag) throws XAException { 178 if (useSharedTxContext) 179 return sharedContext.recover(flag); 180 else 181 return super.recover(flag); 182 } 183 184 public void rollback(Xid xid) throws XAException { 185 if (useSharedTxContext) 186 sharedContext.rollback(xid); 187 else 188 super.rollback(xid); 189 } 190 191 public boolean setTransactionTimeout(int seconds) throws XAException { 192 if (useSharedTxContext) 193 return sharedContext.setTransactionTimeout(seconds); 194 else 195 return super.setTransactionTimeout(seconds); 196 } 197 198 public void start(Xid xid, int flags) throws XAException { 199 if (useSharedTxContext) 200 sharedContext.start(xid, flags); 201 else 202 super.start(xid, flags); 203 } 204 } 205 | Popular Tags |