Skip to content
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
22 changes: 16 additions & 6 deletions wrapper/src/main/java/software/amazon/jdbc/PluginServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -653,11 +653,16 @@ public void releaseResources() {
}

@Override
public boolean isNetworkException(final Throwable throwable) {
public boolean isNetworkException(Throwable throwable) {
return this.isNetworkException(throwable, this.targetDriverDialect);
}

@Override
public boolean isNetworkException(final Throwable throwable, @Nullable TargetDriverDialect targetDriverDialect) {
if (this.exceptionHandler != null) {
return this.exceptionHandler.isNetworkException(throwable);
return this.exceptionHandler.isNetworkException(throwable, targetDriverDialect);
}
return this.exceptionManager.isNetworkException(this.dialect, throwable);
return this.exceptionManager.isNetworkException(this.dialect, throwable, targetDriverDialect);
}

@Override
Expand All @@ -669,11 +674,16 @@ public boolean isNetworkException(final String sqlState) {
}

@Override
public boolean isLoginException(final Throwable throwable) {
public boolean isLoginException(Throwable throwable) {
return this.isLoginException(throwable, this.targetDriverDialect);
}

@Override
public boolean isLoginException(final Throwable throwable, @Nullable TargetDriverDialect targetDriverDialect) {
if (this.exceptionHandler != null) {
return this.exceptionHandler.isLoginException(throwable);
return this.exceptionHandler.isLoginException(throwable, targetDriverDialect);
}
return this.exceptionManager.isLoginException(this.dialect, throwable);
return this.exceptionManager.isLoginException(this.dialect, throwable, targetDriverDialect);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,32 @@

import java.sql.SQLException;
import java.util.List;
import org.checkerframework.checker.nullness.qual.Nullable;
import software.amazon.jdbc.targetdriverdialect.TargetDriverDialect;
import software.amazon.jdbc.util.StringUtils;

public abstract class AbstractPgExceptionHandler implements ExceptionHandler {
public abstract List<String> getNetworkErrors();

public abstract List<String> getAccessErrors();

@Override
public boolean isNetworkException(final Throwable throwable) {
public boolean isNetworkException(Throwable throwable) {
return this.isNetworkException(throwable, null);
}

@Override
public boolean isNetworkException(final Throwable throwable, @Nullable TargetDriverDialect targetDriverDialect) {
Throwable exception = throwable;

while (exception != null) {
if (exception instanceof SQLException) {
return isNetworkException(((SQLException) exception).getSQLState());
} else if (targetDriverDialect != null) {
String sqlState = targetDriverDialect.getSQLState(throwable);
if (!StringUtils.isNullOrEmpty(sqlState)) {
return isNetworkException(sqlState);
}
}

exception = exception.getCause();
Expand All @@ -56,6 +69,11 @@ public boolean isNetworkException(final String sqlState) {

@Override
public boolean isLoginException(final Throwable throwable) {
return this.isLoginException(throwable, null);
}

@Override
public boolean isLoginException(final Throwable throwable, @Nullable TargetDriverDialect targetDriverDialect) {
Throwable exception = throwable;

while (exception != null) {
Expand All @@ -66,6 +84,8 @@ public boolean isLoginException(final Throwable throwable) {
String sqlState = null;
if (exception instanceof SQLException) {
sqlState = ((SQLException) exception).getSQLState();
} else if (targetDriverDialect != null) {
sqlState = targetDriverDialect.getSQLState(throwable);
}

if (isLoginException(sqlState)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,32 @@

package software.amazon.jdbc.exceptions;

import org.checkerframework.checker.nullness.qual.Nullable;
import software.amazon.jdbc.targetdriverdialect.TargetDriverDialect;

public interface ExceptionHandler {

/**
* The method determines whether provided throwable is about any network issues.
*
* @deprecated Use similar method below that accepts throwable and target driver dialect.
*/
@Deprecated()
boolean isNetworkException(Throwable throwable);

boolean isNetworkException(Throwable throwable, @Nullable TargetDriverDialect targetDriverDialect);

boolean isNetworkException(String sqlState);

boolean isLoginException(String sqlState);

/**
* The method determines whether provided throwable is about any login or authentication issues.
*
* @deprecated Use similar method below that accepts throwable and target driver dialect.
*/
@Deprecated()
boolean isLoginException(Throwable throwable);

boolean isLoginException(Throwable throwable, @Nullable TargetDriverDialect targetDriverDialect);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import software.amazon.jdbc.Driver;
import software.amazon.jdbc.dialect.Dialect;
import software.amazon.jdbc.targetdriverdialect.TargetDriverDialect;

public class ExceptionManager {

Expand All @@ -41,19 +42,21 @@ public static void resetCustomHandler() {
Driver.resetCustomExceptionHandler();
}

public boolean isLoginException(final Dialect dialect, final Throwable throwable) {
public boolean isLoginException(
final Dialect dialect, final Throwable throwable, final TargetDriverDialect targetDriverDialect) {
final ExceptionHandler handler = getHandler(dialect);
return handler.isLoginException(throwable);
return handler.isLoginException(throwable, targetDriverDialect);
}

public boolean isLoginException(final Dialect dialect, final String sqlState) {
final ExceptionHandler handler = getHandler(dialect);
return handler.isLoginException(sqlState);
}

public boolean isNetworkException(final Dialect dialect, final Throwable throwable) {
public boolean isNetworkException(
final Dialect dialect, final Throwable throwable, final TargetDriverDialect targetDriverDialect) {
final ExceptionHandler handler = getHandler(dialect);
return handler.isNetworkException(throwable);
return handler.isNetworkException(throwable, targetDriverDialect);
}

public boolean isNetworkException(final Dialect dialect, final String sqlState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import org.checkerframework.checker.nullness.qual.Nullable;
import software.amazon.jdbc.targetdriverdialect.TargetDriverDialect;
import software.amazon.jdbc.util.StringUtils;

public class GenericExceptionHandler implements ExceptionHandler {

Expand All @@ -36,12 +39,22 @@ public class GenericExceptionHandler implements ExceptionHandler {
);

@Override
public boolean isNetworkException(final Throwable throwable) {
public boolean isNetworkException(Throwable throwable) {
return this.isNetworkException(throwable, null);
}

@Override
public boolean isNetworkException(final Throwable throwable, @Nullable TargetDriverDialect targetDriverDialect) {
Throwable exception = throwable;

while (exception != null) {
if (exception instanceof SQLException) {
return isNetworkException(((SQLException) exception).getSQLState());
} else if (targetDriverDialect != null) {
String sqlState = targetDriverDialect.getSQLState(throwable);
if (!StringUtils.isNullOrEmpty(sqlState)) {
return isNetworkException(sqlState);
}
}

exception = exception.getCause();
Expand All @@ -66,7 +79,12 @@ public boolean isNetworkException(final String sqlState) {
}

@Override
public boolean isLoginException(final Throwable throwable) {
public boolean isLoginException(Throwable throwable) {
return this.isLoginException(throwable, null);
}

@Override
public boolean isLoginException(final Throwable throwable, TargetDriverDialect targetDriverDialect) {
Throwable exception = throwable;

while (exception != null) {
Expand All @@ -77,6 +95,8 @@ public boolean isLoginException(final Throwable throwable) {
String sqlState = null;
if (exception instanceof SQLException) {
sqlState = ((SQLException) exception).getSQLState();
} else if (targetDriverDialect != null) {
sqlState = targetDriverDialect.getSQLState(throwable);
}

if (isLoginException(sqlState)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package software.amazon.jdbc.exceptions;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

package software.amazon.jdbc.exceptions;

import com.mysql.cj.exceptions.CJException;
import java.sql.SQLException;
import org.checkerframework.checker.nullness.qual.Nullable;
import software.amazon.jdbc.targetdriverdialect.TargetDriverDialect;
import software.amazon.jdbc.util.StringUtils;

public class MySQLExceptionHandler implements ExceptionHandler {
public static final String SQLSTATE_ACCESS_ERROR = "28000";
Expand All @@ -26,7 +28,12 @@ public class MySQLExceptionHandler implements ExceptionHandler {
"setNetworkTimeout cannot be called on a closed connection";

@Override
public boolean isNetworkException(final Throwable throwable) {
public boolean isNetworkException(Throwable throwable) {
return this.isNetworkException(throwable, null);
}

@Override
public boolean isNetworkException(final Throwable throwable, @Nullable TargetDriverDialect targetDriverDialect) {
Throwable exception = throwable;

while (exception != null) {
Expand All @@ -44,8 +51,11 @@ public boolean isNetworkException(final Throwable throwable) {
if (isNetworkException(sqlException.getSQLState()) || isHikariMariaDbNetworkException(sqlException)) {
return true;
}
} else if (exception instanceof CJException) {
return isNetworkException(((CJException) exception).getSQLState());
} else if (targetDriverDialect != null) {
String sqlState = targetDriverDialect.getSQLState(throwable);
if (!StringUtils.isNullOrEmpty(sqlState)) {
return isNetworkException(sqlState);
}
}

exception = exception.getCause();
Expand All @@ -64,7 +74,12 @@ public boolean isNetworkException(final String sqlState) {
}

@Override
public boolean isLoginException(final Throwable throwable) {
public boolean isLoginException(Throwable throwable) {
return this.isLoginException(throwable, null);
}

@Override
public boolean isLoginException(final Throwable throwable, @Nullable TargetDriverDialect targetDriverDialect) {
Throwable exception = throwable;

while (exception != null) {
Expand All @@ -75,8 +90,8 @@ public boolean isLoginException(final Throwable throwable) {
String sqlState = null;
if (exception instanceof SQLException) {
sqlState = ((SQLException) exception).getSQLState();
} else if (exception instanceof CJException) {
sqlState = ((CJException) exception).getSQLState();
} else if (targetDriverDialect != null) {
sqlState = targetDriverDialect.getSQLState(throwable);
}

if (isLoginException(sqlState)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ private Connection getVerifiedWriterConnection(

} catch (SQLException ex) {
this.closeConnection(writerCandidateConn);
if (this.pluginService.isLoginException(ex)) {
if (this.pluginService.isLoginException(ex, this.pluginService.getTargetDriverDialect())) {
throw WrapperUtils.wrapExceptionIfNeeded(SQLException.class, ex);
} else {
if (writerCandidate != null) {
Expand Down Expand Up @@ -322,7 +322,7 @@ private Connection getVerifiedReaderConnection(

} catch (SQLException ex) {
this.closeConnection(readerCandidateConn);
if (this.pluginService.isLoginException(ex)) {
if (this.pluginService.isLoginException(ex, this.pluginService.getTargetDriverDialect())) {
throw WrapperUtils.wrapExceptionIfNeeded(SQLException.class, ex);
} else {
if (readerCandidate != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ private Connection connectInternal(HostSpec hostSpec, Properties props,
return connectFunc.call();

} catch (final SQLException exception) {
if (this.pluginService.isLoginException(exception) && !secretWasFetched) {
if (this.pluginService.isLoginException(exception, this.pluginService.getTargetDriverDialect())
&& !secretWasFetched) {
// Login unsuccessful with cached credentials
// Try to re-fetch credentials and try again

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ private Connection connectInternal(String driverProtocol, HostSpec hostSpec, Pro
"IamAuthConnectionPlugin.connectException",
new Object[] {exception}));

if (!this.pluginService.isLoginException(exception) || !isCachedToken) {
if (!this.pluginService.isLoginException(exception, this.pluginService.getTargetDriverDialect())
|| !isCachedToken) {
throw exception;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,9 @@ public Set<String> getAllowedOnConnectionMethodNames() {
}
});
}

@Override
public String getSQLState(Throwable throwable) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package software.amazon.jdbc.targetdriverdialect;

import com.mysql.cj.exceptions.CJException;
import com.mysql.cj.jdbc.MysqlDataSource;
import java.sql.DriverManager;
import java.sql.SQLException;
Expand Down Expand Up @@ -92,4 +93,8 @@ public void registerDriver() throws SQLException {
throw new SQLException(Messages.get("MysqlConnectorJDriverHelper.canNotRegister"), e);
}
}

public String getSQLState(final Throwable throwable) {
return throwable instanceof CJException ? ((CJException) throwable).getSQLState() : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,10 @@ public Set<String> getAllowedOnConnectionMethodNames() {
}
});
}

@Override
public String getSQLState(Throwable throwable) {
final MysqlConnectorJDriverHelper helper = new MysqlConnectorJDriverHelper();
return helper.getSQLState(throwable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@ void prepareDataSource(
boolean ping(final @NonNull Connection connection);

Set<String> getAllowedOnConnectionMethodNames();

String getSQLState(final Throwable throwable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,11 @@ public boolean isNetworkException(Throwable throwable) {
return false;
}

@Override
public boolean isNetworkException(Throwable throwable, @Nullable TargetDriverDialect targetDriverDialect) {
return false;
}

@Override
public boolean isNetworkException(String sqlState) {
return false;
Expand All @@ -618,6 +623,11 @@ public boolean isLoginException(Throwable throwable) {
return false;
}

@Override
public boolean isLoginException(Throwable throwable, @Nullable TargetDriverDialect targetDriverDialect) {
return false;
}

@Override
public Dialect getDialect() {
return new UnknownDialect();
Expand Down
Loading