1 21 40 package org.apache.derby.client.net; 41 42 import java.net.InetAddress ; 43 import java.net.UnknownHostException ; 44 import java.util.Collections ; 45 import java.util.Enumeration ; 46 import java.util.LinkedList ; 47 import java.util.List ; 48 import java.util.Vector ; 49 import javax.sql.XAConnection ; 50 import javax.transaction.xa.XAException ; 51 import javax.transaction.xa.XAResource ; 52 import javax.transaction.xa.Xid ; 53 54 import org.apache.derby.client.ClientXid; 55 import org.apache.derby.client.am.Connection; 56 import org.apache.derby.client.am.SqlException; 57 import org.apache.derby.client.am.ClientMessageId; 58 import org.apache.derby.shared.common.reference.SQLState; 59 60 public class NetXAResource implements XAResource { 61 public static final int TMTIMEOUT = 0x00000100; 62 public static final int ACTIVE_ONLY = -1; 63 public static final int XA_NULL_XID = -1; public static final int INITIAL_CALLINFO_ELEMENTS = 1; 65 public static final int RECOVER_XID_ARRAY_LENGTH = 10; 66 public static final ClientXid nullXid = new ClientXid(); 67 68 public static final int XAFUNC_NONE = 0; 70 public static final int XAFUNC_COMMIT = 1; 71 public static final int XAFUNC_END = 2; 72 public static final int XAFUNC_FORGET = 3; 73 public static final int XAFUNC_PREPARE = 4; 74 public static final int XAFUNC_RECOVER = 5; 75 public static final int XAFUNC_ROLLBACK = 6; 76 public static final int XAFUNC_START = 7; 77 public static final String XAFUNCSTR_NONE = "No XA Function"; 78 public static final String XAFUNCSTR_COMMIT = "XAResource.commit()"; 79 public static final String XAFUNCSTR_END = "XAResource.end()"; 80 public static final String XAFUNCSTR_FORGET = "XAResource.forget()"; 81 public static final String XAFUNCSTR_PREPARE = "XAResource.prepare()"; 82 public static final String XAFUNCSTR_RECOVER = "XAResource.recover()"; 83 public static final String XAFUNCSTR_ROLLBACK = "XAResource.rollback()"; 84 public static final String XAFUNCSTR_START = "XAResource.start()"; 85 86 public int nextElement = 0; 87 88 protected static Vector xaResourceSameRMGroup_ = new Vector (); 90 protected int sameRMGroupIndex_ = 0; 91 protected NetXAResource nextSameRM_ = null; 92 protected boolean ignoreMe_ = false; 93 94 95 96 public org.apache.derby.client.am.SqlException exceptionsOnXA = null; 97 98 XAConnection xaconn_; 99 org.apache.derby.client.net.NetXAConnection netXAConn_; 100 org.apache.derby.client.net.NetConnection conn_; 101 int rmId_; NetXACallInfo callInfoArray_[] = 104 new NetXACallInfo[INITIAL_CALLINFO_ELEMENTS]; 105 int numXACallInfo_ = INITIAL_CALLINFO_ELEMENTS; 106 int connectionCount_ = 1; 107 int activeXATransCount_ = 0; 108 String rmIdx_; String rmIdy_; int port_; String ipaddr_; 114 private List specialRegisters_ = Collections.synchronizedList(new LinkedList ()); 115 116 public NetXAResource(XAConnection xaconn, int rmId, 117 String userId, String password, 118 org.apache.derby.client.net.NetXAConnection conn) { 119 xaconn_ = xaconn; 120 rmId_ = rmId; 121 conn_ = conn.getNetConnection(); 122 netXAConn_ = conn; 123 rmIdx_ = userId; 124 rmIdy_ = password; 125 port_ = conn_.netAgent_.getPort(); 126 ipaddr_ = conn_.netAgent_.socket_.getLocalAddress().getHostAddress(); 127 conn.setNetXAResource(this); 128 129 conn_.currXACallInfoOffset_ = 0; 131 132 for (int i = 0; i < INITIAL_CALLINFO_ELEMENTS; ++i) { 134 callInfoArray_[i] = new NetXACallInfo(null, XAResource.TMNOFLAGS, this, 135 null); 136 } 137 138 callInfoArray_[0].actualConn_ = conn; 141 callInfoArray_[0].currConnection_ = true; 142 callInfoArray_[0].freeEntry_ = false; 143 callInfoArray_[0].saveConnectionVariables(); 145 146 initForReuse(); 148 } 149 150 public void commit(Xid xid, boolean onePhase) throws XAException { 151 NetAgent netAgent = conn_.netAgent_; 152 int rc = XAResource.XA_OK; 153 154 exceptionsOnXA = null; 155 if (conn_.agent_.loggingEnabled()) { 156 conn_.agent_.logWriter_.traceEntry(this, "commit", xid, onePhase); 157 } 158 if (conn_.isPhysicalConnClosed()) { 159 connectionClosedFailure(); 160 } 161 162 NetXACallInfo callInfo = callInfoArray_[conn_.currXACallInfoOffset_]; 164 callInfo.xaFlags_ = (onePhase ? XAResource.TMONEPHASE : 165 XAResource.TMNOFLAGS); 166 callInfo.xid_ = xid; 167 callInfo.xaResource_ = this; 168 callInfo.xaRetVal_ = XAResource.XA_OK; try { 170 netAgent.beginWriteChainOutsideUOW(); 171 netAgent.netConnectionRequest_.writeXaCommit(conn_, xid); 172 netAgent.flowOutsideUOW(); 173 netAgent.netConnectionReply_.readXaCommit(conn_); 174 if (callInfo.xaRetVal_ != XAResource.XA_OK) { callInfo.xaFunction_ = XAFUNC_COMMIT; 176 rc = xaRetValErrorAccumSQL(callInfo, rc); 177 callInfo.xaRetVal_ = XAResource.XA_OK; } 179 netAgent.endReadChain(); 180 } catch (SqlException sqle) { 181 rc = XAException.XAER_RMERR; 182 exceptionsOnXA = org.apache.derby.client.am.Utils.accumulateSQLException 183 (sqle, exceptionsOnXA); 184 } finally { 185 conn_.pendingEndXACallinfoOffset_ = -1; } 187 if (rc != XAResource.XA_OK) { 188 throwXAException(rc, false); 189 } 190 } 191 192 210 211 public void end(Xid xid, int flags) throws XAException { 212 213 NetAgent netAgent = conn_.netAgent_; 214 int rc = XAResource.XA_OK; 215 exceptionsOnXA = null; 216 if (conn_.agent_.loggingEnabled()) { 217 conn_.agent_.logWriter_.traceEntry(this, "end", xid, flags); 218 } 219 if (conn_.isPhysicalConnClosed()) { 220 connectionClosedFailure(); 221 } 222 223 NetXACallInfo callInfo = callInfoArray_[conn_.currXACallInfoOffset_]; 224 callInfo.setReadOnlyTransactionFlag(conn_.readOnlyTransaction_); 225 callInfo.xaFlags_ = flags; 226 callInfo.xid_ = xid; 227 callInfo.xaResource_ = this; 228 callInfo.xaRetVal_ = XAResource.XA_OK; try { 230 netAgent.beginWriteChainOutsideUOW(); 231 netAgent.netConnectionRequest_.writeXaEndUnitOfWork(conn_); 232 netAgent.flowOutsideUOW(); 233 rc = netAgent.netConnectionReply_.readXaEndUnitOfWork(conn_); 234 conn_.pendingEndXACallinfoOffset_ = -1; if (callInfo.xaRetVal_ != XAResource.XA_OK) { callInfo.xaFunction_ = XAFUNC_END; 237 rc = xaRetValErrorAccumSQL(callInfo, rc); 238 callInfo.xaRetVal_ = XAResource.XA_OK; } 240 netAgent.endReadChain(); 241 } catch (SqlException sqle) { 242 rc = XAException.XAER_RMERR; 243 exceptionsOnXA = org.apache.derby.client.am.Utils.accumulateSQLException 244 (sqle, exceptionsOnXA); 245 } finally { 246 conn_.pendingEndXACallinfoOffset_ = -1; } 248 if (rc != XAResource.XA_OK) { 249 throwXAException(rc, false); 250 }else { 251 conn_.setXAState(Connection.XA_T0_NOT_ASSOCIATED); 252 } 253 } 254 255 263 264 public void forget(Xid xid) throws XAException { 265 NetAgent netAgent = conn_.netAgent_; 266 int rc = XAResource.XA_OK; 267 exceptionsOnXA = null; 268 269 if (conn_.agent_.loggingEnabled()) { 270 conn_.agent_.logWriter_.traceEntry(this, "forget", xid); 271 } 272 if (conn_.isPhysicalConnClosed()) { 273 connectionClosedFailure(); 274 } 275 NetXACallInfo callInfo = callInfoArray_[conn_.currXACallInfoOffset_]; 276 callInfo.xid_ = xid; 277 callInfo.xaResource_ = this; 278 callInfo.xaRetVal_ = XAResource.XA_OK; try { 280 netAgent.beginWriteChainOutsideUOW(); 282 283 netAgent.netConnectionRequest_.writeXaForget(netAgent.netConnection_, xid); 285 286 netAgent.flowOutsideUOW(); 287 288 netAgent.netConnectionReply_.readXaForget(netAgent.netConnection_); 290 291 netAgent.endReadChain(); 292 if (callInfo.xaRetVal_ != XAResource.XA_OK) { callInfo.xaFunction_ = XAFUNC_FORGET; 294 rc = xaRetValErrorAccumSQL(callInfo, rc); 295 callInfo.xaRetVal_ = XAResource.XA_OK; } 297 } catch (SqlException sqle) { 298 exceptionsOnXA = org.apache.derby.client.am.Utils.accumulateSQLException 299 (sqle, exceptionsOnXA); 300 throwXAException(XAException.XAER_RMERR); 301 } finally { 302 conn_.pendingEndXACallinfoOffset_ = -1; } 304 if (rc != XAResource.XA_OK) { 305 throwXAException(rc, false); 306 } 307 308 } 309 310 320 public int getTransactionTimeout() throws XAException { 321 if (conn_.agent_.loggingEnabled()) { 322 conn_.agent_.logWriter_.traceEntry(this, "getTransactionTimeout"); 323 } 324 exceptionsOnXA = null; 325 if (conn_.isPhysicalConnClosed()) { 326 connectionClosedFailure(); 327 } 328 329 if (conn_.agent_.loggingEnabled()) { 330 conn_.agent_.logWriter_.traceExit(this, "getTransactionTimeout", 0); 331 } 332 return 0; } 334 335 347 public int prepare(Xid xid) throws XAException { exceptionsOnXA = null; 350 351 if (conn_.agent_.loggingEnabled()) { 352 conn_.agent_.logWriter_.traceEntry(this, "prepare", xid); 353 } 354 if (conn_.isPhysicalConnClosed()) { 355 connectionClosedFailure(); 356 } 357 358 NetAgent netAgent = conn_.netAgent_; 360 int rc = XAResource.XA_OK; 361 NetXACallInfo callInfo = callInfoArray_[conn_.currXACallInfoOffset_]; 362 callInfo.xid_ = xid; 363 callInfo.xaResource_ = this; 364 callInfo.xaRetVal_ = XAResource.XA_OK; try { 366 netAgent.beginWriteChainOutsideUOW(); 367 netAgent.netConnectionRequest_.writeXaPrepare(conn_); 369 netAgent.flowOutsideUOW(); 370 371 rc = netAgent.netConnectionReply_.readXaPrepare(conn_); 373 if ((callInfo.xaRetVal_ != XAResource.XA_OK) && 374 (callInfo.xaRetVal_ != XAException.XA_RDONLY)) { callInfo.xaFunction_ = XAFUNC_PREPARE; 376 rc = xaRetValErrorAccumSQL(callInfo, rc); 377 callInfo.xaRetVal_ = XAResource.XA_OK; } 379 380 netAgent.endReadChain(); 381 } catch (SqlException sqle) { 382 rc = XAException.XAER_RMERR; 383 exceptionsOnXA = org.apache.derby.client.am.Utils.accumulateSQLException 384 (sqle, exceptionsOnXA); 385 } finally { 386 conn_.pendingEndXACallinfoOffset_ = -1; } 388 if ((rc != XAResource.XA_OK ) && (rc != XAResource.XA_RDONLY)) { 389 throwXAException(rc, false); 390 } 391 if (conn_.agent_.loggingEnabled()) { 392 conn_.agent_.logWriter_.traceExit(this, "prepare", rc); 393 } 394 return rc; 395 } 396 397 412 public Xid [] recover(int flag) throws XAException { 413 int rc = XAResource.XA_OK; 414 NetAgent netAgent = conn_.netAgent_; 415 416 if (conn_.agent_.loggingEnabled()) { 417 conn_.agent_.logWriter_.traceEntry(this, "recover", flag); 418 } 419 exceptionsOnXA = null; 420 if (conn_.isPhysicalConnClosed()) { 421 connectionClosedFailure(); 422 } 423 424 Xid [] xidList = null; 425 int numXid = 0; 426 427 NetXACallInfo callInfo = callInfoArray_[conn_.currXACallInfoOffset_]; 428 callInfo.xaFlags_ = flag; 429 callInfo.xaResource_ = this; 430 callInfo.xaRetVal_ = XAResource.XA_OK; try { 432 netAgent.beginWriteChainOutsideUOW(); 433 netAgent.netConnectionRequest_.writeXaRecover(conn_, flag); 435 netAgent.flowOutsideUOW(); 436 netAgent.netConnectionReply_.readXaRecover(conn_); 437 if (callInfo.xaRetVal_ != XAResource.XA_OK) { callInfo.xaFunction_ = XAFUNC_RECOVER; 439 rc = xaRetValErrorAccumSQL(callInfo, rc); 440 callInfo.xaRetVal_ = XAResource.XA_OK; } 442 netAgent.endReadChain(); 443 if (conn_.indoubtTransactions_ != null) { 444 numXid = conn_.indoubtTransactions_.size(); 445 xidList = new Xid [numXid]; 446 int i = 0; 447 nextElement = 0; 448 for (Enumeration e = conn_.indoubtTransactions_.keys(); 449 e.hasMoreElements(); i++) { 450 xidList[i] = (Xid ) e.nextElement(); 451 } 452 } 453 } catch (SqlException sqle) { 454 rc = XAException.XAER_RMERR; 455 exceptionsOnXA = org.apache.derby.client.am.Utils.accumulateSQLException 456 (sqle, exceptionsOnXA); 457 } finally { 458 conn_.pendingEndXACallinfoOffset_ = -1; } 460 if (rc != XAResource.XA_OK) { 461 throwXAException(rc, false); 462 } 463 464 if (conn_.agent_.loggingEnabled()) { 465 conn_.agent_.logWriter_.traceExit(this, "recover", xidList); 466 } 467 return xidList; 468 } 469 470 477 public void rollback(Xid xid) throws XAException { 478 NetAgent netAgent = conn_.netAgent_; 479 int rc = XAResource.XA_OK; 480 exceptionsOnXA = null; 481 482 if (conn_.agent_.loggingEnabled()) { 483 conn_.agent_.logWriter_.traceEntry(this, "rollback", xid); 484 } 485 if (conn_.isPhysicalConnClosed()) { 486 connectionClosedFailure(); 487 } 488 489 NetXACallInfo callInfo = callInfoArray_[conn_.currXACallInfoOffset_]; 491 callInfo.xid_ = xid; 492 callInfo.xaResource_ = this; 493 callInfo.xaRetVal_ = XAResource.XA_OK; try { 495 netAgent.beginWriteChainOutsideUOW(); 496 netAgent.netConnectionRequest_.writeXaRollback(conn_, xid); 497 netAgent.flowOutsideUOW(); 498 rc = netAgent.netConnectionReply_.readXaRollback(conn_); 500 netAgent.endReadChain(); 501 if (callInfo.xaRetVal_ != XAResource.XA_OK) { callInfo.xaFunction_ = XAFUNC_END; 503 rc = xaRetValErrorAccumSQL(callInfo, rc); 504 callInfo.xaRetVal_ = XAResource.XA_OK; } 506 } catch (SqlException sqle) { 507 rc = XAException.XAER_RMERR; 508 exceptionsOnXA = org.apache.derby.client.am.Utils.accumulateSQLException 509 (sqle, exceptionsOnXA); 510 } finally { 511 conn_.pendingEndXACallinfoOffset_ = -1; } 513 if (rc != XAResource.XA_OK) { 514 throwXAException(rc, false); 515 } 516 517 } 518 519 528 public boolean setTransactionTimeout(int seconds) throws XAException { 529 if (conn_.agent_.loggingEnabled()) { 530 conn_.agent_.logWriter_.traceExit(this, "setTransactionTimeout", false); 531 } 532 exceptionsOnXA = null; 533 return false; 537 } 538 539 548 public synchronized void start(Xid xid, int flags) throws XAException { 549 550 NetAgent netAgent = conn_.netAgent_; 551 int rc = XAResource.XA_OK; 552 exceptionsOnXA = null; 553 if (conn_.agent_.loggingEnabled()) { 554 conn_.agent_.logWriter_.traceEntry(this, "start", xid, flags); 555 } 556 if (conn_.isPhysicalConnClosed()) { 557 connectionClosedFailure(); 558 } 559 560 try { 563 if(conn_.autoCommit_) 564 conn_.flowAutoCommit(); 565 } catch (SqlException sqle) { 566 rc = XAException.XAER_RMERR; 567 exceptionsOnXA = org.apache.derby.client.am.Utils.accumulateSQLException 568 (sqle, exceptionsOnXA); 569 } 570 571 NetXACallInfo callInfo = callInfoArray_[conn_.currXACallInfoOffset_]; 573 callInfo.xaFlags_ = flags; 574 callInfo.xaInProgress_ = true; 575 callInfo.xid_ = xid; 576 callInfo.xaResource_ = this; 577 callInfo.xaRetVal_ = XAResource.XA_OK; try { 579 netAgent.beginWriteChainOutsideUOW(); 580 netAgent.netConnectionRequest_.writeXaStartUnitOfWork(conn_); 581 netAgent.flowOutsideUOW(); 582 netAgent.netConnectionReply_.readXaStartUnitOfWork(conn_); 583 if (callInfo.xaRetVal_ != XAResource.XA_OK) { callInfo.xaFunction_ = XAFUNC_START; 585 rc = xaRetValErrorAccumSQL(callInfo, rc); 586 callInfo.xaRetVal_ = XAResource.XA_OK; } 588 if (rc == XAResource.XA_OK) { 591 conn_.setXAState(Connection.XA_T1_ASSOCIATED); 592 } 593 594 } catch (SqlException sqle) { 595 rc = XAException.XAER_RMERR; 596 exceptionsOnXA = org.apache.derby.client.am.Utils.accumulateSQLException 597 (sqle, exceptionsOnXA); 598 } finally { 599 conn_.pendingEndXACallinfoOffset_ = -1; } 601 if (rc != XAResource.XA_OK) { 602 throwXAException(rc, false); 603 } 604 } 605 606 607 protected void throwXAException(int rc) throws XAException { 608 throwXAException(rc, rc != XAException.XAER_NOTA); 609 } 610 611 private String getXAExceptionText(int rc) { 612 String xaExceptionText; 613 switch (rc) { 614 case javax.transaction.xa.XAException.XA_RBROLLBACK: 615 xaExceptionText = "XA_RBROLLBACK"; 616 break; 617 case javax.transaction.xa.XAException.XA_RBCOMMFAIL: 618 xaExceptionText = "XA_RBCOMMFAIL"; 619 break; 620 case javax.transaction.xa.XAException.XA_RBDEADLOCK: 621 xaExceptionText = "XA_RBDEADLOCK"; 622 break; 623 case javax.transaction.xa.XAException.XA_RBINTEGRITY: 624 xaExceptionText = "XA_RBINTEGRITY"; 625 break; 626 case javax.transaction.xa.XAException.XA_RBOTHER: 627 xaExceptionText = "XA_RBOTHER"; 628 break; 629 case javax.transaction.xa.XAException.XA_RBPROTO: 630 xaExceptionText = "XA_RBPROTO"; 631 break; 632 case javax.transaction.xa.XAException.XA_RBTIMEOUT: 633 xaExceptionText = "XA_RBTIMEOUT"; 634 break; 635 case javax.transaction.xa.XAException.XA_RBTRANSIENT: 636 xaExceptionText = "XA_RBTRANSIENT"; 637 break; 638 case javax.transaction.xa.XAException.XA_NOMIGRATE: 639 xaExceptionText = "XA_NOMIGRATE"; 640 break; 641 case javax.transaction.xa.XAException.XA_HEURHAZ: 642 xaExceptionText = "XA_HEURHAZ"; 643 break; 644 case javax.transaction.xa.XAException.XA_HEURCOM: 645 xaExceptionText = "XA_HEURCOM"; 646 break; 647 case javax.transaction.xa.XAException.XA_HEURRB: 648 xaExceptionText = "XA_HEURRB"; 649 break; 650 case javax.transaction.xa.XAException.XA_HEURMIX: 651 xaExceptionText = "XA_HEURMIX"; 652 break; 653 case javax.transaction.xa.XAException.XA_RETRY: 654 xaExceptionText = "XA_RETRY"; 655 break; 656 case javax.transaction.xa.XAException.XA_RDONLY: 657 xaExceptionText = "XA_RDONLY"; 658 break; 659 case javax.transaction.xa.XAException.XAER_ASYNC: 660 xaExceptionText = "XAER_ASYNC"; 661 break; 662 case javax.transaction.xa.XAException.XAER_RMERR: 663 xaExceptionText = "XAER_RMERR"; 664 break; 665 case javax.transaction.xa.XAException.XAER_NOTA: 666 xaExceptionText = "XAER_NOTA"; 667 break; 668 case javax.transaction.xa.XAException.XAER_INVAL: 669 xaExceptionText = "XAER_INVAL"; 670 break; 671 case javax.transaction.xa.XAException.XAER_PROTO: 672 xaExceptionText = "XAER_PROTO"; 673 break; 674 case javax.transaction.xa.XAException.XAER_RMFAIL: 675 xaExceptionText = "XAER_RMFAIL"; 676 break; 677 case javax.transaction.xa.XAException.XAER_DUPID: 678 xaExceptionText = "XAER_DUPID"; 679 break; 680 case javax.transaction.xa.XAException.XAER_OUTSIDE: 681 xaExceptionText = "XAER_OUTSIDE"; 682 break; 683 case XAResource.XA_OK: 684 xaExceptionText = "XA_OK"; 685 break; 686 default: 687 xaExceptionText = "Unknown Error"; 688 break; 689 } 690 return xaExceptionText; 691 } 692 693 protected void throwXAException(int rc, boolean resetFlag) throws XAException { String xaExceptionText; 695 if (resetFlag) { 696 NetXACallInfo callInfo = callInfoArray_[conn_.currXACallInfoOffset_]; 698 callInfo.xaInProgress_ = false; 699 callInfo.xaWasSuspended = false; 700 } 701 702 xaExceptionText = getXAExceptionText(rc); 703 org.apache.derby.client.am.SqlException sqlExceptions = exceptionsOnXA; 705 706 while (exceptionsOnXA != null) { xaExceptionText = xaExceptionText + " : " + exceptionsOnXA.getMessage(); 708 exceptionsOnXA = (org.apache.derby.client.am.SqlException) 709 exceptionsOnXA.getNextException(); 710 } 711 org.apache.derby.client.am.XaException xaException = 712 new org.apache.derby.client.am.XaException(conn_.agent_.logWriter_, 713 sqlExceptions, 714 xaExceptionText); 715 xaException.errorCode = rc; 716 setXaStateForXAException(rc); 717 throw xaException; 718 } 719 720 721 729 private void setXaStateForXAException(int rc) { 730 switch (rc) 731 { 732 case javax.transaction.xa.XAException.XAER_RMFAIL: 736 case javax.transaction.xa.XAException.XAER_RMERR: 737 case javax.transaction.xa.XAException.XA_RBROLLBACK: 738 case javax.transaction.xa.XAException.XA_RBCOMMFAIL: 739 case javax.transaction.xa.XAException.XA_RBDEADLOCK: 740 case javax.transaction.xa.XAException.XA_RBINTEGRITY: 741 case javax.transaction.xa.XAException.XA_RBOTHER: 742 case javax.transaction.xa.XAException.XA_RBPROTO: 743 case javax.transaction.xa.XAException.XA_RBTIMEOUT: 744 case javax.transaction.xa.XAException.XA_RBTRANSIENT: 745 conn_.setXAState(Connection.XA_T0_NOT_ASSOCIATED); 746 break; 747 default: 762 return; 763 } 764 } 765 766 public boolean isSameRM(XAResource xares) throws XAException { 767 boolean isSame = false; exceptionsOnXA = null; 769 770 if (conn_.agent_.loggingEnabled()) { 771 conn_.agent_.logWriter_.traceEntry(this, "isSameRM", xares); 772 } 773 if (conn_.isPhysicalConnClosed()) { 774 connectionClosedFailure(); 775 } 776 777 if (xares instanceof org.apache.derby.client.net.NetXAResource) { NetXAResource derbyxares = (NetXAResource) xares; 780 while (true) { 781 if (!conn_.databaseName_.equalsIgnoreCase(derbyxares.conn_.databaseName_)) { 782 break; } 784 if (!conn_.netAgent_.server_.equalsIgnoreCase 785 (derbyxares.conn_.netAgent_.server_)) { try { 787 String server1 = this.processLocalHost(conn_.netAgent_.server_); 789 String server2 = 790 this.processLocalHost(derbyxares.conn_.netAgent_.server_); 791 InetAddress serverIP1 = InetAddress.getByName(server1); 793 InetAddress serverIP2 = InetAddress.getByName(server2); 794 if (!serverIP1.equals(serverIP2)) { 795 break; } 797 } catch (UnknownHostException ue) { 798 break; 799 } 800 } 801 if (conn_.netAgent_.port_ != derbyxares.conn_.netAgent_.port_) { 802 break; } 804 isSame = true; break; 806 } 807 } 808 809 if (conn_.agent_.loggingEnabled()) { 810 conn_.agent_.logWriter_.traceExit 811 (this, "isSameRM", isSame); 812 } 813 return isSame; 814 } 815 816 public static boolean xidsEqual(Xid xid1, Xid xid2) { if (xid1.getFormatId() != xid2.getFormatId()) { 819 return false; } 821 822 int xid1Length = xid1.getGlobalTransactionId().length; 824 if (xid1Length != xid2.getGlobalTransactionId().length) { 825 return false; } 827 byte[] xid1Bytes = xid1.getGlobalTransactionId(); 828 byte[] xid2Bytes = xid2.getGlobalTransactionId(); 829 int i; 830 for (i = 0; i < xid1Length; ++i) { if (xid1Bytes[i] != xid2Bytes[i]) { 832 return false; } 834 } 835 836 xid1Length = xid1.getBranchQualifier().length; 838 if (xid1Length != xid2.getBranchQualifier().length) { 839 return false; } 841 xid1Bytes = xid1.getBranchQualifier(); 842 xid2Bytes = xid2.getBranchQualifier(); 843 for (i = 0; i < xid1Length; ++i) { if (xid1Bytes[i] != xid2Bytes[i]) { 845 return false; } 847 } 848 849 return true; } 851 852 853 public List getSpecialRegisters() { 854 return specialRegisters_; 855 } 856 857 public void addSpecialRegisters(String s) { 858 if (s.substring(0, 1).equals("@")) { 859 if (specialRegisters_.remove(s.substring(1))) { 861 specialRegisters_.remove(s); 862 specialRegisters_.add(s.substring(1)); 863 } else { 864 specialRegisters_.remove(s); 865 specialRegisters_.add(s); 866 } 867 } else { specialRegisters_.remove(s); 869 specialRegisters_.add(s); 870 } 871 } 872 873 private void connectionClosedFailure() throws XAException { exceptionsOnXA = org.apache.derby.client.am.Utils.accumulateSQLException 875 (new SqlException(null, 876 new ClientMessageId(SQLState.NO_CURRENT_CONNECTION)), 877 exceptionsOnXA); 878 throwXAException(javax.transaction.xa.XAException.XAER_RMFAIL); 879 } 880 881 private String getXAFuncStr(int xaFunc) { 882 switch (xaFunc) { 883 case XAFUNC_COMMIT: 884 return XAFUNCSTR_COMMIT; 885 case XAFUNC_END: 886 return XAFUNCSTR_END; 887 case XAFUNC_FORGET: 888 return XAFUNCSTR_FORGET; 889 case XAFUNC_PREPARE: 890 return XAFUNCSTR_PREPARE; 891 case XAFUNC_RECOVER: 892 return XAFUNCSTR_RECOVER; 893 case XAFUNC_ROLLBACK: 894 return XAFUNCSTR_ROLLBACK; 895 case XAFUNC_START: 896 return XAFUNCSTR_START; 897 } 898 return XAFUNCSTR_NONE; 899 } 900 901 protected int xaRetValErrorAccumSQL(NetXACallInfo callInfo, int currentRC) { 902 903 int rc = callInfo.xaRetVal_; 906 907 if (rc != XAResource.XA_OK) { SqlException accumSql = new SqlException(conn_.netAgent_.logWriter_, 910 new ClientMessageId(SQLState.NET_XARETVAL_ERROR), 911 getXAFuncStr(callInfo.xaFunction_), 912 getXAExceptionText(rc), 913 org.apache.derby.client.am.SqlCode.queuedXAError); 914 exceptionsOnXA = org.apache.derby.client.am.Utils.accumulateSQLException 915 (accumSql, exceptionsOnXA); 916 917 if (currentRC != XAResource.XA_OK) { if (currentRC < 0) { return currentRC; 920 } 921 } 922 } 923 return rc; 924 } 925 926 private String processLocalHost(String serverName) { 927 if (serverName.equalsIgnoreCase("localhost")) { try { 929 InetAddress localhostNameIA = InetAddress.getLocalHost(); 930 String localhostName = localhostNameIA.getHostName(); 931 return localhostName; 932 } catch (SecurityException se) { 933 return serverName; 934 } catch (UnknownHostException ue) { 935 return serverName; 936 } 937 } 938 return serverName; 940 } 941 942 protected void removeXaresFromSameRMchain() { 943 try { 945 this.ignoreMe_ = true; NetXAResource prevXAResource = null; 948 NetXAResource currXAResource; 949 synchronized (xaResourceSameRMGroup_) { currXAResource = (NetXAResource) xaResourceSameRMGroup_.elementAt(sameRMGroupIndex_); 951 while (currXAResource != null) { if (currXAResource.ignoreMe_) { if (prevXAResource != null) { prevXAResource.nextSameRM_ = currXAResource.nextSameRM_; 955 } else { xaResourceSameRMGroup_.set(sameRMGroupIndex_, 957 currXAResource.nextSameRM_); 958 } 959 return; 960 } 961 prevXAResource = currXAResource; 963 currXAResource = currXAResource.nextSameRM_; 964 } 965 } 966 } finally { 967 this.ignoreMe_ = false; 968 } 969 } 970 971 972 public void initForReuse() { 973 synchronized (xaResourceSameRMGroup_) { int groupCount = xaResourceSameRMGroup_.size(); 978 int index = 0; 979 int firstFreeElement = -1; 980 NetXAResource xaResourceGroup = null; 981 982 for (; index < groupCount; ++index) { xaResourceGroup = (NetXAResource) xaResourceSameRMGroup_.elementAt(index); 984 if (xaResourceGroup == null) { if (firstFreeElement == -1) { firstFreeElement = index; 987 } 988 continue; } 990 try { 991 if (xaResourceGroup.isSameRM(this)) { NetXAResource nextXares = (NetXAResource) 993 xaResourceSameRMGroup_.elementAt(sameRMGroupIndex_); 994 while (nextXares != null) { if (nextXares.equals(this)) { break; 997 } 998 nextXares = nextXares.nextSameRM_; 1000 } 1001 1002 if (nextXares == null) { sameRMGroupIndex_ = index; 1005 this.nextSameRM_ = xaResourceGroup.nextSameRM_; 1006 xaResourceGroup.nextSameRM_ = this; 1007 } 1008 return; } 1010 } catch (XAException xae) { 1011 } 1012 } 1013 1014 if (firstFreeElement == -1) { xaResourceSameRMGroup_.add(this); 1017 sameRMGroupIndex_ = groupCount; 1018 } else { xaResourceSameRMGroup_.setElementAt(this, firstFreeElement); 1020 sameRMGroupIndex_ = firstFreeElement; 1021 } 1022 } 1023 } 1024} 1025 | Popular Tags |