Search in sources :

Example 1 with NativePacketPayload

use of com.mysql.cj.protocol.a.NativePacketPayload in project ABC by RuiPinto96274.

the class NativeSession method getProcessHost.

public String getProcessHost() {
    try {
        long threadId = getThreadId();
        String processHost = findProcessHost(threadId);
        if (processHost == null) {
            // http://bugs.mysql.com/bug.php?id=44167 - connection ids on the wire wrap at 4 bytes even though they're 64-bit numbers
            this.log.logWarn(String.format("Connection id %d not found in \"SHOW PROCESSLIST\", assuming 32-bit overflow, using SELECT CONNECTION_ID() instead", threadId));
            NativePacketPayload resultPacket = sendCommand(this.commandBuilder.buildComQuery(null, "SELECT CONNECTION_ID()"), false, 0);
            Resultset rs = ((NativeProtocol) this.protocol).readAllResults(-1, false, resultPacket, false, null, new ResultsetFactory(Type.FORWARD_ONLY, null));
            ValueFactory<Long> lvf = new LongValueFactory(getPropertySet());
            Row r;
            if ((r = rs.getRows().next()) != null) {
                threadId = r.getValue(0, lvf);
                processHost = findProcessHost(threadId);
            } else {
                this.log.logError("No rows returned for statement \"SELECT CONNECTION_ID()\", local connection check will most likely be incorrect");
            }
        }
        if (processHost == null) {
            this.log.logWarn(String.format("Cannot find process listing for connection %d in SHOW PROCESSLIST output, unable to determine if locally connected", threadId));
        }
        return processHost;
    } catch (IOException e) {
        throw ExceptionFactory.createException(e.getMessage(), e);
    }
}
Also used : LongValueFactory(com.mysql.cj.result.LongValueFactory) Resultset(com.mysql.cj.protocol.Resultset) NativeProtocol(com.mysql.cj.protocol.a.NativeProtocol) Row(com.mysql.cj.result.Row) IOException(java.io.IOException) NativePacketPayload(com.mysql.cj.protocol.a.NativePacketPayload) ResultsetFactory(com.mysql.cj.protocol.a.ResultsetFactory)

Example 2 with NativePacketPayload

use of com.mysql.cj.protocol.a.NativePacketPayload in project ABC by RuiPinto96274.

the class NativeSession method findProcessHost.

private String findProcessHost(long threadId) {
    try {
        String processHost = null;
        String ps = this.protocol.getServerSession().getServerVariable("performance_schema");
        NativePacketPayload resultPacket = // performance_schema.threads in MySQL 5.5 does not contain PROCESSLIST_HOST column
        versionMeetsMinimum(5, 6, 0) && ps != null && ("1".contentEquals(ps) || "ON".contentEquals(ps)) ? sendCommand(this.commandBuilder.buildComQuery(null, "select PROCESSLIST_ID, PROCESSLIST_USER, PROCESSLIST_HOST from performance_schema.threads where PROCESSLIST_ID=" + threadId), false, 0) : sendCommand(this.commandBuilder.buildComQuery(null, "SHOW PROCESSLIST"), false, 0);
        Resultset rs = ((NativeProtocol) this.protocol).readAllResults(-1, false, resultPacket, false, null, new ResultsetFactory(Type.FORWARD_ONLY, null));
        ValueFactory<Long> lvf = new LongValueFactory(getPropertySet());
        ValueFactory<String> svf = new StringValueFactory(this.propertySet);
        Row r;
        while ((r = rs.getRows().next()) != null) {
            long id = r.getValue(0, lvf);
            if (threadId == id) {
                processHost = r.getValue(2, svf);
                break;
            }
        }
        return processHost;
    } catch (IOException e) {
        throw ExceptionFactory.createException(e.getMessage(), e);
    }
}
Also used : StringValueFactory(com.mysql.cj.result.StringValueFactory) NativeProtocol(com.mysql.cj.protocol.a.NativeProtocol) IOException(java.io.IOException) NativePacketPayload(com.mysql.cj.protocol.a.NativePacketPayload) ResultsetFactory(com.mysql.cj.protocol.a.ResultsetFactory) LongValueFactory(com.mysql.cj.result.LongValueFactory) Resultset(com.mysql.cj.protocol.Resultset) Row(com.mysql.cj.result.Row)

Example 3 with NativePacketPayload

use of com.mysql.cj.protocol.a.NativePacketPayload in project ABC by RuiPinto96274.

the class NativeSession method loadServerVariables.

/**
 * Loads the result of 'SHOW VARIABLES' into the serverVariables field so
 * that the driver can configure itself.
 *
 * @param syncMutex
 *            synchronization mutex
 * @param version
 *            driver version string
 */
public void loadServerVariables(Object syncMutex, String version) {
    if (this.cacheServerConfiguration.getValue()) {
        createConfigCacheIfNeeded(syncMutex);
        Map<String, String> cachedVariableMap = this.serverConfigCache.get(this.hostInfo.getDatabaseUrl());
        if (cachedVariableMap != null) {
            String cachedServerVersion = cachedVariableMap.get(SERVER_VERSION_STRING_VAR_NAME);
            if (cachedServerVersion != null && getServerSession().getServerVersion() != null && cachedServerVersion.equals(getServerSession().getServerVersion().toString())) {
                Map<String, String> localVariableMap = this.protocol.getServerSession().getServerVariables();
                Map<String, String> newLocalVariableMap = new HashMap<>();
                newLocalVariableMap.putAll(cachedVariableMap);
                // preserving variables already configured on previous session initialization steps
                newLocalVariableMap.putAll(localVariableMap);
                this.protocol.getServerSession().setServerVariables(newLocalVariableMap);
                return;
            }
            this.serverConfigCache.invalidate(this.hostInfo.getDatabaseUrl());
        }
    }
    try {
        if (version != null && version.indexOf('*') != -1) {
            StringBuilder buf = new StringBuilder(version.length() + 10);
            for (int i = 0; i < version.length(); i++) {
                char c = version.charAt(i);
                buf.append(c == '*' ? "[star]" : c);
            }
            version = buf.toString();
        }
        String versionComment = (this.propertySet.getBooleanProperty(PropertyKey.paranoid).getValue() || version == null) ? "" : "/* " + version + " */";
        this.protocol.getServerSession().setServerVariables(new HashMap<String, String>());
        if (versionMeetsMinimum(5, 1, 0)) {
            StringBuilder queryBuf = new StringBuilder(versionComment).append("SELECT");
            queryBuf.append("  @@session.auto_increment_increment AS auto_increment_increment");
            queryBuf.append(", @@character_set_client AS character_set_client");
            queryBuf.append(", @@character_set_connection AS character_set_connection");
            queryBuf.append(", @@character_set_results AS character_set_results");
            queryBuf.append(", @@character_set_server AS character_set_server");
            queryBuf.append(", @@collation_server AS collation_server");
            queryBuf.append(", @@collation_connection AS collation_connection");
            queryBuf.append(", @@init_connect AS init_connect");
            queryBuf.append(", @@interactive_timeout AS interactive_timeout");
            if (!versionMeetsMinimum(5, 5, 0)) {
                queryBuf.append(", @@language AS language");
            }
            queryBuf.append(", @@license AS license");
            queryBuf.append(", @@lower_case_table_names AS lower_case_table_names");
            queryBuf.append(", @@max_allowed_packet AS max_allowed_packet");
            queryBuf.append(", @@net_write_timeout AS net_write_timeout");
            queryBuf.append(", @@performance_schema AS performance_schema");
            if (!versionMeetsMinimum(8, 0, 3)) {
                queryBuf.append(", @@query_cache_size AS query_cache_size");
                queryBuf.append(", @@query_cache_type AS query_cache_type");
            }
            queryBuf.append(", @@sql_mode AS sql_mode");
            queryBuf.append(", @@system_time_zone AS system_time_zone");
            queryBuf.append(", @@time_zone AS time_zone");
            if (versionMeetsMinimum(8, 0, 3) || (versionMeetsMinimum(5, 7, 20) && !versionMeetsMinimum(8, 0, 0))) {
                queryBuf.append(", @@transaction_isolation AS transaction_isolation");
            } else {
                queryBuf.append(", @@tx_isolation AS transaction_isolation");
            }
            queryBuf.append(", @@wait_timeout AS wait_timeout");
            NativePacketPayload resultPacket = sendCommand(this.commandBuilder.buildComQuery(null, queryBuf.toString()), false, 0);
            Resultset rs = ((NativeProtocol) this.protocol).readAllResults(-1, false, resultPacket, false, null, new ResultsetFactory(Type.FORWARD_ONLY, null));
            Field[] f = rs.getColumnDefinition().getFields();
            if (f.length > 0) {
                ValueFactory<String> vf = new StringValueFactory(this.propertySet);
                Row r;
                if ((r = rs.getRows().next()) != null) {
                    for (int i = 0; i < f.length; i++) {
                        String value = r.getValue(i, vf);
                        this.protocol.getServerSession().getServerVariables().put(f[i].getColumnLabel(), // recent server versions return "utf8mb3" instead of "utf8"
                        "utf8mb3".equalsIgnoreCase(value) ? "utf8" : value);
                    }
                }
            }
        } else {
            NativePacketPayload resultPacket = sendCommand(this.commandBuilder.buildComQuery(null, versionComment + "SHOW VARIABLES"), false, 0);
            Resultset rs = ((NativeProtocol) this.protocol).readAllResults(-1, false, resultPacket, false, null, new ResultsetFactory(Type.FORWARD_ONLY, null));
            ValueFactory<String> vf = new StringValueFactory(this.propertySet);
            Row r;
            while ((r = rs.getRows().next()) != null) {
                this.protocol.getServerSession().getServerVariables().put(r.getValue(0, vf), r.getValue(1, vf));
            }
        }
    } catch (IOException e) {
        throw ExceptionFactory.createException(e.getMessage(), e);
    }
    if (this.cacheServerConfiguration.getValue()) {
        this.protocol.getServerSession().getServerVariables().put(SERVER_VERSION_STRING_VAR_NAME, getServerSession().getServerVersion().toString());
        Map<String, String> localVariableMap = new HashMap<>();
        localVariableMap.putAll(this.protocol.getServerSession().getServerVariables());
        this.serverConfigCache.put(this.hostInfo.getDatabaseUrl(), Collections.unmodifiableMap(localVariableMap));
    }
}
Also used : StringValueFactory(com.mysql.cj.result.StringValueFactory) HashMap(java.util.HashMap) NativeProtocol(com.mysql.cj.protocol.a.NativeProtocol) IOException(java.io.IOException) NativePacketPayload(com.mysql.cj.protocol.a.NativePacketPayload) ResultsetFactory(com.mysql.cj.protocol.a.ResultsetFactory) Field(com.mysql.cj.result.Field) Resultset(com.mysql.cj.protocol.Resultset) Row(com.mysql.cj.result.Row)

Example 4 with NativePacketPayload

use of com.mysql.cj.protocol.a.NativePacketPayload in project ABC by RuiPinto96274.

the class ServerPreparedQuery method prepareExecutePacket.

public NativePacketPayload prepareExecutePacket() {
    ServerPreparedQueryBindValue[] parameterBindings = this.queryBindings.getBindValues();
    if (this.queryBindings.isLongParameterSwitchDetected()) {
        // Check when values were bound
        boolean firstFound = false;
        long boundTimeToCheck = 0;
        for (int i = 0; i < this.parameterCount - 1; i++) {
            if (parameterBindings[i].isStream()) {
                if (firstFound && boundTimeToCheck != parameterBindings[i].boundBeforeExecutionNum) {
                    throw ExceptionFactory.createException(Messages.getString("ServerPreparedStatement.11") + Messages.getString("ServerPreparedStatement.12"), MysqlErrorNumbers.SQL_STATE_DRIVER_NOT_CAPABLE, 0, true, null, this.session.getExceptionInterceptor());
                }
                firstFound = true;
                boundTimeToCheck = parameterBindings[i].boundBeforeExecutionNum;
            }
        }
        // Okay, we've got all "newly"-bound streams, so reset server-side state to clear out previous bindings
        serverResetStatement();
    }
    this.queryBindings.checkAllParametersSet();
    // 
    for (int i = 0; i < this.parameterCount; i++) {
        if (parameterBindings[i].isStream()) {
            serverLongData(i, parameterBindings[i]);
        }
    }
    // 
    // store the parameter values
    // 
    NativePacketPayload packet = this.session.getSharedSendPacket();
    packet.writeInteger(IntegerDataType.INT1, NativeConstants.COM_STMT_EXECUTE);
    packet.writeInteger(IntegerDataType.INT4, this.serverStatementId);
    boolean supportsQueryAttributes = this.session.getServerSession().supportsQueryAttributes();
    boolean sendQueryAttributes = false;
    if (supportsQueryAttributes) {
        // Servers between 8.0.23 8.0.25 are affected by Bug#103102, Bug#103268 and Bug#103377. Query attributes cannot be sent to these servers.
        sendQueryAttributes = this.session.getServerSession().getServerVersion().meetsMinimum(new ServerVersion(8, 0, 26));
    } else if (this.queryAttributesBindings.getCount() > 0) {
        this.session.getLog().logWarn(Messages.getString("QueryAttributes.SetButNotSupported"));
    }
    byte flags = 0;
    if (this.resultFields != null && this.resultFields.getFields() != null && this.useCursorFetch && this.resultSetType == Type.FORWARD_ONLY && this.fetchSize > 0) {
        // we only create cursor-backed result sets if
        // a) The query is a SELECT
        // b) The server supports it
        // c) We know it is forward-only (note this doesn't preclude updatable result sets)
        // d) The user has set a fetch size
        flags |= OPEN_CURSOR_FLAG;
    }
    if (sendQueryAttributes) {
        flags |= PARAMETER_COUNT_AVAILABLE;
    }
    packet.writeInteger(IntegerDataType.INT1, flags);
    // placeholder for parameter iterations
    packet.writeInteger(IntegerDataType.INT4, 1);
    int parametersAndAttributesCount = this.parameterCount;
    if (supportsQueryAttributes) {
        if (sendQueryAttributes) {
            parametersAndAttributesCount += this.queryAttributesBindings.getCount();
        }
        if (sendQueryAttributes || parametersAndAttributesCount > 0) {
            // Servers between 8.0.23 and 8.0.25 don't expect a 'parameter_count' value if the statement was prepared without parameters.
            packet.writeInteger(IntegerDataType.INT_LENENC, parametersAndAttributesCount);
        }
    }
    if (parametersAndAttributesCount > 0) {
        /* Reserve place for null-marker bytes */
        int nullCount = (parametersAndAttributesCount + 7) / 8;
        int nullBitsPosition = packet.getPosition();
        for (int i = 0; i < nullCount; i++) {
            packet.writeInteger(IntegerDataType.INT1, 0);
        }
        byte[] nullBitsBuffer = new byte[nullCount];
        // In case if buffers (type) changed or there are query attributes to send.
        if (this.queryBindings.getSendTypesToServer().get() || sendQueryAttributes && this.queryAttributesBindings.getCount() > 0) {
            packet.writeInteger(IntegerDataType.INT1, 1);
            // Store types of parameters in the first package that is sent to the server.
            for (int i = 0; i < this.parameterCount; i++) {
                packet.writeInteger(IntegerDataType.INT2, parameterBindings[i].bufferType);
                if (supportsQueryAttributes) {
                    // Parameters have no names.
                    packet.writeBytes(StringSelfDataType.STRING_LENENC, "".getBytes());
                }
            }
            if (sendQueryAttributes) {
                this.queryAttributesBindings.runThroughAll(a -> {
                    packet.writeInteger(IntegerDataType.INT2, a.getType());
                    packet.writeBytes(StringSelfDataType.STRING_LENENC, a.getName().getBytes());
                });
            }
        } else {
            packet.writeInteger(IntegerDataType.INT1, 0);
        }
        // Store the parameter values.
        for (int i = 0; i < this.parameterCount; i++) {
            if (!parameterBindings[i].isStream()) {
                if (!parameterBindings[i].isNull()) {
                    parameterBindings[i].storeBinding(packet, this.queryBindings.isLoadDataQuery(), this.charEncoding, this.session.getExceptionInterceptor());
                } else {
                    nullBitsBuffer[i >>> 3] |= (1 << (i & 7));
                }
            }
        }
        if (sendQueryAttributes) {
            for (int i = 0; i < this.queryAttributesBindings.getCount(); i++) {
                if (this.queryAttributesBindings.getAttributeValue(i).isNull()) {
                    int b = i + this.parameterCount;
                    nullBitsBuffer[b >>> 3] |= 1 << (b & 7);
                }
            }
            ValueEncoder valueEncoder = new ValueEncoder(packet, this.charEncoding, this.session.getServerSession().getDefaultTimeZone());
            this.queryAttributesBindings.runThroughAll(a -> valueEncoder.encodeValue(a.getValue(), a.getType()));
        }
        // Go back and write the NULL flags to the beginning of the packet
        int endPosition = packet.getPosition();
        packet.setPosition(nullBitsPosition);
        packet.writeBytes(StringLengthDataType.STRING_FIXED, nullBitsBuffer);
        packet.setPosition(endPosition);
    }
    return packet;
}
Also used : ValueEncoder(com.mysql.cj.protocol.a.ValueEncoder) NativePacketPayload(com.mysql.cj.protocol.a.NativePacketPayload)

Example 5 with NativePacketPayload

use of com.mysql.cj.protocol.a.NativePacketPayload in project ABC by RuiPinto96274.

the class ServerPreparedQuery method sendExecutePacket.

public NativePacketPayload sendExecutePacket(NativePacketPayload packet, String queryAsString) {
    // TODO queryAsString should be shared instead of passed
    final long begin = this.session.getCurrentTimeNanosOrMillis();
    resetCancelledState();
    CancelQueryTask timeoutTask = null;
    try {
        // Get this before executing to avoid a shared packet pollution in the case some other query is issued internally, such as when using I_S.
        timeoutTask = startQueryTimer(this, this.timeoutInMillis);
        statementBegins();
        NativePacketPayload resultPacket = this.session.sendCommand(packet, false, 0);
        final long queryEndTime = this.session.getCurrentTimeNanosOrMillis();
        if (timeoutTask != null) {
            stopQueryTimer(timeoutTask, true, true);
            timeoutTask = null;
        }
        final long executeTime = queryEndTime - begin;
        setExecuteTime(executeTime);
        if (this.logSlowQueries) {
            this.queryWasSlow = // 
            this.useAutoSlowLog ? this.session.getProtocol().getMetricsHolder().checkAbonormallyLongQuery(executeTime) : executeTime > this.slowQueryThresholdMillis.getValue();
            if (this.queryWasSlow) {
                this.session.getProfilerEventHandler().processEvent(ProfilerEvent.TYPE_SLOW_QUERY, this.session, this, null, executeTime, new Throwable(), Messages.getString("ServerPreparedStatement.15", new String[] { String.valueOf(this.session.getSlowQueryThreshold()), String.valueOf(executeTime), this.originalSql, queryAsString }));
            }
        }
        if (this.gatherPerfMetrics) {
            this.session.getProtocol().getMetricsHolder().registerQueryExecutionTime(executeTime);
            this.session.getProtocol().getMetricsHolder().incrementNumberOfPreparedExecutes();
        }
        if (this.profileSQL) {
            this.session.getProfilerEventHandler().processEvent(ProfilerEvent.TYPE_EXECUTE, this.session, this, null, executeTime, new Throwable(), truncateQueryToLog(queryAsString));
        }
        return resultPacket;
    } catch (CJException sqlEx) {
        if (this.session.shouldIntercept()) {
            this.session.invokeQueryInterceptorsPost(() -> {
                return getOriginalSql();
            }, this, null, true);
        }
        throw sqlEx;
    } finally {
        this.statementExecuting.set(false);
        stopQueryTimer(timeoutTask, false, false);
    }
}
Also used : NativePacketPayload(com.mysql.cj.protocol.a.NativePacketPayload) CJException(com.mysql.cj.exceptions.CJException)

Aggregations

NativePacketPayload (com.mysql.cj.protocol.a.NativePacketPayload)90 Test (org.junit.jupiter.api.Test)30 CJException (com.mysql.cj.exceptions.CJException)27 AuthenticationLdapSaslClientPlugin (com.mysql.cj.protocol.a.authentication.AuthenticationLdapSaslClientPlugin)24 ArrayList (java.util.ArrayList)21 NativeProtocol (com.mysql.cj.protocol.a.NativeProtocol)18 Resultset (com.mysql.cj.protocol.Resultset)15 ResultsetFactory (com.mysql.cj.protocol.a.ResultsetFactory)15 Row (com.mysql.cj.result.Row)15 IOException (java.io.IOException)15 StringValueFactory (com.mysql.cj.result.StringValueFactory)12 HashMap (java.util.HashMap)9 MysqlConnection (com.mysql.cj.MysqlConnection)6 NativeSession (com.mysql.cj.NativeSession)6 UnableToConnectException (com.mysql.cj.exceptions.UnableToConnectException)6 JdbcConnection (com.mysql.cj.jdbc.JdbcConnection)6 NativePacketHeader (com.mysql.cj.protocol.a.NativePacketHeader)6 ValueEncoder (com.mysql.cj.protocol.a.ValueEncoder)6 PrivilegedActionException (java.security.PrivilegedActionException)6 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)6