Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default to RMFAIL instead of RMERR #2348

Merged
merged 3 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading