1 23 24 28 29 package com.sun.jts.CosTransactions; 30 31 import javax.transaction.xa.Xid ; 32 import javax.transaction.xa.XAException ; 33 import javax.transaction.xa.XAResource ; 34 35 import javax.resource.spi.XATerminator ; 36 37 import org.omg.CosTransactions.Vote; 38 import org.omg.CosTransactions.HeuristicMixed; 39 40 46 public class XATerminatorImpl implements XATerminator { 47 48 private static void check(Xid xid) throws XAException { 49 if (xid == null || xid.getFormatId() == 0 || 51 xid.getBranchQualifier() == null || 52 xid.getGlobalTransactionId() == null) { 53 throw new XAException (XAException.XAER_NOTA); 54 } 55 } 56 57 74 public void commit(Xid xid, boolean onePhase) throws XAException { 75 76 check(xid); 78 GlobalTID tid = new GlobalTID(xid); 79 80 if (RecoveryManager.readAndUpdateTxMap(tid) == false) { 82 throw new XAException (XAException.XAER_PROTO); 83 } 84 85 boolean exceptionFlag = false; 86 int errorCode = XAException.XAER_PROTO; 87 try { 88 RecoveryManager.waitForRecovery(); 90 91 TopCoordinator coord = (TopCoordinator) 93 RecoveryManager.getCoordinator(tid); 94 95 if (coord == null) { errorCode = XAException.XAER_PROTO; 97 throw new XAException (errorCode); 98 } 99 100 synchronized (coord) { 103 if (onePhase) { 104 coord.beforeCompletion(); 105 if (coord.getParticipantCount() == 1) { 106 coord.commitOnePhase(); 107 } else { 108 Vote vote = Vote.VoteRollback; 109 try { 110 vote = coord.prepare(); 111 } catch (HeuristicMixed exc) { 112 errorCode = XAException.XA_HEURHAZ; 113 throw new XAException (errorCode); 114 } 115 if (vote == Vote.VoteCommit) { 116 coord.commit(); 117 } else if (vote == Vote.VoteRollback) { 118 coord.rollback(true); 119 } 120 } 121 } else { 122 coord.commit(); 123 } 124 } 125 } catch (Throwable exc) { 126 exceptionFlag = true; 127 XAException xaExc = new XAException (errorCode); 128 xaExc.initCause(exc); 129 throw xaExc; 130 } finally { 131 Thread thread = RecoveryManager.removeFromTxMap(tid); 132 if (thread == null || (thread != Thread.currentThread())) { if (!exceptionFlag) { 134 throw new XAException (XAException.XAER_RMERR); 135 } 136 } 137 } 138 } 139 140 150 public void forget(Xid xid) throws XAException {} 151 152 170 public int prepare(Xid xid) throws XAException { 171 172 check(xid); 174 GlobalTID tid = new GlobalTID(xid); 175 176 if (RecoveryManager.readAndUpdateTxMap(tid) == false) { 178 throw new XAException (XAException.XAER_PROTO); 179 } 180 181 boolean exceptionFlag = false; 182 int errorCode = XAException.XAER_PROTO; 183 try { 184 RecoveryManager.waitForRecovery(); 186 187 TopCoordinator coord = (TopCoordinator) 189 RecoveryManager.getCoordinator(tid); 190 191 if (coord == null) { errorCode = XAException.XAER_PROTO; 193 throw new XAException (errorCode); 194 } 195 196 synchronized (coord) { 199 coord.beforeCompletion(); 200 Vote vote = coord.prepare(); 201 if (vote == Vote.VoteRollback) { 202 errorCode = XAException.XA_RBROLLBACK; 203 } else if (vote == Vote.VoteCommit) { 204 return XAResource.XA_OK; 205 } else if (vote == Vote.VoteReadOnly) { 206 return XAResource.XA_RDONLY; 207 } 208 throw new XAException (errorCode); 209 } 210 } catch (Throwable exc) { 211 exceptionFlag = true; 212 XAException xaExc = new XAException (errorCode); 213 xaExc.initCause(exc); 214 throw xaExc; 215 } finally { 216 Thread thread = RecoveryManager.removeFromTxMap(tid); 217 if (thread == null || (thread != Thread.currentThread())) { if (!exceptionFlag) { 219 throw new XAException (XAException.XAER_RMERR); 220 } 221 } 222 } 223 } 224 225 245 public Xid [] recover(int flag) throws XAException { 246 247 RecoveryManager.waitForResync(); 249 250 return (Xid []) TimeoutManager.getInDoubtXids(); 251 } 252 253 268 public void rollback(Xid xid) throws XAException { 269 270 check(xid); 272 GlobalTID tid = new GlobalTID(xid); 273 274 if (RecoveryManager.readAndUpdateTxMap(tid) == false) { 276 throw new XAException (XAException.XAER_PROTO); 277 } 278 279 boolean exceptionFlag = false; 280 int errorCode = XAException.XAER_PROTO; 281 try { 282 RecoveryManager.waitForRecovery(); 284 285 TopCoordinator coord = (TopCoordinator) 287 RecoveryManager.getCoordinator(tid); 288 289 if (coord == null) { errorCode = XAException.XAER_PROTO; 291 throw new XAException (errorCode); 292 } 293 294 synchronized (coord) { 297 coord.rollback(true); 298 } 299 } catch (Throwable exc) { 300 exceptionFlag = true; 301 XAException xaExc = new XAException (errorCode); 302 xaExc.initCause(exc); 303 throw xaExc; 304 } finally { 305 Thread thread = RecoveryManager.removeFromTxMap(tid); 306 if (thread == null || (thread != Thread.currentThread())) { if (!exceptionFlag) { 308 throw new XAException (XAException.XAER_RMERR); 309 } 310 } 311 } 312 } 313 } 314 315 316 | Popular Tags |