📄 netxaresource.java
字号:
default: xaExceptionText = "Unknown Error"; break; } return xaExceptionText; } protected void throwXAException(int rc, boolean resetFlag) throws XAException { // ~~~ String xaExceptionText; if (resetFlag) { // reset the state of the failed connection NetXACallInfo callInfo = callInfoArray_[conn_.currXACallInfoOffset_]; callInfo.xaInProgress_ = false; callInfo.xaWasSuspended = false; } xaExceptionText = getXAExceptionText(rc); // save the SqlException chain to add it to the XAException org.apache.derby.client.am.SqlException sqlExceptions = exceptionsOnXA; while (exceptionsOnXA != null) { // one or more SqlExceptions received, format them xaExceptionText = xaExceptionText + " : " + exceptionsOnXA.getMessage(); exceptionsOnXA = (org.apache.derby.client.am.SqlException) exceptionsOnXA.getNextException(); } org.apache.derby.client.am.XaException xaException = new org.apache.derby.client.am.XaException(conn_.agent_.logWriter_, sqlExceptions, xaExceptionText); xaException.errorCode = rc; setXaStateForXAException(rc); throw xaException; } /** * Reset the transaction branch association state to XA_T0_NOT_ASSOCIATED * for XAER_RM* and XA_RB* Exceptions. All other exeptions leave the state * unchanged * * @param rc // return code from XAException * @throws XAException */ private void setXaStateForXAException(int rc) { switch (rc) { // Reset to T0, not associated for XA_RB*, RM* // XAER_RMFAIL and XAER_RMERR will be fatal to the connection // but that is not dealt with here case javax.transaction.xa.XAException.XAER_RMFAIL: case javax.transaction.xa.XAException.XAER_RMERR: case javax.transaction.xa.XAException.XA_RBROLLBACK: case javax.transaction.xa.XAException.XA_RBCOMMFAIL: case javax.transaction.xa.XAException.XA_RBDEADLOCK: case javax.transaction.xa.XAException.XA_RBINTEGRITY: case javax.transaction.xa.XAException.XA_RBOTHER: case javax.transaction.xa.XAException.XA_RBPROTO: case javax.transaction.xa.XAException.XA_RBTIMEOUT: case javax.transaction.xa.XAException.XA_RBTRANSIENT: conn_.setXAState(Connection.XA_T0_NOT_ASSOCIATED); break; // No change for other XAExceptions // javax.transaction.xa.XAException.XA_NOMIGRATE //javax.transaction.xa.XAException.XA_HEURHAZ // javax.transaction.xa.XAException.XA_HEURCOM // javax.transaction.xa.XAException.XA_HEURRB // javax.transaction.xa.XAException.XA_HEURMIX // javax.transaction.xa.XAException.XA_RETRY // javax.transaction.xa.XAException.XA_RDONLY // javax.transaction.xa.XAException.XAER_ASYNC // javax.transaction.xa.XAException.XAER_NOTA // javax.transaction.xa.XAException.XAER_INVAL // javax.transaction.xa.XAException.XAER_PROTO // javax.transaction.xa.XAException.XAER_DUPID // javax.transaction.xa.XAException.XAER_OUTSIDE default: return; } } public boolean isSameRM(XAResource xares) throws XAException { boolean isSame = false; // preset that the RMs are NOT the same exceptionsOnXA = null; if (conn_.agent_.loggingEnabled()) { conn_.agent_.logWriter_.traceEntry(this, "isSameRM", xares); } if (conn_.isPhysicalConnClosed()) { connectionClosedFailure(); } if (xares instanceof org.apache.derby.client.net.NetXAResource) { // both are NetXAResource so check to see if this is the same RM // remember, isSame is initialized to false NetXAResource derbyxares = (NetXAResource) xares; while (true) { if (!conn_.databaseName_.equalsIgnoreCase(derbyxares.conn_.databaseName_)) { break; // database names are not equal, not same RM } if (!conn_.netAgent_.server_.equalsIgnoreCase (derbyxares.conn_.netAgent_.server_)) { // server name strings not equal, compare IP addresses try { // 1st convert "localhost" to actual server name String server1 = this.processLocalHost(conn_.netAgent_.server_); String server2 = this.processLocalHost(derbyxares.conn_.netAgent_.server_); // now convert the server name to ip address InetAddress serverIP1 = InetAddress.getByName(server1); InetAddress serverIP2 = InetAddress.getByName(server2); if (!serverIP1.equals(serverIP2)) { break; // server IPs are not equal, not same RM } } catch (UnknownHostException ue) { break; } } if (conn_.netAgent_.port_ != derbyxares.conn_.netAgent_.port_) { break; // ports are not equal, not same RM } isSame = true; // everything the same, set RMs are the same break; } } if (conn_.agent_.loggingEnabled()) { conn_.agent_.logWriter_.traceExit (this, "isSameRM", isSame); } return isSame; } public static boolean xidsEqual(Xid xid1, Xid xid2) { // determine if the 2 xids contain the same values even if not same object // comapre the format ids if (xid1.getFormatId() != xid2.getFormatId()) { return false; // format ids are not the same } // compare the global transaction ids int xid1Length = xid1.getGlobalTransactionId().length; if (xid1Length != xid2.getGlobalTransactionId().length) { return false; // length of the global trans ids are not the same } byte[] xid1Bytes = xid1.getGlobalTransactionId(); byte[] xid2Bytes = xid2.getGlobalTransactionId(); int i; for (i = 0; i < xid1Length; ++i) { // check all bytes are the same if (xid1Bytes[i] != xid2Bytes[i]) { return false; // bytes in the global trans ids are not the same } } // compare the branch qualifiers xid1Length = xid1.getBranchQualifier().length; if (xid1Length != xid2.getBranchQualifier().length) { return false; // length of the global trans ids are not the same } xid1Bytes = xid1.getBranchQualifier(); xid2Bytes = xid2.getBranchQualifier(); for (i = 0; i < xid1Length; ++i) { // check all bytes are the same if (xid1Bytes[i] != xid2Bytes[i]) { return false; // bytes in the global trans ids are not the same } } return true; // all of the fields are the same, xid1 == xid2 } public List getSpecialRegisters() { return specialRegisters_; } public void addSpecialRegisters(String s) { if (s.substring(0, 1).equals("@")) { // SET statement is coming from Client if (specialRegisters_.remove(s.substring(1))) { specialRegisters_.remove(s); specialRegisters_.add(s.substring(1)); } else { specialRegisters_.remove(s); specialRegisters_.add(s); } } else { // SET statement is coming from Server specialRegisters_.remove(s); specialRegisters_.add(s); } } private void connectionClosedFailure() throws XAException { // throw an XAException XAER_RMFAIL, with a chained SqlException - closed exceptionsOnXA = org.apache.derby.client.am.Utils.accumulateSQLException (new SqlException(null, "Connection is Closed."), exceptionsOnXA); throwXAException(javax.transaction.xa.XAException.XAER_RMFAIL); } private String getXAFuncStr(int xaFunc) { switch (xaFunc) { case XAFUNC_COMMIT: return XAFUNCSTR_COMMIT; case XAFUNC_END: return XAFUNCSTR_END; case XAFUNC_FORGET: return XAFUNCSTR_FORGET; case XAFUNC_PREPARE: return XAFUNCSTR_PREPARE; case XAFUNC_RECOVER: return XAFUNCSTR_RECOVER; case XAFUNC_ROLLBACK: return XAFUNCSTR_ROLLBACK; case XAFUNC_START: return XAFUNCSTR_START; } return XAFUNCSTR_NONE; } protected int xaRetValErrorAccumSQL(NetXACallInfo callInfo, int currentRC) { // xaRetVal_ is set by the server to be one of the // standard constants from XAException. int rc = callInfo.xaRetVal_; if (rc != XAResource.XA_OK) { // error was detected // create an SqlException to report this error within String xaRetValStr = "Error executing a " + getXAFuncStr(callInfo.xaFunction_) + ", " + "Server returned " + getXAExceptionText(rc); SqlException accumSql = new SqlException(conn_.netAgent_.logWriter_, xaRetValStr, org.apache.derby.client.am.SqlState.undefined, org.apache.derby.client.am.SqlCode.queuedXAError); exceptionsOnXA = org.apache.derby.client.am.Utils.accumulateSQLException (accumSql, exceptionsOnXA); if (currentRC != XAResource.XA_OK) { // the rc passed into this function had an error also, prioritize error if (currentRC < 0) { // rc passed in was a major error use it instead of current error return currentRC; } } } return rc; } private String processLocalHost(String serverName) { if (serverName.equalsIgnoreCase("localhost")) { // this is a localhost, find hostname try { InetAddress localhostNameIA = InetAddress.getLocalHost(); String localhostName = localhostNameIA.getHostName(); return localhostName; } catch (SecurityException se) { return serverName; } catch (UnknownHostException ue) { return serverName; } } // not "localhost", return original server name return serverName; } protected void removeXaresFromSameRMchain() { // check all NetXAResources on the same RM for the NetXAResource to remove try { this.ignoreMe_ = true; // use the ignoreMe_ flag to indicate the // XAResource to remove NetXAResource prevXAResource = null; NetXAResource currXAResource; synchronized (xaResourceSameRMGroup_) { // make sure no one changes this vector list currXAResource = (NetXAResource) xaResourceSameRMGroup_.elementAt(sameRMGroupIndex_); while (currXAResource != null) { // is this the XAResource to remove? if (currXAResource.ignoreMe_) { // this NetXAResource is the one to remove if (prevXAResource != null) { // this XAResource is not first in chain, just move next to prev prevXAResource.nextSameRM_ = currXAResource.nextSameRM_; } else { // this XAResource is first in chain, just move next to root xaResourceSameRMGroup_.set(sameRMGroupIndex_, currXAResource.nextSameRM_); } return; } // this is not the NetXAResource to remove, try the next one prevXAResource = currXAResource; currXAResource = currXAResource.nextSameRM_; } } } finally { this.ignoreMe_ = false; } } public void initForReuse() { // add this new XAResource to the list of other XAResources for the Same RM // first find out if there are any other XAResources for the same RM // then check to make sure it is not already in the chain synchronized (xaResourceSameRMGroup_) { // make sure no one changes this vector list int groupCount = xaResourceSameRMGroup_.size(); int index = 0; int firstFreeElement = -1; NetXAResource xaResourceGroup = null; for (; index < groupCount; ++index) { // check if this group is the same RM xaResourceGroup = (NetXAResource) xaResourceSameRMGroup_.elementAt(index); if (xaResourceGroup == null) { // this is a free element, save its index if first found if (firstFreeElement == -1) { // first free element, save index firstFreeElement = index; } continue; // go to next element } try { if (xaResourceGroup.isSameRM(this)) { // it is the same RM add this XAResource to the chain if not there NetXAResource nextXares = (NetXAResource) xaResourceSameRMGroup_.elementAt(sameRMGroupIndex_); while (nextXares != null) { // is this NetXAResource the one we are trying to add? if (nextXares.equals(this)) { // the XAResource to be added already is in chain, don't add break; } // Xid was not on that NetXAResource, try the next one nextXares = nextXares.nextSameRM_; } if (nextXares == null) { // XAResource to be added is not in the chain already, add it // add it at the head of the chain sameRMGroupIndex_ = index; this.nextSameRM_ = xaResourceGroup.nextSameRM_; xaResourceGroup.nextSameRM_ = this; } return; // done } } catch (XAException xae) { } } // no other same RM was found, add this as first of new group if (firstFreeElement == -1) { // no free element found, add new element to end xaResourceSameRMGroup_.add(this); sameRMGroupIndex_ = groupCount; } else { // use first free element found xaResourceSameRMGroup_.setElementAt(this, firstFreeElement); sameRMGroupIndex_ = firstFreeElement; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -