Search in sources :

Example 1 with NativeSession

use of com.mysql.cj.NativeSession in project ABC by RuiPinto96274.

the class NativeProtocol method beforeHandshake.

@Override
public void beforeHandshake() {
    // Reset packet sequences
    this.packetReader.resetMessageSequence();
    // Create session state
    this.serverSession = new NativeServerSession(this.propertySet);
    this.serverSession.setCharsetSettings(new NativeCharsetSettings((NativeSession) this.session));
    // Read the first packet
    this.serverSession.setCapabilities(readServerCapabilities());
}
Also used : NativeCharsetSettings(com.mysql.cj.NativeCharsetSettings) NativeSession(com.mysql.cj.NativeSession)

Example 2 with NativeSession

use of com.mysql.cj.NativeSession 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 3 with NativeSession

use of com.mysql.cj.NativeSession in project ABC by RuiPinto96274.

the class ConnectionRegressionTest method testBug92625.

/**
 * Tests fix for BUG#92625 (28731795), CONTRIBUTION: FIX OBSERVED NPE IN CLEARINPUTSTREAM.
 *
 * @throws Exception
 */
@Test
public void testBug92625() throws Exception {
    Properties props = new Properties();
    props.setProperty(PropertyKey.sslMode.getKeyName(), "DISABLED");
    props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
    props.setProperty(PropertyKey.useServerPrepStmts.getKeyName(), "true");
    Connection con = getConnectionWithProps(props);
    ((NativeSession) ((JdbcConnection) con).getSession()).getProtocol().getSocketConnection().forceClose();
    assertThrows(CommunicationsException.class, new Callable<Void>() {

        public Void call() throws Exception {
            ((JdbcConnection) con).serverPrepareStatement("SELECT 1");
            return null;
        }
    });
}
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) Properties(java.util.Properties) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) SQLTransientException(java.sql.SQLTransientException) InvocationTargetException(java.lang.reflect.InvocationTargetException) XAException(javax.transaction.xa.XAException) SocketException(java.net.SocketException) SQLClientInfoException(java.sql.SQLClientInfoException) SQLException(java.sql.SQLException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) PasswordExpiredException(com.mysql.cj.exceptions.PasswordExpiredException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) SQLNonTransientConnectionException(java.sql.SQLNonTransientConnectionException) CommunicationsException(com.mysql.cj.jdbc.exceptions.CommunicationsException) CertificateException(java.security.cert.CertificateException) ClosedOnExpiredPasswordException(com.mysql.cj.exceptions.ClosedOnExpiredPasswordException) PropertyNotModifiableException(com.mysql.cj.exceptions.PropertyNotModifiableException) Test(org.junit.jupiter.api.Test)

Example 4 with NativeSession

use of com.mysql.cj.NativeSession in project ABC by RuiPinto96274.

the class ConnectionRegressionTest method testLongAuthResponsePayload.

/**
 * Test for sha256_password long data exchange.
 *
 * @throws Exception
 */
@Test
public void testLongAuthResponsePayload() throws Exception {
    NativeSession testSess;
    assumeTrue((testSess = (NativeSession) ((MysqlConnection) this.conn).getSession()).versionMeetsMinimum(5, 6, 6), "Requires MySQL 5.6.6+.");
    assumeTrue(pluginIsActive(this.stmt, "sha256_password"), "sha256_password required to run this test");
    assumeTrue(supportsTestCertificates(this.stmt), "This test requires the server configured with SSL certificates from ConnectorJ/src/test/config/ssl-test-certs");
    assumeTrue(supportsTestSha256PasswordKeys(this.stmt), "This test requires the server configured with RSA keys from ConnectorJ/src/test/config/ssl-test-certs");
    Properties props = new Properties();
    props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
    try {
        // create user with long password and sha256_password auth
        String pwd = testSess.versionMeetsMinimum(8, 0, 4) || testSess.versionMeetsMinimum(5, 7, 21) && !testSess.versionMeetsMinimum(8, 0, 0) || testSess.versionMeetsMinimum(5, 6, 39) && !testSess.versionMeetsMinimum(5, 7, 0) ? "aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeaaaaaaaaaabbbbbbbbbbccccccccccdddddddddd" : "aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeaaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeee" + "aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeaaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeee" + "aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeaaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeee";
        if (!testSess.versionMeetsMinimum(8, 0, 5)) {
            this.stmt.executeUpdate("SET @current_old_passwords = @@global.old_passwords");
        }
        createUser(this.stmt, "'wl6134user'@'%'", "identified WITH sha256_password");
        this.stmt.executeUpdate("grant all on *.* to 'wl6134user'@'%'");
        if (!testSess.versionMeetsMinimum(8, 0, 5)) {
            this.stmt.executeUpdate("SET GLOBAL old_passwords= 2");
            this.stmt.executeUpdate("SET SESSION old_passwords= 2");
        }
        this.stmt.executeUpdate(((MysqlConnection) this.conn).getSession().versionMeetsMinimum(5, 7, 6) ? "ALTER USER 'wl6134user'@'%' IDENTIFIED BY '" + pwd + "'" : "set password for 'wl6134user'@'%' = PASSWORD('" + pwd + "')");
        this.stmt.executeUpdate("flush privileges");
        this.rs = this.stmt.executeQuery("SELECT plugin FROM mysql.user WHERE user='wl6134user'");
        assertTrue(this.rs.next());
        assumeTrue("sha256_password".equals(this.rs.getString(1)), "This test requires the server configured with default sha256_password plugin");
        props.setProperty(PropertyKey.USER.getKeyName(), "wl6134user");
        props.setProperty(PropertyKey.PASSWORD.getKeyName(), pwd);
        props.setProperty(PropertyKey.defaultAuthenticationPlugin.getKeyName(), Sha256PasswordPlugin.class.getName());
        props.setProperty(PropertyKey.useSSL.getKeyName(), "false");
        Connection testConn2 = null;
        try {
            testConn2 = DriverManager.getConnection(dbUrl, props);
            fail("SQLException expected due to password is too long for RSA encryption");
        } catch (Exception e) {
            assertTrue(e.getMessage().startsWith("Data must not be longer than"));
        } finally {
            if (testConn2 != null) {
                testConn2.close();
            }
        }
        try {
            String trustStorePath = "src/test/config/ssl-test-certs/ca-truststore";
            System.setProperty("javax.net.ssl.keyStore", trustStorePath);
            System.setProperty("javax.net.ssl.keyStorePassword", "password");
            System.setProperty("javax.net.ssl.trustStore", trustStorePath);
            System.setProperty("javax.net.ssl.trustStorePassword", "password");
            props.setProperty(PropertyKey.useSSL.getKeyName(), "true");
            props.setProperty(PropertyKey.requireSSL.getKeyName(), "true");
            props.setProperty(PropertyKey.verifyServerCertificate.getKeyName(), "false");
            assertCurrentUser(dbUrl, props, "wl6134user", true);
        } catch (Exception e) {
            throw e;
        } finally {
            if (testConn2 != null) {
                testConn2.close();
            }
        }
    } finally {
        if (!testSess.versionMeetsMinimum(8, 0, 5)) {
            this.stmt.executeUpdate("SET GLOBAL old_passwords = @current_old_passwords");
        }
    }
}
Also used : NativeSession(com.mysql.cj.NativeSession) 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) Sha256PasswordPlugin(com.mysql.cj.protocol.a.authentication.Sha256PasswordPlugin) Properties(java.util.Properties) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) SQLTransientException(java.sql.SQLTransientException) InvocationTargetException(java.lang.reflect.InvocationTargetException) XAException(javax.transaction.xa.XAException) SocketException(java.net.SocketException) SQLClientInfoException(java.sql.SQLClientInfoException) SQLException(java.sql.SQLException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) PasswordExpiredException(com.mysql.cj.exceptions.PasswordExpiredException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) SQLNonTransientConnectionException(java.sql.SQLNonTransientConnectionException) CommunicationsException(com.mysql.cj.jdbc.exceptions.CommunicationsException) CertificateException(java.security.cert.CertificateException) ClosedOnExpiredPasswordException(com.mysql.cj.exceptions.ClosedOnExpiredPasswordException) PropertyNotModifiableException(com.mysql.cj.exceptions.PropertyNotModifiableException) Test(org.junit.jupiter.api.Test)

Example 5 with NativeSession

use of com.mysql.cj.NativeSession in project ABC by RuiPinto96274.

the class ClientPreparedStatement method executePreparedBatchAsMultiStatement.

/**
 * Rewrites the already prepared statement into a multi-statement
 * query of 'statementsPerBatch' values and executes the entire batch
 * using this new statement.
 *
 * @param batchTimeout
 *            timeout for the batch execution
 * @return update counts in the same fashion as executeBatch()
 *
 * @throws SQLException
 *             if a database access error occurs or this method is called on a closed PreparedStatement
 */
protected long[] executePreparedBatchAsMultiStatement(int batchTimeout) throws SQLException {
    synchronized (checkClosed().getConnectionMutex()) {
        // This is kind of an abuse, but it gets the job done
        if (this.batchedValuesClause == null) {
            this.batchedValuesClause = ((PreparedQuery<?>) this.query).getOriginalSql() + ";";
        }
        JdbcConnection locallyScopedConn = this.connection;
        boolean multiQueriesEnabled = locallyScopedConn.getPropertySet().getBooleanProperty(PropertyKey.allowMultiQueries).getValue();
        CancelQueryTask timeoutTask = null;
        try {
            clearWarnings();
            int numBatchedArgs = this.query.getBatchedArgs().size();
            if (this.retrieveGeneratedKeys) {
                this.batchedGeneratedKeys = new ArrayList<>(numBatchedArgs);
            }
            int numValuesPerBatch = ((PreparedQuery<?>) this.query).computeBatchSize(numBatchedArgs);
            if (numBatchedArgs < numValuesPerBatch) {
                numValuesPerBatch = numBatchedArgs;
            }
            java.sql.PreparedStatement batchedStatement = null;
            int batchedParamIndex = 1;
            int numberToExecuteAsMultiValue = 0;
            int batchCounter = 0;
            int updateCountCounter = 0;
            long[] updateCounts = new long[numBatchedArgs * getParseInfo().getNumberOfQueries()];
            SQLException sqlEx = null;
            try {
                if (!multiQueriesEnabled) {
                    ((NativeSession) locallyScopedConn.getSession()).enableMultiQueries();
                }
                batchedStatement = this.retrieveGeneratedKeys ? ((Wrapper) locallyScopedConn.prepareStatement(generateMultiStatementForBatch(numValuesPerBatch), RETURN_GENERATED_KEYS)).unwrap(java.sql.PreparedStatement.class) : ((Wrapper) locallyScopedConn.prepareStatement(generateMultiStatementForBatch(numValuesPerBatch))).unwrap(java.sql.PreparedStatement.class);
                timeoutTask = startQueryTimer((StatementImpl) batchedStatement, batchTimeout);
                numberToExecuteAsMultiValue = numBatchedArgs < numValuesPerBatch ? numBatchedArgs : numBatchedArgs / numValuesPerBatch;
                int numberArgsToExecute = numberToExecuteAsMultiValue * numValuesPerBatch;
                for (int i = 0; i < numberArgsToExecute; i++) {
                    if (i != 0 && i % numValuesPerBatch == 0) {
                        try {
                            batchedStatement.execute();
                        } catch (SQLException ex) {
                            sqlEx = handleExceptionForBatch(batchCounter, numValuesPerBatch, updateCounts, ex);
                        }
                        updateCountCounter = processMultiCountsAndKeys((StatementImpl) batchedStatement, updateCountCounter, updateCounts);
                        batchedStatement.clearParameters();
                        batchedParamIndex = 1;
                    }
                    batchedParamIndex = setOneBatchedParameterSet(batchedStatement, batchedParamIndex, this.query.getBatchedArgs().get(batchCounter++));
                }
                try {
                    batchedStatement.execute();
                } catch (SQLException ex) {
                    sqlEx = handleExceptionForBatch(batchCounter - 1, numValuesPerBatch, updateCounts, ex);
                }
                updateCountCounter = processMultiCountsAndKeys((StatementImpl) batchedStatement, updateCountCounter, updateCounts);
                batchedStatement.clearParameters();
                numValuesPerBatch = numBatchedArgs - batchCounter;
                if (timeoutTask != null) {
                    // we need to check the cancel state now because we loose if after the following batchedStatement.close()
                    ((JdbcPreparedStatement) batchedStatement).checkCancelTimeout();
                }
            } finally {
                if (batchedStatement != null) {
                    batchedStatement.close();
                    batchedStatement = null;
                }
            }
            try {
                if (numValuesPerBatch > 0) {
                    batchedStatement = this.retrieveGeneratedKeys ? locallyScopedConn.prepareStatement(generateMultiStatementForBatch(numValuesPerBatch), RETURN_GENERATED_KEYS) : locallyScopedConn.prepareStatement(generateMultiStatementForBatch(numValuesPerBatch));
                    if (timeoutTask != null) {
                        timeoutTask.setQueryToCancel((Query) batchedStatement);
                    }
                    batchedParamIndex = 1;
                    while (batchCounter < numBatchedArgs) {
                        batchedParamIndex = setOneBatchedParameterSet(batchedStatement, batchedParamIndex, this.query.getBatchedArgs().get(batchCounter++));
                    }
                    try {
                        batchedStatement.execute();
                    } catch (SQLException ex) {
                        sqlEx = handleExceptionForBatch(batchCounter - 1, numValuesPerBatch, updateCounts, ex);
                    }
                    updateCountCounter = processMultiCountsAndKeys((StatementImpl) batchedStatement, updateCountCounter, updateCounts);
                    batchedStatement.clearParameters();
                }
                if (timeoutTask != null) {
                    stopQueryTimer(timeoutTask, true, true);
                    timeoutTask = null;
                }
                if (sqlEx != null) {
                    throw SQLError.createBatchUpdateException(sqlEx, updateCounts, this.exceptionInterceptor);
                }
                return updateCounts;
            } finally {
                if (batchedStatement != null) {
                    batchedStatement.close();
                }
            }
        } finally {
            stopQueryTimer(timeoutTask, false, false);
            resetCancelledState();
            if (!multiQueriesEnabled) {
                ((NativeSession) locallyScopedConn.getSession()).disableMultiQueries();
            }
            clearBatch();
        }
    }
}
Also used : Wrapper(java.sql.Wrapper) SQLException(java.sql.SQLException) ClientPreparedQuery(com.mysql.cj.ClientPreparedQuery) PreparedQuery(com.mysql.cj.PreparedQuery) CancelQueryTask(com.mysql.cj.CancelQueryTask) NativeSession(com.mysql.cj.NativeSession)

Aggregations

NativeSession (com.mysql.cj.NativeSession)25 JdbcConnection (com.mysql.cj.jdbc.JdbcConnection)13 MysqlConnection (com.mysql.cj.MysqlConnection)12 Connection (java.sql.Connection)12 Properties (java.util.Properties)12 Test (org.junit.jupiter.api.Test)12 MysqlPooledConnection (com.mysql.cj.jdbc.MysqlPooledConnection)9 MysqlXAConnection (com.mysql.cj.jdbc.MysqlXAConnection)9 SuspendableXAConnection (com.mysql.cj.jdbc.SuspendableXAConnection)9 ReplicationConnection (com.mysql.cj.jdbc.ha.ReplicationConnection)9 IOException (java.io.IOException)9 SQLException (java.sql.SQLException)9 PooledConnection (javax.sql.PooledConnection)9 XAConnection (javax.sql.XAConnection)9 ClosedOnExpiredPasswordException (com.mysql.cj.exceptions.ClosedOnExpiredPasswordException)6 PasswordExpiredException (com.mysql.cj.exceptions.PasswordExpiredException)6 PropertyNotModifiableException (com.mysql.cj.exceptions.PropertyNotModifiableException)6 CommunicationsException (com.mysql.cj.jdbc.exceptions.CommunicationsException)6 NativePacketHeader (com.mysql.cj.protocol.a.NativePacketHeader)6 NativePacketPayload (com.mysql.cj.protocol.a.NativePacketPayload)6