1 23 24 package org.apache.slide.common; 25 26 import java.util.Vector ; 27 28 import javax.transaction.xa.XAException ; 29 import javax.transaction.xa.XAResource ; 30 import javax.transaction.xa.Xid ; 31 32 import org.apache.slide.transaction.SlideTransactionManager; 33 34 41 public abstract class AbstractSimpleService extends AbstractServiceBase 42 implements Service { 43 44 45 47 48 public static final int TX_IDLE = 0; 49 public static final int TX_PREPARED = 1; 50 public static final int TX_SUSPENDED = 2; 51 52 53 55 56 59 protected Xid currentContext = null; 60 61 62 65 protected int status = TX_IDLE; 66 67 68 71 protected int transactionTimeout = 72 SlideTransactionManager.DEFAULT_TRANSACTION_TIMEOUT; 73 74 75 78 protected boolean rollbackOnly = false; 79 80 81 83 84 98 public void commit(Xid xid, boolean onePhase) 99 throws XAException { 100 101 if (currentContext == null) 102 throw new XAException (XAException.XAER_NOTA); 103 if (xid == null) 104 throw new XAException (XAException.XAER_INVAL); 105 if (currentContext.getGlobalTransactionId() 106 != xid.getGlobalTransactionId()) 107 throw new XAException (XAException.XAER_PROTO); 108 109 if (!onePhase && status != TX_PREPARED) 110 throw new XAException (XAException.XAER_PROTO); 111 if (onePhase && (!((status == TX_IDLE) || (status == TX_SUSPENDED)))) 112 throw new XAException (XAException.XAER_PROTO); 113 114 if (rollbackOnly) { 115 rollback(xid); 116 throw new XAException (XAException.XA_RBROLLBACK); 117 } 118 119 currentContext = null; 120 status = TX_IDLE; 121 rollbackOnly = false; 122 123 } 124 125 126 145 public void end(Xid xid, int flags) 146 throws XAException { 147 148 if (currentContext == null) 149 throw new XAException (XAException.XAER_NOTA); 150 if (xid == null) 151 throw new XAException (XAException.XAER_INVAL); 152 if (currentContext.getGlobalTransactionId() 153 != xid.getGlobalTransactionId()) 154 throw new XAException (XAException.XAER_PROTO); 155 156 if (flags == XAResource.TMSUSPEND) 157 status = TX_SUSPENDED; 158 159 if (flags == XAResource.TMFAIL) 160 rollbackOnly = true; 161 162 } 163 164 165 173 public void forget(Xid xid) 174 throws XAException { 175 176 if (currentContext == null) 177 throw new XAException (XAException.XAER_NOTA); 178 if (xid == null) 179 throw new XAException (XAException.XAER_INVAL); 180 if (currentContext.getGlobalTransactionId() 181 != xid.getGlobalTransactionId()) 182 throw new XAException (XAException.XAER_PROTO); 183 184 currentContext = null; 185 status = TX_IDLE; 186 rollbackOnly = false; 187 188 } 189 190 191 202 public int getTransactionTimeout() 203 throws XAException { 204 205 return transactionTimeout; 206 207 } 208 209 210 221 public boolean isSameRM(XAResource xares) 222 throws XAException { 223 224 if (xares == null) 225 return false; 226 227 return (this == xares); 228 229 } 230 231 232 245 public int prepare(Xid xid) 246 throws XAException { 247 248 if (currentContext == null) 249 throw new XAException (XAException.XAER_NOTA); 250 if (xid == null) 251 throw new XAException (XAException.XAER_INVAL); 252 if (currentContext.getGlobalTransactionId() 253 != xid.getGlobalTransactionId()) 254 throw new XAException (XAException.XAER_PROTO); 255 256 if (!((status == TX_IDLE) || (status == TX_SUSPENDED))) 257 throw new XAException (XAException.XAER_PROTO); 258 259 if (rollbackOnly) { 260 throw new XAException (XAException.XA_RBROLLBACK); 263 } 264 265 status = TX_PREPARED; 266 267 return XAResource.XA_OK; 268 269 } 270 271 272 287 public Xid [] recover(int flag) 288 throws XAException { 289 290 Vector list = new Vector (); 291 292 if ((status == TX_PREPARED) && (currentContext != null)) 293 list.addElement(currentContext); 294 295 return (Xid []) list.toArray(new Xid [list.size()]); 296 297 } 298 299 300 307 public void rollback(Xid xid) 308 throws XAException { 309 310 if (currentContext == null) 311 throw new XAException (XAException.XAER_NOTA); 312 if (xid == null) 313 throw new XAException (XAException.XAER_INVAL); 314 if (currentContext.getGlobalTransactionId() 315 != xid.getGlobalTransactionId()) 316 throw new XAException (XAException.XAER_PROTO); 317 318 status = TX_IDLE; 319 currentContext = null; 320 rollbackOnly = false; 321 322 } 323 324 325 334 public boolean setTransactionTimeout(int seconds) 335 throws XAException { 336 transactionTimeout = seconds; 337 return true; 338 } 339 340 341 357 public void start(Xid xid, int flags) 358 throws XAException { 359 360 if (xid == null) 361 throw new XAException (XAException.XAER_INVAL); 362 if ( (currentContext != null) && 363 (currentContext.getGlobalTransactionId() 364 != xid.getGlobalTransactionId()) ) 365 throw new XAException (XAException.XAER_OUTSIDE); 366 367 switch (flags) { 368 case XAResource.TMNOFLAGS: 369 if (currentContext != null) 370 throw new XAException (XAException.XAER_INVAL); 371 currentContext = xid; 372 break; 373 case XAResource.TMJOIN: 374 if (currentContext == null) 375 throw new XAException (XAException.XAER_NOTA); 376 if (currentContext.getGlobalTransactionId() 377 != xid.getGlobalTransactionId()) 378 throw new XAException (XAException.XAER_INVAL); 379 break; 380 case XAResource.TMRESUME: 381 if (currentContext == null) 382 throw new XAException (XAException.XAER_NOTA); 383 if (status != TX_SUSPENDED) 384 throw new XAException (XAException.XAER_INVAL); 385 status = TX_IDLE; 386 break; 387 } 388 389 } 390 391 392 } 393 394 | Popular Tags |