1 21 22 31 32 33 package org.apache.derby.impl.drda; 34 import org.apache.derby.iapi.services.sanity.SanityManager; 35 import javax.transaction.xa.*; 36 37 38 class DRDAXAProtocol { 39 40 private DRDAConnThread connThread; 41 private DDMReader reader; 42 private DDMWriter writer; 43 44 45 DRDAXAProtocol(DRDAConnThread connThread) 46 { 47 this.connThread = connThread; 48 reader = connThread.getReader(); 49 writer = connThread.getWriter(); 50 51 } 52 53 54 55 59 protected void parseSYNCCTL() throws DRDAProtocolException 60 { 61 62 reader.markCollection(); 63 64 int codePoint = reader.getCodePoint(CodePoint.SYNCTYPE); 65 int syncType = parseSYNCTYPE(); 66 67 int xaflags = 0; 68 boolean readXAFlags = false; 69 Xid xid = null; 70 71 codePoint = reader.getCodePoint(); 72 while (codePoint != -1) 73 { 74 switch(codePoint) 75 { 76 case CodePoint.XID: 77 xid = parseXID(); 78 break; 79 case CodePoint.XAFLAGS: 80 xaflags = parseXAFlags(); 81 readXAFlags =true; 82 break; 83 case CodePoint.TIMEOUT: 84 reader.skipBytes(); 86 break; 87 case CodePoint.RLSCONV: 88 connThread.codePointNotSupported(codePoint); 89 default: 90 connThread.invalidCodePoint(codePoint); 91 } 92 93 codePoint = reader.getCodePoint(); 94 } 95 96 97 { 98 connThread.trace("syncType = " + syncTypeToString(syncType)); 99 connThread.trace("xid = " + xid); 100 connThread.trace("xaflags =" + xaflagsToString(xaflags)); 101 } 102 103 if (syncType != CodePoint.SYNCTYPE_INDOUBT) 104 { 105 if (xid == null) 106 connThread.missingCodePoint(CodePoint.XID); 107 108 if (syncType != CodePoint.SYNCTYPE_REQ_FORGET && 110 ! readXAFlags) 111 if (SanityManager.DEBUG) 112 connThread.missingCodePoint(CodePoint.XAFLAGS); 113 } 114 115 switch (syncType) 116 { 117 case CodePoint.SYNCTYPE_NEW_UOW: 118 startXATransaction(xid,xaflags); 121 break; 122 case CodePoint.SYNCTYPE_END_UOW: 123 endXA(xid,xaflags); 125 break; 126 case CodePoint.SYNCTYPE_PREPARE: 127 prepareXATransaction(xid); 128 break; 130 case CodePoint.SYNCTYPE_MIGRATE: 131 connThread.codePointNotSupported(codePoint); 133 break; 134 case CodePoint.SYNCTYPE_REQ_COMMIT: 135 commitTransaction(xid,xaflags); 137 break; 138 case CodePoint.SYNCTYPE_COMMITTED: 139 commitTransaction(xid, xaflags); 141 break; 142 case CodePoint.SYNCTYPE_REQ_FORGET: 143 forgetXATransaction(xid); 145 break; 146 case CodePoint.SYNCTYPE_ROLLBACK: 147 rollbackTransaction(xid); 149 break; 150 case CodePoint.SYNCTYPE_INDOUBT: 151 if (readXAFlags) 153 recoverXA(xaflags); 154 else 155 recoverXA(); 156 break; 157 default: 158 connThread.invalidCodePoint(codePoint); 159 } 160 161 } 162 163 180 protected int parseSYNCTYPE() throws DRDAProtocolException 181 { 182 return reader.readUnsignedByte(); 183 184 } 185 186 187 190 private Xid parseXID () throws DRDAProtocolException 191 { 192 int formatId = reader.readNetworkInt(); 193 byte[] gtrid = null; 194 byte[] bqual = null; 195 if (formatId != -1) 196 { 197 int gtridLen = reader.readNetworkInt(); 198 int bqualLen = reader.readNetworkInt(); 199 200 gtrid = reader.readBytes(gtridLen); 201 bqual = reader.readBytes(bqualLen); 202 } 203 return new DRDAXid(formatId, gtrid, bqual); 204 } 205 206 207 213 private int parseXIDSHR() throws DRDAProtocolException 214 { 215 return reader.readUnsignedByte(); 216 } 217 218 224 private int parseXAFlags() throws DRDAProtocolException 225 { 226 return reader.readNetworkInt(); 227 } 228 229 230 237 private void startXATransaction(Xid xid, int xaflags) throws DRDAProtocolException 238 { 239 XAResource xaResource = getXAResource(); 240 int xaRetVal = xaResource.XA_OK; 241 242 try { 243 if (xid.getFormatId() != -1) 244 xaResource.start(xid,xaflags); 245 } catch (XAException xe) 246 { 247 xaRetVal = processXAException(xe); 248 } 249 writeSYNCCRD(CodePoint.SYNCTYPE_NEW_UOW, 250 xaRetVal, null); 251 252 } 253 254 255 262 private void commitTransaction(Xid xid, int xaflags) throws DRDAProtocolException 263 { 264 boolean local = ( xid.getFormatId() == -1); 265 if (local) 266 commitLocalTransaction(); 267 else 268 commitXATransaction(xid, xaflags); 269 } 270 271 276 private void commitLocalTransaction() throws DRDAProtocolException 277 { 278 int xaRetVal = XAResource.XA_OK; 279 try { 280 connThread.getDatabase().commit(); 281 } 282 catch (Exception e) 283 { 284 xaRetVal = XAException.XAER_RMFAIL; 285 if (SanityManager.DEBUG) 286 { 287 connThread.getServer().consoleExceptionPrint(e); 288 } 289 290 } 291 writeSYNCCRD(CodePoint.SYNCTYPE_COMMITTED, 292 xaRetVal, null); 293 294 } 295 296 297 304 private void commitXATransaction(Xid xid, int xaflags) throws DRDAProtocolException 305 { 306 XAResource xaResource = getXAResource(); 307 int xaRetVal = xaResource.XA_OK; 308 boolean isOnePhase = (xaflags & XAResource.TMONEPHASE) != 0; 310 try { 311 xaResource.commit(xid, isOnePhase); 312 if (SanityManager.DEBUG) 313 connThread.trace("committed XA transaction: xaRetVal=" + xaRetVal); 314 315 } catch (XAException xe) 316 { 317 xaRetVal = processXAException(xe); 318 } 319 writeSYNCCRD(CodePoint.SYNCTYPE_COMMITTED, 320 xaRetVal, null); 321 322 } 323 324 329 private void rollbackTransaction(Xid xid) throws DRDAProtocolException 330 { 331 boolean local = ( xid.getFormatId() == -1); 332 if (local) 333 rollbackLocalTransaction(); 334 else 335 rollbackXATransaction(xid); 336 } 337 338 342 private void rollbackLocalTransaction() throws DRDAProtocolException 343 { 344 int xaRetVal = XAResource.XA_OK; 345 try { 346 connThread.getDatabase().rollback(); 347 } 348 catch (Exception e) 349 { 350 xaRetVal = XAException.XAER_RMFAIL; 351 if (SanityManager.DEBUG) 352 { 353 connThread.getServer().consoleExceptionPrint(e); 354 } 355 356 } 357 writeSYNCCRD(CodePoint.SYNCTYPE_COMMITTED, 358 xaRetVal, null); 359 360 } 361 362 368 private void rollbackXATransaction(Xid xid) throws DRDAProtocolException 369 { 370 XAResource xaResource = getXAResource(); 371 int xaRetVal = xaResource.XA_OK; 372 373 try { 374 xaResource.rollback(xid); 375 if (SanityManager.DEBUG) 376 { 377 connThread.trace("rollback XA transaction: xaRetVal=" + xaRetVal); 378 } 379 } catch (XAException xe) 380 { 381 xaRetVal = processXAException(xe); 382 } 383 writeSYNCCRD(CodePoint.SYNCTYPE_ROLLBACK, 384 xaRetVal, null); 385 386 } 387 388 395 private void endXA(Xid xid, int xaflags) throws DRDAProtocolException 396 { 397 XAResource xaResource = getXAResource(); 398 int xaRetVal = xaResource.XA_OK; 399 400 try { 401 xaResource.end(xid,xaflags); 402 if (SanityManager.DEBUG) 403 { 404 connThread.trace("ended XA transaction. xid = " + xid + 405 " xaflags =" + xaflags + 406 "xaRetVal=" + xaRetVal); 407 } 408 } catch (XAException xe) 409 { 410 xaRetVal = processXAException(xe); 411 } 412 writeSYNCCRD(CodePoint.SYNCTYPE_END_UOW, 413 xaRetVal, null); 414 } 415 416 417 423 private void prepareXATransaction(Xid xid) throws DRDAProtocolException 424 { 425 XAResource xaResource = getXAResource(); 426 int xaRetVal = xaResource.XA_OK; 427 428 try { 429 xaRetVal = xaResource.prepare(xid); 430 if (SanityManager.DEBUG) 431 { 432 connThread.trace("prepared xa transaction: xaRetVal=" + 433 xaRetVal); 434 } 435 } catch (XAException xe) 436 { 437 xaRetVal = processXAException(xe); 438 } 439 writeSYNCCRD(CodePoint.SYNCTYPE_PREPARE, 440 xaRetVal, null); 441 } 442 443 449 private void forgetXATransaction(Xid xid) throws DRDAProtocolException 450 { 451 XAResource xaResource = getXAResource(); 452 int xaRetVal = xaResource.XA_OK; 453 454 try { 455 xaResource.forget(xid); 456 if (SanityManager.DEBUG) 457 { 458 connThread.trace("forgot xa transaction: xaRetVal=" + xaRetVal); 459 } 460 } catch (XAException xe) 461 { 462 xaRetVal = processXAException(xe); 463 } 464 writeSYNCCRD(CodePoint.SYNCTYPE_REQ_FORGET, 465 xaRetVal, null); 466 } 467 468 private void recoverXA() throws DRDAProtocolException 471 { 472 recoverXA(XAResource.TMSTARTRSCAN); 473 } 474 475 480 private void recoverXA(int xaflags) throws DRDAProtocolException 481 { 482 XAResource xaResource = getXAResource(); 483 int xaRetVal = xaResource.XA_OK; 484 Xid[] indoubtXids = null; 485 try { 486 indoubtXids = xaResource.recover(xaflags); 487 } catch (XAException xe) 488 { 489 xaRetVal = processXAException(xe); 490 } 491 writeSYNCCRD(CodePoint.SYNCTYPE_INDOUBT, 492 xaRetVal, indoubtXids); 493 } 494 495 502 private void writeSYNCCRD (int synctype, int xaRetVal, Xid[] xids) throws DRDAProtocolException 503 { 504 writer.createDssReply(); 505 writer.startDdm(CodePoint.SYNCCRD); 506 writer.startDdm(CodePoint.XARETVAL); 507 writer.writeInt(xaRetVal); 508 writer.endDdm(); 509 if (xids != null) 510 writePRPHRCLST(xids); 511 writer.endDdmAndDss(); 512 } 513 514 519 private void writePRPHRCLST(Xid[] xids) throws DRDAProtocolException 520 { 521 int xidcnt = (xids == null ? 0 : xids.length); 522 writer.startDdm(CodePoint.PRPHRCLST); 523 writer.writeScalar2Bytes(CodePoint.XIDCNT, xidcnt); 524 for (int i = 0; i < xidcnt; i++) 525 writeXID(xids[i]); 526 writer.endDdm(); 527 } 528 529 534 535 private void writeXID(Xid xid) throws DRDAProtocolException 536 { 537 writer.startDdm(CodePoint.XID); 538 int formatId = xid.getFormatId(); 539 byte[] gtrid = xid.getGlobalTransactionId(); 540 byte[] bqual = xid.getBranchQualifier(); 541 542 writer.writeInt(formatId); 543 writer.writeInt(gtrid.length); 544 writer.writeInt(bqual.length); 545 writer.writeBytes(gtrid); 546 writer.writeBytes(bqual); 547 writer.endDdm(); 548 } 549 550 551 555 private XAResource getXAResource() 556 { 557 return ((XADatabase) connThread.getDatabase()).getXAResource(); 558 559 } 560 561 565 private String syncTypeToString(int syncType) 566 { 567 switch (syncType) 568 { 569 case CodePoint.SYNCTYPE_NEW_UOW: 570 return "SYNCTYPE_NEW_UOW"; 571 572 case CodePoint.SYNCTYPE_END_UOW: 573 return "SYNCTYPE_END_UOW"; 574 575 case CodePoint.SYNCTYPE_PREPARE: 576 return "SYNCTYPE_PREPARE"; 577 578 case CodePoint.SYNCTYPE_MIGRATE: 579 return "SYNCTYPE_MIGRATE"; 580 581 case CodePoint.SYNCTYPE_REQ_COMMIT: 582 return "SYNCTYPE_REQ_COMMIT"; 583 584 case CodePoint.SYNCTYPE_COMMITTED: 585 return "SYNCTYPE_COMMITTED"; 586 587 case CodePoint.SYNCTYPE_REQ_FORGET: 588 return "SYNCTYPE_FORGET"; 589 590 case CodePoint.SYNCTYPE_ROLLBACK: 591 return "SYNCTYPE_ROLLBACK"; 592 593 case CodePoint.SYNCTYPE_REQ_LOG: 594 return "SYNCTYPE_REQ_LOG"; 595 596 case CodePoint.SYNCTYPE_MIGRATED: 597 return "SYNCTYPE_MIGRATED"; 598 599 case CodePoint.SYNCTYPE_INDOUBT: 600 return "SYNCTYPE_INDOUBT"; 601 602 default: 603 return "UNKNOWN SYNCTYPE"; 604 } 605 } 606 607 612 private String xaflagsToString(int xaflags) 613 { 614 switch (xaflags) 615 { 616 case XAResource.TMENDRSCAN : 617 return "XAResource.TMENDRSCAN"; 618 619 case XAResource.TMFAIL: 620 return "XAResource.TMFAIL"; 621 622 case XAResource.TMNOFLAGS: 623 return "XAResource.TMNOFLAGS"; 624 625 case XAResource.TMJOIN: 626 return "XAResource.TMJOIN"; 627 628 case XAResource.TMONEPHASE: 629 return "XAResource.TMONEPHASE"; 630 631 case XAResource.TMRESUME: 632 return "XAResource.TMRESUME"; 633 634 case XAResource.TMSTARTRSCAN: 635 return "XAResource.TMSTARTRSCAN"; 636 637 case XAResource.TMSUCCESS: 638 return "XAResource.TMSUCCESS"; 639 640 case XAResource.TMSUSPEND: 641 return "XAResource.TMSUSPEND"; 642 643 default: 644 return "UNRECOGNIZED flags:" + xaflags; 645 646 } 647 } 648 649 654 private int processXAException(XAException xe) 655 { 656 int xaRetVal = xe.errorCode; 657 if (SanityManager.DEBUG) 658 { 659 connThread.getServer().consoleExceptionPrint(xe); 660 } 661 return xaRetVal; 662 } 663 664 } 665 666 667 668 669 670 671 672 673 674 675 676 | Popular Tags |