Search in sources :

Example 16 with CJException

use of com.mysql.cj.exceptions.CJException in project aws-mysql-jdbc by awslabs.

the class NativeProtocol method explainSlowQuery.

/**
 * Runs an 'EXPLAIN' on the given query and dumps the results to the log
 *
 * @param query
 *            full query string
 * @param truncatedQuery
 *            query string truncated for profiling
 */
public void explainSlowQuery(String query, String truncatedQuery) {
    if (StringUtils.startsWithIgnoreCaseAndWs(truncatedQuery, EXPLAINABLE_STATEMENT) || (versionMeetsMinimum(5, 6, 3) && StringUtils.startsWithIgnoreCaseAndWs(truncatedQuery, EXPLAINABLE_STATEMENT_EXTENSION) != -1)) {
        try {
            NativePacketPayload resultPacket = sendCommand(getCommandBuilder().buildComQuery(getSharedSendPacket(), "EXPLAIN " + query), false, 0);
            Resultset rs = readAllResults(-1, false, resultPacket, false, null, new ResultsetFactory(Type.FORWARD_ONLY, null));
            StringBuilder explainResults = new StringBuilder(Messages.getString("Protocol.6"));
            explainResults.append(truncatedQuery);
            explainResults.append(Messages.getString("Protocol.7"));
            appendResultSetSlashGStyle(explainResults, rs);
            this.log.logWarn(explainResults.toString());
        } catch (CJException sqlEx) {
            throw sqlEx;
        } catch (Exception ex) {
            throw ExceptionFactory.createException(ex.getMessage(), ex, getExceptionInterceptor());
        }
    }
}
Also used : Resultset(com.mysql.cj.protocol.Resultset) CJException(com.mysql.cj.exceptions.CJException) WrongArgumentException(com.mysql.cj.exceptions.WrongArgumentException) CJCommunicationsException(com.mysql.cj.exceptions.CJCommunicationsException) InvalidPathException(java.nio.file.InvalidPathException) CJConnectionFeatureNotAvailableException(com.mysql.cj.exceptions.CJConnectionFeatureNotAvailableException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) PasswordExpiredException(com.mysql.cj.exceptions.PasswordExpiredException) CJPacketTooBigException(com.mysql.cj.exceptions.CJPacketTooBigException) URISyntaxException(java.net.URISyntaxException) DataTruncationException(com.mysql.cj.exceptions.DataTruncationException) FeatureNotAvailableException(com.mysql.cj.exceptions.FeatureNotAvailableException) MalformedURLException(java.net.MalformedURLException) CJOperationNotSupportedException(com.mysql.cj.exceptions.CJOperationNotSupportedException) ClosedOnExpiredPasswordException(com.mysql.cj.exceptions.ClosedOnExpiredPasswordException) CJException(com.mysql.cj.exceptions.CJException)

Example 17 with CJException

use of com.mysql.cj.exceptions.CJException in project aws-mysql-jdbc by awslabs.

the class NativeProtocol method appendDeadlockStatusInformation.

private void appendDeadlockStatusInformation(Session sess, String xOpen, StringBuilder errorBuf) {
    if (sess.getPropertySet().getBooleanProperty(PropertyKey.includeInnodbStatusInDeadlockExceptions).getValue() && xOpen != null && (xOpen.startsWith("40") || xOpen.startsWith("41")) && getStreamingData() == null) {
        try {
            NativePacketPayload resultPacket = sendCommand(getCommandBuilder().buildComQuery(getSharedSendPacket(), "SHOW ENGINE INNODB STATUS"), false, 0);
            Resultset rs = readAllResults(-1, false, resultPacket, false, null, new ResultsetFactory(Type.FORWARD_ONLY, null));
            int colIndex = 0;
            Field f = null;
            for (int i = 0; i < rs.getColumnDefinition().getFields().length; i++) {
                f = rs.getColumnDefinition().getFields()[i];
                if ("Status".equals(f.getName())) {
                    colIndex = i;
                    break;
                }
            }
            ValueFactory<String> vf = new StringValueFactory(this.propertySet);
            Row r;
            if ((r = rs.getRows().next()) != null) {
                errorBuf.append("\n\n").append(r.getValue(colIndex, vf));
            } else {
                errorBuf.append("\n\n").append(Messages.getString("MysqlIO.NoInnoDBStatusFound"));
            }
        } catch (IOException | CJException ex) {
            errorBuf.append("\n\n").append(Messages.getString("MysqlIO.InnoDBStatusFailed")).append("\n\n").append(Util.stackTraceToString(ex));
        }
    }
    if (sess.getPropertySet().getBooleanProperty(PropertyKey.includeThreadDumpInDeadlockExceptions).getValue()) {
        errorBuf.append("\n\n*** Java threads running at time of deadlock ***\n\n");
        ThreadMXBean threadMBean = ManagementFactory.getThreadMXBean();
        long[] threadIds = threadMBean.getAllThreadIds();
        ThreadInfo[] threads = threadMBean.getThreadInfo(threadIds, Integer.MAX_VALUE);
        List<ThreadInfo> activeThreads = new ArrayList<>();
        for (ThreadInfo info : threads) {
            if (info != null) {
                activeThreads.add(info);
            }
        }
        for (ThreadInfo threadInfo : activeThreads) {
            // "Thread-60" daemon prio=1 tid=0x093569c0 nid=0x1b99 in Object.wait()
            errorBuf.append('"').append(threadInfo.getThreadName()).append("\" tid=").append(threadInfo.getThreadId()).append(" ").append(threadInfo.getThreadState());
            if (threadInfo.getLockName() != null) {
                errorBuf.append(" on lock=").append(threadInfo.getLockName());
            }
            if (threadInfo.isSuspended()) {
                errorBuf.append(" (suspended)");
            }
            if (threadInfo.isInNative()) {
                errorBuf.append(" (running in native)");
            }
            StackTraceElement[] stackTrace = threadInfo.getStackTrace();
            if (stackTrace.length > 0) {
                errorBuf.append(" in ");
                errorBuf.append(stackTrace[0].getClassName()).append(".");
                errorBuf.append(stackTrace[0].getMethodName()).append("()");
            }
            errorBuf.append("\n");
            if (threadInfo.getLockOwnerName() != null) {
                errorBuf.append("\t owned by ").append(threadInfo.getLockOwnerName()).append(" Id=").append(threadInfo.getLockOwnerId()).append("\n");
            }
            for (int j = 0; j < stackTrace.length; j++) {
                StackTraceElement ste = stackTrace[j];
                errorBuf.append("\tat ").append(ste.toString()).append("\n");
            }
        }
    }
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) StringValueFactory(com.mysql.cj.result.StringValueFactory) ArrayList(java.util.ArrayList) LazyString(com.mysql.cj.util.LazyString) IOException(java.io.IOException) Field(com.mysql.cj.result.Field) ThreadInfo(java.lang.management.ThreadInfo) Resultset(com.mysql.cj.protocol.Resultset) ResultsetRow(com.mysql.cj.protocol.ResultsetRow) Row(com.mysql.cj.result.Row) CJException(com.mysql.cj.exceptions.CJException)

Example 18 with CJException

use of com.mysql.cj.exceptions.CJException in project aws-mysql-jdbc by awslabs.

the class NativeProtocol method sendQueryPacket.

/**
 * Send a query stored in a packet to the server.
 *
 * @param <T>
 *            extends {@link Resultset}
 * @param callingQuery
 *            {@link Query}
 * @param queryPacket
 *            {@link NativePacketPayload} containing query
 * @param maxRows
 *            rows limit
 * @param streamResults
 *            whether a stream result should be created
 * @param cachedMetadata
 *            use this metadata instead of the one provided on wire
 * @param resultSetFactory
 *            {@link ProtocolEntityFactory}
 * @return T instance
 * @throws IOException
 *             if an i/o error occurs
 */
public final <T extends Resultset> T sendQueryPacket(Query callingQuery, NativePacketPayload queryPacket, int maxRows, boolean streamResults, ColumnDefinition cachedMetadata, ProtocolEntityFactory<T, NativePacketPayload> resultSetFactory) throws IOException {
    final long queryStartTime = getCurrentTimeNanosOrMillis();
    this.statementExecutionDepth++;
    byte[] queryBuf = queryPacket.getByteBuffer();
    // save the packet position
    int oldPacketPosition = queryPacket.getPosition();
    int queryPosition = queryPacket.getTag("QUERY");
    LazyString query = new LazyString(queryBuf, queryPosition, (oldPacketPosition - queryPosition));
    try {
        if (this.queryInterceptors != null) {
            T interceptedResults = invokeQueryInterceptorsPre(query, callingQuery, false);
            if (interceptedResults != null) {
                return interceptedResults;
            }
        }
        if (this.autoGenerateTestcaseScript) {
            StringBuilder debugBuf = new StringBuilder(query.length() + 32);
            generateQueryCommentBlock(debugBuf);
            debugBuf.append(query);
            debugBuf.append(';');
            TestUtils.dumpTestcaseQuery(debugBuf.toString());
        }
        // Send query command and sql query string
        NativePacketPayload resultPacket = sendCommand(queryPacket, false, 0);
        final long queryEndTime = getCurrentTimeNanosOrMillis();
        final long queryDuration = queryEndTime - queryStartTime;
        if (callingQuery != null) {
            callingQuery.setExecuteTime(queryDuration);
        }
        boolean queryWasSlow = this.logSlowQueries && (this.useAutoSlowLog ? this.metricsHolder.checkAbonormallyLongQuery(queryDuration) : queryDuration > this.propertySet.getIntegerProperty(PropertyKey.slowQueryThresholdMillis).getValue());
        long fetchBeginTime = this.profileSQL ? getCurrentTimeNanosOrMillis() : 0L;
        T rs = readAllResults(maxRows, streamResults, resultPacket, false, cachedMetadata, resultSetFactory);
        if (this.profileSQL || queryWasSlow) {
            long fetchEndTime = this.profileSQL ? getCurrentTimeNanosOrMillis() : 0L;
            // Extract the actual query from the network packet
            boolean truncated = oldPacketPosition - queryPosition > this.maxQuerySizeToLog.getValue();
            int extractPosition = truncated ? this.maxQuerySizeToLog.getValue() + queryPosition : oldPacketPosition;
            String extractedQuery = StringUtils.toString(queryBuf, queryPosition, (extractPosition - queryPosition));
            if (truncated) {
                extractedQuery += Messages.getString("Protocol.2");
            }
            ProfilerEventHandler eventSink = this.session.getProfilerEventHandler();
            if (this.logSlowQueries) {
                if (queryWasSlow) {
                    eventSink.processEvent(ProfilerEvent.TYPE_SLOW_QUERY, this.session, callingQuery, rs, queryDuration, new Throwable(), Messages.getString("Protocol.SlowQuery", new Object[] { this.useAutoSlowLog ? " 95% of all queries " : String.valueOf(this.slowQueryThreshold), this.queryTimingUnits, Long.valueOf(queryDuration), extractedQuery }));
                    if (this.propertySet.getBooleanProperty(PropertyKey.explainSlowQueries).getValue()) {
                        if (oldPacketPosition - queryPosition < MAX_QUERY_SIZE_TO_EXPLAIN) {
                            // skip until the query is located in the packet
                            queryPacket.setPosition(queryPosition);
                            explainSlowQuery(query.toString(), extractedQuery);
                        } else {
                            this.log.logWarn(Messages.getString("Protocol.3", new Object[] { MAX_QUERY_SIZE_TO_EXPLAIN }));
                        }
                    }
                }
                if (this.serverSession.noGoodIndexUsed()) {
                    eventSink.processEvent(ProfilerEvent.TYPE_SLOW_QUERY, this.session, callingQuery, rs, queryDuration, new Throwable(), Messages.getString("Protocol.4") + extractedQuery);
                }
                if (this.serverSession.noIndexUsed()) {
                    eventSink.processEvent(ProfilerEvent.TYPE_SLOW_QUERY, this.session, callingQuery, rs, queryDuration, new Throwable(), Messages.getString("Protocol.5") + extractedQuery);
                }
                if (this.serverSession.queryWasSlow()) {
                    eventSink.processEvent(ProfilerEvent.TYPE_SLOW_QUERY, this.session, callingQuery, rs, queryDuration, new Throwable(), Messages.getString("Protocol.ServerSlowQuery") + extractedQuery);
                }
            }
            if (this.profileSQL) {
                eventSink.processEvent(ProfilerEvent.TYPE_QUERY, this.session, callingQuery, rs, queryDuration, new Throwable(), extractedQuery);
                eventSink.processEvent(ProfilerEvent.TYPE_FETCH, this.session, callingQuery, rs, (fetchEndTime - fetchBeginTime), new Throwable(), null);
            }
        }
        if (this.hadWarnings) {
            scanForAndThrowDataTruncation();
        }
        if (this.queryInterceptors != null) {
            rs = invokeQueryInterceptorsPost(query, callingQuery, rs, false);
        }
        return rs;
    } catch (CJException sqlEx) {
        if (this.queryInterceptors != null) {
            // TODO why doing this?
            // we don't do anything with the result set in this case
            invokeQueryInterceptorsPost(query, callingQuery, null, false);
        }
        if (callingQuery != null) {
            callingQuery.checkCancelTimeout();
        }
        throw sqlEx;
    } finally {
        this.statementExecutionDepth--;
    }
}
Also used : ProfilerEventHandler(com.mysql.cj.log.ProfilerEventHandler) LazyString(com.mysql.cj.util.LazyString) LazyString(com.mysql.cj.util.LazyString) CJException(com.mysql.cj.exceptions.CJException)

Example 19 with CJException

use of com.mysql.cj.exceptions.CJException in project aws-mysql-jdbc by awslabs.

the class CachingSha2PasswordPlugin method nextAuthenticationStep.

@Override
public boolean nextAuthenticationStep(NativePacketPayload fromServer, List<NativePacketPayload> toServer) {
    toServer.clear();
    if (this.password == null || this.password.length() == 0 || fromServer == null) {
        // no password
        NativePacketPayload bresp = new NativePacketPayload(new byte[] { 0 });
        toServer.add(bresp);
    } else {
        try {
            if (this.stage == AuthStage.FAST_AUTH_SEND_SCRAMBLE) {
                // send a scramble for fast auth
                this.seed = fromServer.readString(StringSelfDataType.STRING_TERM, null);
                toServer.add(new NativePacketPayload(Security.scrambleCachingSha2(StringUtils.getBytes(this.password, this.protocol.getServerSession().getCharsetSettings().getPasswordCharacterEncoding()), this.seed.getBytes())));
                this.stage = AuthStage.FAST_AUTH_READ_RESULT;
                return true;
            } else if (this.stage == AuthStage.FAST_AUTH_READ_RESULT) {
                int fastAuthResult = fromServer.readBytes(StringLengthDataType.STRING_FIXED, 1)[0];
                switch(fastAuthResult) {
                    case 3:
                        this.stage = AuthStage.FAST_AUTH_COMPLETE;
                        return true;
                    case 4:
                        this.stage = AuthStage.FULL_AUTH;
                        break;
                    default:
                        throw ExceptionFactory.createException("Unknown server response after fast auth.", this.protocol.getExceptionInterceptor());
                }
            }
            if (this.protocol.getSocketConnection().isSSLEstablished()) {
                // allow plain text over SSL
                NativePacketPayload bresp = new NativePacketPayload(StringUtils.getBytes(this.password, this.protocol.getServerSession().getCharsetSettings().getPasswordCharacterEncoding()));
                bresp.setPosition(bresp.getPayloadLength());
                bresp.writeInteger(IntegerDataType.INT1, 0);
                bresp.setPosition(0);
                toServer.add(bresp);
            } else if (this.serverRSAPublicKeyFile.getValue() != null) {
                // encrypt with given key, don't use "Public Key Retrieval"
                NativePacketPayload bresp = new NativePacketPayload(encryptPassword());
                toServer.add(bresp);
            } else {
                if (!this.protocol.getPropertySet().getBooleanProperty(PropertyKey.allowPublicKeyRetrieval).getValue()) {
                    throw ExceptionFactory.createException(UnableToConnectException.class, Messages.getString("Sha256PasswordPlugin.2"), this.protocol.getExceptionInterceptor());
                }
                // We must request the public key from the server to encrypt the password
                if (this.publicKeyRequested && fromServer.getPayloadLength() > NativeConstants.SEED_LENGTH + 1) {
                    // auth data is null terminated
                    // Servers affected by Bug#70865 could send Auth Switch instead of key after Public Key Retrieval,
                    // so we check payload length to detect that.
                    // read key response
                    this.publicKeyString = fromServer.readString(StringSelfDataType.STRING_TERM, null);
                    NativePacketPayload bresp = new NativePacketPayload(encryptPassword());
                    toServer.add(bresp);
                    this.publicKeyRequested = false;
                } else {
                    // build and send Public Key Retrieval packet
                    // was 1 in sha256_password
                    NativePacketPayload bresp = new NativePacketPayload(new byte[] { 2 });
                    toServer.add(bresp);
                    this.publicKeyRequested = true;
                }
            }
        } catch (CJException | DigestException e) {
            throw ExceptionFactory.createException(e.getMessage(), e, this.protocol.getExceptionInterceptor());
        }
    }
    return true;
}
Also used : DigestException(java.security.DigestException) NativePacketPayload(com.mysql.cj.protocol.a.NativePacketPayload) UnableToConnectException(com.mysql.cj.exceptions.UnableToConnectException) CJException(com.mysql.cj.exceptions.CJException)

Example 20 with CJException

use of com.mysql.cj.exceptions.CJException in project aws-mysql-jdbc by awslabs.

the class ServerPreparedStatement method realClose.

@Override
public void realClose(boolean calledExplicitly, boolean closeOpenResults) throws SQLException {
    JdbcConnection locallyScopedConn = this.connection;
    if (locallyScopedConn == null) {
        // already closed
        return;
    }
    synchronized (locallyScopedConn.getConnectionMutex()) {
        if (this.connection != null) {
            // 
            // Don't communicate with the server if we're being called from the finalizer...
            // 
            // This will leak server resources, but if we don't do this, we'll deadlock (potentially, because there's no guarantee when, what order, and
            // what concurrency finalizers will be called with). Well-behaved programs won't rely on finalizers to clean up their statements.
            // 
            CJException exceptionDuringClose = null;
            if (this.isCached) {
                locallyScopedConn.decachePreparedStatement(this);
                this.isCached = false;
            }
            super.realClose(calledExplicitly, closeOpenResults);
            ((ServerPreparedQuery) this.query).clearParameters(false);
            // Finally deallocate the prepared statement.
            if (calledExplicitly && !locallyScopedConn.isClosed()) {
                synchronized (locallyScopedConn.getConnectionMutex()) {
                    try {
                        ((NativeSession) locallyScopedConn.getSession()).sendCommand(this.commandBuilder.buildComStmtClose(null, ((ServerPreparedQuery) this.query).getServerStatementId()), true, 0);
                    } catch (CJException sqlEx) {
                        exceptionDuringClose = sqlEx;
                    }
                }
            }
            if (exceptionDuringClose != null) {
                throw exceptionDuringClose;
            }
        }
    }
}
Also used : NativeSession(com.mysql.cj.NativeSession) ServerPreparedQuery(com.mysql.cj.ServerPreparedQuery) CJException(com.mysql.cj.exceptions.CJException)

Aggregations

CJException (com.mysql.cj.exceptions.CJException)21 NativePacketPayload (com.mysql.cj.protocol.a.NativePacketPayload)9 ArrayList (java.util.ArrayList)6 AuthenticationLdapSaslClientPlugin (com.mysql.cj.protocol.a.authentication.AuthenticationLdapSaslClientPlugin)5 Test (org.junit.jupiter.api.Test)5 CJCommunicationsException (com.mysql.cj.exceptions.CJCommunicationsException)4 IOException (java.io.IOException)4 SQLException (java.sql.SQLException)4 UnableToConnectException (com.mysql.cj.exceptions.UnableToConnectException)3 WrongArgumentException (com.mysql.cj.exceptions.WrongArgumentException)3 ServerPreparedQuery (com.mysql.cj.ServerPreparedQuery)2 OperationCancelledException (com.mysql.cj.exceptions.OperationCancelledException)2 PasswordExpiredException (com.mysql.cj.exceptions.PasswordExpiredException)2 Resultset (com.mysql.cj.protocol.Resultset)2 LazyString (com.mysql.cj.util.LazyString)2 Properties (java.util.Properties)2 NativeSession (com.mysql.cj.NativeSession)1 PreparedQuery (com.mysql.cj.PreparedQuery)1 UsernameCallback (com.mysql.cj.callback.UsernameCallback)1 HostInfo (com.mysql.cj.conf.HostInfo)1