Search in sources :

Example 1 with NativeProtocol

use of com.mysql.cj.protocol.a.NativeProtocol 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 NativeProtocol

use of com.mysql.cj.protocol.a.NativeProtocol 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 NativeProtocol

use of com.mysql.cj.protocol.a.NativeProtocol 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 NativeProtocol

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

the class ConnectionRegressionTest method testBug25642021.

/**
 * Tests fix for BUG#25642021, CHANGEUSER() FAILS WHEN ENABLEPACKETDEBUG=TRUE.
 *
 * @throws Exception
 */
@Test
public void testBug25642021() throws Exception {
    Properties props = getPropertiesFromTestsuiteUrl();
    props.setProperty(PropertyKey.sslMode.getKeyName(), "DISABLED");
    props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
    props.setProperty(PropertyKey.enablePacketDebug.getKeyName(), "true");
    props.setProperty(PropertyKey.maintainTimeStats.getKeyName(), "true");
    Connection newConn = getConnectionWithProps(props);
    ((JdbcConnection) newConn).changeUser(props.getProperty(PropertyKey.USER.getKeyName()), props.getProperty(PropertyKey.PASSWORD.getKeyName()));
    // check that decorators are still in place
    NativeProtocol p = ((NativeSession) ((JdbcConnection) newConn).getSession()).getProtocol();
    MessageSender<NativePacketPayload> sender = p.getPacketSender();
    MessageReader<NativePacketHeader, NativePacketPayload> reader = p.getPacketReader();
    assertEquals(DebugBufferingPacketSender.class, sender.getClass());
    assertEquals(TimeTrackingPacketSender.class, sender.undecorate().getClass());
    assertEquals(SimplePacketSender.class, sender.undecorate().undecorate().getClass());
    assertEquals(SimplePacketSender.class, sender.undecorate().undecorate().undecorate().getClass());
    assertEquals(MultiPacketReader.class, reader.getClass());
    assertEquals(DebugBufferingPacketReader.class, reader.undecorate().getClass());
    assertEquals(TimeTrackingPacketReader.class, reader.undecorate().undecorate().getClass());
    assertEquals(SimplePacketReader.class, reader.undecorate().undecorate().undecorate().getClass());
    assertEquals(SimplePacketReader.class, reader.undecorate().undecorate().undecorate().undecorate().getClass());
}
Also used : ReplicationConnection(com.mysql.cj.jdbc.ha.ReplicationConnection) MysqlPooledConnection(com.mysql.cj.jdbc.MysqlPooledConnection) SuspendableXAConnection(com.mysql.cj.jdbc.SuspendableXAConnection) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) PooledConnection(javax.sql.PooledConnection) MysqlXAConnection(com.mysql.cj.jdbc.MysqlXAConnection) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) MysqlConnection(com.mysql.cj.MysqlConnection) NativeSession(com.mysql.cj.NativeSession) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) NativeProtocol(com.mysql.cj.protocol.a.NativeProtocol) Properties(java.util.Properties) NativePacketPayload(com.mysql.cj.protocol.a.NativePacketPayload) NativePacketHeader(com.mysql.cj.protocol.a.NativePacketHeader) Test(org.junit.jupiter.api.Test)

Example 5 with NativeProtocol

use of com.mysql.cj.protocol.a.NativeProtocol in project JavaSegundasQuintas by ecteruel.

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)

Aggregations

NativePacketPayload (com.mysql.cj.protocol.a.NativePacketPayload)18 NativeProtocol (com.mysql.cj.protocol.a.NativeProtocol)18 Resultset (com.mysql.cj.protocol.Resultset)12 ResultsetFactory (com.mysql.cj.protocol.a.ResultsetFactory)12 Row (com.mysql.cj.result.Row)12 IOException (java.io.IOException)12 StringValueFactory (com.mysql.cj.result.StringValueFactory)9 MysqlConnection (com.mysql.cj.MysqlConnection)6 NativeSession (com.mysql.cj.NativeSession)6 JdbcConnection (com.mysql.cj.jdbc.JdbcConnection)6 NativePacketHeader (com.mysql.cj.protocol.a.NativePacketHeader)6 LongValueFactory (com.mysql.cj.result.LongValueFactory)6 Connection (java.sql.Connection)6 Properties (java.util.Properties)6 Test (org.junit.jupiter.api.Test)6 MysqlPooledConnection (com.mysql.cj.jdbc.MysqlPooledConnection)3 MysqlXAConnection (com.mysql.cj.jdbc.MysqlXAConnection)3 SuspendableXAConnection (com.mysql.cj.jdbc.SuspendableXAConnection)3 ReplicationConnection (com.mysql.cj.jdbc.ha.ReplicationConnection)3 Field (com.mysql.cj.result.Field)3