Skip to content

Commit

Permalink
Default to RMFAIL instead of RMERR (#2348)
Browse files Browse the repository at this point in the history
* Default to RMFAIL instead of RMERR

* Code review comments

* Code review comments p2
  • Loading branch information
tkyc committed Mar 19, 2024
1 parent 00a2556 commit 41710d2
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package com.microsoft.sqlserver.jdbc;

import java.net.SocketException;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
Expand All @@ -25,7 +24,6 @@
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;

import com.microsoft.sqlserver.jdbc.SQLServerError.TransientError;


/**
Expand Down Expand Up @@ -754,27 +752,13 @@ private XAReturnValue dtc_XA_interface(int nType, Xid xid, int xaFlags) throws X
}
}
}
} catch (SQLTimeoutException ex) {
} catch (SQLTimeoutException | SQLServerException ex) {
if (xaLogger.isLoggable(Level.FINER))
xaLogger.finer(toString() + " exception:" + ex);
XAException e = new XAException(ex.toString());
e.errorCode = XAException.XAER_RMFAIL;
throw e;

} catch (SQLServerException ex) {
if (xaLogger.isLoggable(Level.FINER))
xaLogger.finer(toString() + " exception:" + ex);

if (ex.getMessage().equals(SQLServerException.getErrString("R_noServerResponse"))
|| TransientError.isTransientError(ex.getSQLServerError()) || isResourceManagerFailure(ex)) {
XAException e = new XAException(ex.toString());
e.errorCode = XAException.XAER_RMFAIL;
throw e;
}

XAException e = new XAException(ex.toString());
e.errorCode = XAException.XAER_RMERR;
throw e;
}

if (xaLogger.isLoggable(Level.FINER))
Expand Down Expand Up @@ -945,68 +929,4 @@ private static int nextResourceID() {
return baseResourceID.incrementAndGet();
}

private enum ResourceManagerFailure {
CONN_RESET("Connection reset"),
CONN_RESET_BY_PEER("Connection reset by peer"),
CONN_TIMEOUT("Connection timed out"),
CONN_RESILIENCY_CLIENT_UNRECOVERABLE(SQLServerException.getErrString("R_crClientUnrecoverable"));

private final String errString;

ResourceManagerFailure(String errString) {
this.errString = errString;
}

@Override
public String toString() {
return errString;
}

static ResourceManagerFailure fromString(String errString) {
for (ResourceManagerFailure resourceManagerFailure : ResourceManagerFailure.values()) {
if (errString.equalsIgnoreCase(resourceManagerFailure.toString())) {
return resourceManagerFailure;
}
}
return null;
}
}

/**
* Check if the root exception of the throwable should be a XAER_RMFAIL exception
*
* @param throwable
* The exception to check if the root cause should be a XAER_RMFAIL
*
* @return True if XAER_RMFAIL, otherwise false
*/
private boolean isResourceManagerFailure(Throwable throwable) {
Throwable root = Util.getRootCause(throwable);

if (null == root) {
return false;
}

if (xaLogger.isLoggable(Level.FINE)) {
xaLogger.fine(toString() + " Resource manager failure root exception: " + root);
}

ResourceManagerFailure err = ResourceManagerFailure.fromString(root.getMessage());

if (null == err) {
return false;
}

// Add as needed here for future XAER_RMFAIL exceptions
switch (err) {
case CONN_RESET:
case CONN_RESET_BY_PEER:
case CONN_TIMEOUT:
case CONN_RESILIENCY_CLIENT_UNRECOVERABLE:
return true;
default:
return false;
}
}

}
19 changes: 0 additions & 19 deletions src/main/java/com/microsoft/sqlserver/jdbc/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import java.net.UnknownHostException;
import java.text.DecimalFormat;
import java.text.MessageFormat;
import java.util.List;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import java.util.Properties;
Expand Down Expand Up @@ -1046,23 +1044,6 @@ static char[] bytesToChars(byte[] bytes) {
}
return chars;
}

/**
* @param throwable
* The exception to find root cause for.
*
* @return The root cause of the exception, otherwise null if null throwable input
*/
static Throwable getRootCause(Throwable throwable) {
final List<Throwable> list = new ArrayList<>();

while (throwable != null && !list.contains(throwable)) {
list.add(throwable);
throwable = throwable.getCause();
}

return list.isEmpty() ? null : list.get(list.size() - 1);
}
}


Expand Down

0 comments on commit 41710d2

Please sign in to comment.