use of com.mysql.cj.exceptions.CJException in project aws-mysql-jdbc by awslabs.
the class AbstractQuery method checkCancelTimeout.
@Override
public void checkCancelTimeout() {
synchronized (this.cancelTimeoutMutex) {
if (this.cancelStatus != CancelStatus.NOT_CANCELED) {
CJException cause = this.cancelStatus == CancelStatus.CANCELED_BY_TIMEOUT ? new CJTimeoutException() : new OperationCancelledException();
resetCancelledState();
throw cause;
}
}
}
use of com.mysql.cj.exceptions.CJException in project aws-mysql-jdbc by awslabs.
the class ConnectionImpl method connectOneTryOnly.
private void connectOneTryOnly(boolean isForReconnect) throws SQLException {
Exception connectionNotEstablishedBecause = null;
try {
JdbcConnection c = getProxy();
this.session.connect(this.origHostInfo, this.user, this.password, this.database, getLoginTimeout(), c);
// save state from old connection
boolean oldAutoCommit = getAutoCommit();
int oldIsolationLevel = this.isolationLevel;
boolean oldReadOnly = isReadOnly(false);
String oldDb = getDatabase();
this.session.setQueryInterceptors(this.queryInterceptors);
// Server properties might be different from previous connection, so initialize again...
initializePropsFromServer();
if (isForReconnect) {
// Restore state from old connection
setAutoCommit(oldAutoCommit);
setTransactionIsolation(oldIsolationLevel);
setDatabase(oldDb);
setReadOnly(oldReadOnly);
}
return;
} catch (UnableToConnectException rejEx) {
close();
this.session.getProtocol().getSocketConnection().forceClose();
throw rejEx;
} catch (Exception EEE) {
if ((EEE instanceof PasswordExpiredException || EEE instanceof SQLException && ((SQLException) EEE).getErrorCode() == MysqlErrorNumbers.ER_MUST_CHANGE_PASSWORD) && !this.disconnectOnExpiredPasswords.getValue()) {
return;
}
if (this.session != null) {
this.session.forceClose();
}
connectionNotEstablishedBecause = EEE;
if (EEE instanceof SQLException) {
throw (SQLException) EEE;
}
if (EEE.getCause() != null && EEE.getCause() instanceof SQLException) {
throw (SQLException) EEE.getCause();
}
if (EEE instanceof CJException) {
throw (CJException) EEE;
}
SQLException chainedEx = SQLError.createSQLException(Messages.getString("Connection.UnableToConnect"), MysqlErrorNumbers.SQL_STATE_UNABLE_TO_CONNECT_TO_DATASOURCE, getExceptionInterceptor());
chainedEx.initCause(connectionNotEstablishedBecause);
throw chainedEx;
}
}
use of com.mysql.cj.exceptions.CJException in project aws-mysql-jdbc by awslabs.
the class ServerPreparedStatement method serverPrepare.
protected void serverPrepare(String sql) throws SQLException {
synchronized (checkClosed().getConnectionMutex()) {
SQLException t = null;
try {
ServerPreparedQuery q = (ServerPreparedQuery) this.query;
q.serverPrepare(sql);
} catch (IOException ioEx) {
t = SQLError.createCommunicationsException(this.connection, this.session.getProtocol().getPacketSentTimeHolder(), this.session.getProtocol().getPacketReceivedTimeHolder(), ioEx, this.exceptionInterceptor);
} catch (CJException sqlEx) {
SQLException ex = SQLExceptionsMapping.translateException(sqlEx);
if (this.dumpQueriesOnException.getValue()) {
StringBuilder messageBuf = new StringBuilder(((PreparedQuery<?>) this.query).getOriginalSql().length() + 32);
messageBuf.append("\n\nQuery being prepared when exception was thrown:\n\n");
messageBuf.append(((PreparedQuery<?>) this.query).getOriginalSql());
ex = appendMessageToException(ex, messageBuf.toString(), this.exceptionInterceptor);
}
t = ex;
} finally {
// Leave the I/O channel in a known state...there might be packets out there that we're not interested in
try {
this.session.clearInputStream();
} catch (Exception e) {
if (t == null) {
t = SQLError.createCommunicationsException(this.connection, this.session.getProtocol().getPacketSentTimeHolder(), this.session.getProtocol().getPacketReceivedTimeHolder(), e, this.exceptionInterceptor);
}
}
if (t != null) {
throw t;
}
}
}
}
use of com.mysql.cj.exceptions.CJException in project aws-mysql-jdbc by awslabs.
the class SessionFactory method getSession.
/**
* Creates {@link Session} by given URL.
*
* @param connUrl
* the session {@link ConnectionUrl}.
* @return a {@link Session} instance.
*/
protected Session getSession(ConnectionUrl connUrl) {
CJException latestException = null;
List<HostInfo> hostsList = connUrl.getHostsList();
for (HostInfo hi : hostsList) {
try {
return new SessionImpl(hi);
} catch (CJCommunicationsException e) {
if (e.getCause() == null) {
throw e;
}
latestException = e;
}
}
if (latestException != null) {
throw ExceptionFactory.createException(CJCommunicationsException.class, Messages.getString("Session.Create.Failover.0"), latestException);
}
return null;
}
use of com.mysql.cj.exceptions.CJException in project aws-mysql-jdbc by awslabs.
the class AuthenticationTest method authLdapSaslCliPluginChallengeMissingAttributes.
/**
* Test wrong 'server-first-message' due to missing attributes.
* Data based on test vector from <a href="https://tools.ietf.org/html/rfc5802#section-5">RFC 5802, Section 5</a>.
*
* @throws Exception
*/
@Test
public void authLdapSaslCliPluginChallengeMissingAttributes() throws Exception {
// Server's 'server-first-message' attributes:
String ar = "r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j";
String as = "s=QSXCR+Q6sek8bf92";
String ai = "i=4096";
for (int i = 0; i < 3; i++) {
AuthenticationPlugin<NativePacketPayload> authPlugin = new AuthenticationLdapSaslClientPlugin();
// Initialize plugin with some protocol (none is needed).
authPlugin.init(null);
// Set authentication parameters.
authPlugin.setAuthenticationParameters("user", "pencil");
// Initial server packet: Protocol::AuthSwitchRequest
// [authentication_ldap_sasl_client.SCRAM-SHA-1]
// ;; "." --> 0 byte.
// ;; first part of the packet is already processed.
NativePacketPayload challenge = new NativePacketPayload("SCRAM-SHA-1".getBytes("ASCII"));
// Expected 'client-first-message':
// [n,,n=user,r=<CNONCE>]
// ;; <CNONCE> is generated internally and needs to be replaced by the expected value from the test vector in order to continue the test.
List<NativePacketPayload> response = new ArrayList<>();
authPlugin.nextAuthenticationStep(challenge, response);
assertEquals(1, response.size());
String data = response.get(0).readString(StringSelfDataType.STRING_EOF, "UTF-8");
assertTrue(data.startsWith("n,,n=user,r="));
assertEquals("n,,n=user,r=".length() + 32, data.length());
// Server's 'server-first-message':
// [r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,s=QSXCR+Q6sek8bf92,i=4096]
// ;; But skip one of the attributes at a time.
String sfm = null;
switch(i) {
case 0:
sfm = String.join(",", as, ai);
break;
case 1:
sfm = String.join(",", ar, ai);
break;
case 2:
sfm = String.join(",", ar, as);
break;
}
NativePacketPayload badChallenge = new NativePacketPayload(sfm.getBytes("UTF-8"));
// Expect Exception.
CJException ex = assertThrows(CJException.class, "Error while processing an authentication iteration for the authentication mechanism 'SCRAM-SHA-1'\\.", () -> authPlugin.nextAuthenticationStep(badChallenge, response));
assertEquals(SaslException.class, ex.getCause().getClass());
assertEquals("Missing required SCRAM attribute from server first message.", ex.getCause().getMessage());
}
}
Aggregations