Search in sources :

Example 11 with JdbcConnection

use of com.mysql.cj.jdbc.JdbcConnection in project JavaSegundasQuintas by ecteruel.

the class StatementRegressionTest method testBug71396.

/**
 * Tests fix for BUG#71396 - setMaxRows (SQL_SELECT_LIMIT) from one query used in later queries (sometimes)
 *
 * @throws Exception
 */
@Test
public void testBug71396() throws Exception {
    final String queryLimitClause = "SELECT * FROM testBug71396 LIMIT 2";
    final String queryLimitClauseInJoin = "SELECT * FROM testBug71396 A JOIN (SELECT * FROM testBug71396 LIMIT 2) B ON A.c != B.c";
    final String queryLimitInQuotes = "SELECT * FROM testBug71396 WHERE c != 'Unlimited'";
    final String queryLimitInComment = "SELECT * FROM testBug71396 -- Unlimited";
    final String queryNoLimit = "SELECT * FROM testBug71396";
    final String[] queries = new String[] { queryLimitClause, queryLimitClauseInJoin, queryLimitInQuotes, queryLimitInComment, queryNoLimit };
    Connection testConn;
    Statement testStmt;
    ResultSet testRS;
    PreparedStatement[] testPStmtSet;
    createTable("testBug71396", "(c VARCHAR(5))");
    this.stmt.execute("INSERT INTO testBug71396 VALUES ('One'), ('Two'), ('Three')");
    /*
         * Case 1: Statement.executeQuery() and Statement.execute() with plain Connection.
         */
    Properties props = new Properties();
    props.setProperty(PropertyKey.useSSL.getKeyName(), "false");
    props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
    testConn = getConnectionWithProps(props);
    // safety check
    testBug71396StatementMultiCheck(testConn, queries, new int[] { 2, 4, 3, 3, 3 });
    // initialize Statement with a given maxRow value, keep open until end of the case
    testStmt = testBug71396StatementInit(testConn, 1);
    // check results count using the same Statement[maxRows = 1] for all queries
    testBug71396StatementMultiCheck(testStmt, queries, new int[] { 1, 1, 1, 1, 1 });
    // check results count using same Connection and one new Statement[default maxRows] per query
    testBug71396StatementMultiCheck(testConn, queries, new int[] { 2, 4, 3, 3, 3 });
    // recheck results count reusing the first Statement[maxRows = 1] for all queries - confirm maxRows wasn't lost
    testBug71396StatementMultiCheck(testStmt, queries, new int[] { 1, 1, 1, 1, 1 });
    testStmt.close();
    testConn.close();
    /*
         * Case 2: PreparedStatement.executeQuery() and PreparedStatement.execute() with plain Connection.
         */
    testConn = getConnectionWithProps(props);
    // safety check
    testBug71396PrepStatementMultiCheck(testConn, queries, new int[] { 2, 4, 3, 3, 3 });
    // initialize Statement with a given maxRow value, keep open until end of the case
    testStmt = testBug71396StatementInit(testConn, 1);
    // initialize a set of PreparedStatements with a given maxRow value, keep open until end of the case
    testPStmtSet = testBug71396PrepStatementInit(testConn, queries, 1);
    // check results count using same Connection and one PreparedStatement[maxRows = 1] per query
    testBug71396PrepStatementMultiCheck(testPStmtSet, queries, new int[] { 1, 1, 1, 1, 1 });
    // check results count using same Connection and one new PreparedStatement[default maxRows] per query
    testBug71396PrepStatementMultiCheck(testConn, queries, new int[] { 2, 4, 3, 3, 3 });
    // check results count reusing the first PreparedStatement[maxRows = 1] per query - confirm maxRows wasn't lost
    testBug71396PrepStatementMultiCheck(testPStmtSet, queries, new int[] { 1, 1, 1, 1, 1 });
    testBug71396PrepStatementClose(testPStmtSet);
    testStmt.close();
    testConn.close();
    /*
         * Case 3: PreparedStatement.executeQuery() and PreparedStatement.execute() with
         * Connection[useServerPrepStmts=true].
         */
    props.setProperty(PropertyKey.useServerPrepStmts.getKeyName(), "true");
    testConn = getConnectionWithProps(props);
    // safety check
    testBug71396PrepStatementMultiCheck(testConn, queries, new int[] { 2, 4, 3, 3, 3 });
    // initialize Statement with a given maxRow value, keep open until end of the case.
    testStmt = testBug71396StatementInit(testConn, 1);
    // initialize a set of PreparedStatements with a given maxRow value, keep open until end of the case
    testPStmtSet = testBug71396PrepStatementInit(testConn, queries, 1);
    // check results count using same Connection and one PreparedStatement[maxRows = 1] per query
    testBug71396PrepStatementMultiCheck(testPStmtSet, queries, new int[] { 1, 1, 1, 1, 1 });
    // check results count using same Connection and one new PreparedStatement[default maxRows] per query
    testBug71396PrepStatementMultiCheck(testConn, queries, new int[] { 2, 4, 3, 3, 3 });
    // check results count reusing the first PreparedStatement[maxRows = 1] per query - confirm maxRows wasn't lost
    testBug71396PrepStatementMultiCheck(testPStmtSet, queries, new int[] { 1, 1, 1, 1, 1 });
    testBug71396PrepStatementClose(testPStmtSet);
    testStmt.close();
    testConn.close();
    /*
         * Case 4: Statement.executeQuery() and Statement.execute() with Connection[maxRows=2].
         */
    props.remove(PropertyKey.useServerPrepStmts.getKeyName());
    props.setProperty(PropertyKey.maxRows.getKeyName(), "2");
    testConn = getConnectionWithProps(props);
    // safety check
    testBug71396StatementMultiCheck(testConn, queries, new int[] { 2, 2, 2, 2, 2 });
    // initialize Statement with a given maxRow value, keep open until end of the case
    testStmt = testBug71396StatementInit(testConn, 1);
    // check results count using the same Statement[maxRows = 1] for all queries
    testBug71396StatementMultiCheck(testStmt, queries, new int[] { 1, 1, 1, 1, 1 });
    // check results count using same Connection and one new Statement[default maxRows] per query
    testBug71396StatementMultiCheck(testConn, queries, new int[] { 2, 2, 2, 2, 2 });
    // recheck results count reusing the first Statement[maxRows = 1] for all queries - confirm maxRows wasn't lost
    testBug71396StatementMultiCheck(testStmt, queries, new int[] { 1, 1, 1, 1, 1 });
    testStmt.close();
    testConn.close();
    /*
         * Case 5: PreparedStatement.executeQuery() and PreparedStatement.execute() with Connection[maxRows=2].
         */
    testConn = getConnectionWithProps(props);
    // safety check
    testBug71396PrepStatementMultiCheck(testConn, queries, new int[] { 2, 2, 2, 2, 2 });
    // initialize Statement with a given maxRow value, keep open until end of the case
    testStmt = testBug71396StatementInit(testConn, 1);
    // initialize a set of PreparedStatements with a given maxRow value, keep open until end of the case
    testPStmtSet = testBug71396PrepStatementInit(testConn, queries, 1);
    // check results count using same Connection and one PreparedStatement[maxRows = 1] per query
    testBug71396PrepStatementMultiCheck(testPStmtSet, queries, new int[] { 1, 1, 1, 1, 1 });
    // check results count using same Connection and one new PreparedStatement[default maxRows] per query
    testBug71396PrepStatementMultiCheck(testConn, queries, new int[] { 2, 2, 2, 2, 2 });
    // check results count reusing the first PreparedStatement[maxRows = 1] per query - confirm maxRows wasn't lost
    testBug71396PrepStatementMultiCheck(testPStmtSet, queries, new int[] { 1, 1, 1, 1, 1 });
    testBug71396PrepStatementClose(testPStmtSet);
    testStmt.close();
    testConn.close();
    /*
         * Case 6: PreparedStatement.executeQuery() and PreparedStatement.execute() with
         * Connection[useServerPrepStmts=true;maxRows=2].
         */
    props.setProperty(PropertyKey.useServerPrepStmts.getKeyName(), "true");
    testConn = getConnectionWithProps(props);
    // safety check
    testBug71396PrepStatementMultiCheck(testConn, queries, new int[] { 2, 2, 2, 2, 2 });
    // initialize Statement with a given maxRow value, keep open until end of the case
    testStmt = testBug71396StatementInit(testConn, 1);
    // initialize a set of PreparedStatements with a given maxRow value, keep open until end of the case
    testPStmtSet = testBug71396PrepStatementInit(testConn, queries, 1);
    // check results count using same Connection and one PreparedStatement[maxRows = 1] per query
    testBug71396PrepStatementMultiCheck(testPStmtSet, queries, new int[] { 1, 1, 1, 1, 1 });
    // check results count using same Connection and one new PreparedStatement[default maxRows] per query
    testBug71396PrepStatementMultiCheck(testConn, queries, new int[] { 2, 2, 2, 2, 2 });
    // check results count reusing the first PreparedStatement[maxRows = 1] per query - confirm maxRows wasn't lost
    testBug71396PrepStatementMultiCheck(testPStmtSet, queries, new int[] { 1, 1, 1, 1, 1 });
    testBug71396PrepStatementClose(testPStmtSet);
    testStmt.close();
    testConn.close();
    /*
         * Case 7: Multiple combinations between maxRows connection prop, Statement.setMaxRows() and LIMIT clause.
         * Covers some cases not tested previously.
         */
    props.clear();
    props.setProperty(PropertyKey.useSSL.getKeyName(), "false");
    props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
    testBug71396MultiSettingsCheck(props, -1, 1, 1);
    testBug71396MultiSettingsCheck(props, -1, 2, 2);
    testBug71396MultiSettingsCheck(props, 1, 1, 1);
    testBug71396MultiSettingsCheck(props, 1, 2, 1);
    testBug71396MultiSettingsCheck(props, 2, 1, 1);
    testBug71396MultiSettingsCheck(props, 2, 2, 2);
    props.setProperty(PropertyKey.maxRows.getKeyName(), "1");
    testBug71396MultiSettingsCheck(props, -1, 1, 1);
    testBug71396MultiSettingsCheck(props, -1, 2, 1);
    testBug71396MultiSettingsCheck(props, 1, 1, 1);
    testBug71396MultiSettingsCheck(props, 1, 2, 1);
    testBug71396MultiSettingsCheck(props, 2, 1, 1);
    testBug71396MultiSettingsCheck(props, 2, 2, 2);
    props.setProperty(PropertyKey.maxRows.getKeyName(), "2");
    testBug71396MultiSettingsCheck(props, -1, 1, 1);
    testBug71396MultiSettingsCheck(props, -1, 2, 2);
    testBug71396MultiSettingsCheck(props, 1, 1, 1);
    testBug71396MultiSettingsCheck(props, 1, 2, 1);
    testBug71396MultiSettingsCheck(props, 2, 1, 1);
    testBug71396MultiSettingsCheck(props, 2, 2, 2);
    // Case 8: New session due to user change
    createUser("'testBug71396User'@'%'", "IDENTIFIED BY 'testBug71396User'");
    this.stmt.execute("GRANT SELECT ON *.* TO 'testBug71396User'@'%'");
    props.clear();
    props.setProperty(PropertyKey.useSSL.getKeyName(), "false");
    props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
    testConn = getConnectionWithProps(props);
    testStmt = testBug71396StatementInit(testConn, 5);
    ((JdbcConnection) testConn).changeUser("testBug71396User", "testBug71396User");
    Statement testStmtTmp = testConn.createStatement();
    testRS = testStmtTmp.executeQuery("SELECT CURRENT_USER(), @@SESSION.SQL_SELECT_LIMIT");
    assertTrue(testRS.next());
    assertEquals("testBug71396User@%", testRS.getString(1));
    assertTrue(testRS.getBigDecimal(2).compareTo(new BigDecimal(Integer.MAX_VALUE)) == 1, String.format("expected:higher than<%d> but was:<%s>", Integer.MAX_VALUE, testRS.getBigDecimal(2)));
    testRS.close();
    testStmtTmp.close();
    testRS = testStmt.executeQuery("SELECT CURRENT_USER(), @@SESSION.SQL_SELECT_LIMIT");
    assertTrue(testRS.next());
    assertEquals("testBug71396User@%", testRS.getString(1));
    assertEquals(new BigDecimal(5), testRS.getBigDecimal(2));
    testRS.close();
    testStmt.close();
    testConn.close();
    // Case 9: New session due to reconnection
    testConn = getConnectionWithProps(props);
    testStmt = testBug71396StatementInit(testConn, 5);
    // true or false argument is irrelevant for this test case
    ((JdbcConnection) testConn).createNewIO(true);
    testStmtTmp = testConn.createStatement();
    testRS = testStmtTmp.executeQuery("SELECT @@SESSION.SQL_SELECT_LIMIT");
    assertTrue(testRS.next());
    assertTrue(testRS.getBigDecimal(1).compareTo(new BigDecimal(Integer.MAX_VALUE)) == 1, String.format("expected:higher than<%d> but was:<%s>", Integer.MAX_VALUE, testRS.getBigDecimal(1)));
    testRS.close();
    testStmtTmp.close();
    testRS = testStmt.executeQuery("SELECT @@SESSION.SQL_SELECT_LIMIT");
    assertTrue(testRS.next());
    assertEquals(new BigDecimal(5), testRS.getBigDecimal(1));
    testRS.close();
    testStmt.close();
    testConn.close();
}
Also used : JdbcStatement(com.mysql.cj.jdbc.JdbcStatement) JdbcPreparedStatement(com.mysql.cj.jdbc.JdbcPreparedStatement) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) ServerPreparedStatement(com.mysql.cj.jdbc.ServerPreparedStatement) ClientPreparedStatement(com.mysql.cj.jdbc.ClientPreparedStatement) ReplicationConnection(com.mysql.cj.jdbc.ha.ReplicationConnection) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) MysqlConnection(com.mysql.cj.MysqlConnection) ResultSet(java.sql.ResultSet) JdbcPreparedStatement(com.mysql.cj.jdbc.JdbcPreparedStatement) PreparedStatement(java.sql.PreparedStatement) ServerPreparedStatement(com.mysql.cj.jdbc.ServerPreparedStatement) ClientPreparedStatement(com.mysql.cj.jdbc.ClientPreparedStatement) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) Properties(java.util.Properties) BigDecimal(java.math.BigDecimal) StatementsTest(testsuite.simple.StatementsTest) Test(org.junit.jupiter.api.Test)

Example 12 with JdbcConnection

use of com.mysql.cj.jdbc.JdbcConnection in project JavaSegundasQuintas by ecteruel.

the class StatementRegressionTest method testBug74998.

/**
 * Tests fix for BUG#74998 - readRemainingMultiPackets not computed correctly for rows larger than 16 MB.
 *
 * This bug is observed only when a multipacket uses packets 127 and 128. It happens due to the transition from positive to negative values in a signed byte
 * numeric value (127 + 1 == -128).
 *
 * The test case forces a multipacket to use packets 127, 128 and 129, where packet 129 is 0-length, this being another boundary case.
 * Query (*1) generates the following MySQL protocol packets from the server:
 * - Packets 1 to 4 contain protocol control data and results metadata info. (*2)
 * - Packets 5 to 126 contain each row "X". (*3)
 * - Packets 127 to 129 contain row "Y..." as a multipacket (size("Y...") = 32*1024*1024-15 requires 3 packets). (*4)
 * - Packet 130 contains row "Z". (*5)
 *
 * @throws Exception
 */
@Test
public void testBug74998() throws Exception {
    int maxAllowedPacketAtServer = Integer.parseInt(((JdbcConnection) this.conn).getSession().getServerSession().getServerVariable("max_allowed_packet"));
    int maxAllowedPacketMinimumForTest = 32 * 1024 * 1024;
    boolean changeMaxAllowedPacket = maxAllowedPacketAtServer < maxAllowedPacketMinimumForTest;
    if (!versionMeetsMinimum(5, 7)) {
        this.rs = this.stmt.executeQuery("SHOW VARIABLES LIKE 'innodb_log_file_size'");
        this.rs.next();
        long defaultInnodbLogFileSize = this.rs.getInt(2);
        assumeFalse(defaultInnodbLogFileSize < maxAllowedPacketMinimumForTest * 10, "This test requires innodb_log_file_size > " + (maxAllowedPacketMinimumForTest * 10));
    }
    // (*2)
    createTable("testBug74998", "(id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, data LONGBLOB)");
    Connection con1 = null;
    try {
        if (changeMaxAllowedPacket) {
            this.stmt.executeUpdate("SET GLOBAL max_allowed_packet=" + maxAllowedPacketMinimumForTest);
        }
        StringBuilder query = new StringBuilder("INSERT INTO testBug74998 (data) VALUES ('X')");
        for (int i = 0; i < 121; i++) {
            query.append(",('X')");
        }
        Properties props = new Properties();
        props.setProperty(PropertyKey.useSSL.getKeyName(), "false");
        props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
        con1 = getConnectionWithProps(props);
        Statement st = con1.createStatement();
        // (*3)
        assertEquals(122, st.executeUpdate(query.toString()));
        // 32MB - 15Bytes causes an empty packet at the end of the multipacket sequence
        int lengthOfRowForMultiPacket = maxAllowedPacketMinimumForTest - 15;
        // (*4)
        st.executeUpdate("INSERT INTO testBug74998 (data) VALUES (REPEAT('Y', " + lengthOfRowForMultiPacket + "))");
        // (*5)
        st.executeUpdate("INSERT INTO testBug74998 (data) VALUES ('Z')");
        try {
            // (*1)
            this.rs = st.executeQuery("SELECT id, data FROM testBug74998 ORDER BY id");
        } catch (CJCommunicationsException | CommunicationsException e) {
            assertFalse(e.getCause() instanceof IOException && "Packets received out of order".compareTo(e.getCause().getMessage()) == 0, "Failed to correctly fetch all data from communications layer due to wrong processing of muli-packet number.");
        }
        // safety check
        for (int i = 1; i <= 122; i++) {
            assertTrue(this.rs.next());
            assertEquals(i, this.rs.getInt(1));
            assertEquals("X", this.rs.getString(2));
        }
        assertTrue(this.rs.next());
        assertEquals(123, this.rs.getInt(1));
        assertEquals("YYYYY", this.rs.getString(2).substring(0, 5));
        assertEquals("YYYYY", this.rs.getString(2).substring(lengthOfRowForMultiPacket - 5));
        assertTrue(this.rs.next());
        assertEquals(124, this.rs.getInt(1));
        assertEquals("Z", this.rs.getString(2));
        assertFalse(this.rs.next());
    } finally {
        if (changeMaxAllowedPacket) {
            this.stmt.executeUpdate("SET GLOBAL max_allowed_packet=" + maxAllowedPacketAtServer);
        }
        if (con1 != null) {
            con1.close();
        }
    }
}
Also used : JdbcStatement(com.mysql.cj.jdbc.JdbcStatement) JdbcPreparedStatement(com.mysql.cj.jdbc.JdbcPreparedStatement) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) ServerPreparedStatement(com.mysql.cj.jdbc.ServerPreparedStatement) ClientPreparedStatement(com.mysql.cj.jdbc.ClientPreparedStatement) ReplicationConnection(com.mysql.cj.jdbc.ha.ReplicationConnection) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) MysqlConnection(com.mysql.cj.MysqlConnection) IOException(java.io.IOException) Properties(java.util.Properties) CJCommunicationsException(com.mysql.cj.exceptions.CJCommunicationsException) CommunicationsException(com.mysql.cj.jdbc.exceptions.CommunicationsException) CJCommunicationsException(com.mysql.cj.exceptions.CJCommunicationsException) StatementsTest(testsuite.simple.StatementsTest) Test(org.junit.jupiter.api.Test)

Example 13 with JdbcConnection

use of com.mysql.cj.jdbc.JdbcConnection in project JavaSegundasQuintas by ecteruel.

the class StatementRegressionTest method testBug23201930.

/**
 * Tests fix for Bug#23201930 - CLIENT HANG WHEN RSLT CUNCURRENCY=CONCUR_UPDATABLE AND RSLTSET TYPE=FORWARD_ONLY.
 *
 * @throws Exception
 */
@Test
public void testBug23201930() throws Exception {
    assumeTrue((((MysqlConnection) this.conn).getSession().getServerSession().getCapabilities().getCapabilityFlags() & NativeServerSession.CLIENT_SSL) != 0, "This test requires server with SSL support.");
    assumeTrue(supportsTestCertificates(this.stmt), "This test requires the server configured with SSL certificates from ConnectorJ/src/test/config/ssl-test-certs");
    boolean useSSL = false;
    boolean useSPS = false;
    boolean useCursor = false;
    boolean useCompr = false;
    final char[] chars = new char[32 * 1024];
    Arrays.fill(chars, 'x');
    // Using large data makes SSL connections hang sometimes.
    final String longData = String.valueOf(chars);
    do {
        final String testCase = String.format("Case [SSL: %s, SPS: %s, Cursor: %s, Compr: %s]", useSSL ? "Y" : "N", useSPS ? "Y" : "N", useCursor ? "Y" : "N", useCompr ? "Y" : "N");
        createTable("testBug23201930", "(id TINYINT AUTO_INCREMENT PRIMARY KEY, f1 INT DEFAULT 1, f2 INT DEFAULT 1, f3 INT DEFAULT 1, " + "f4 INT DEFAULT 1, f5 INT DEFAULT 1, fl LONGBLOB)");
        final Properties props = new Properties();
        props.setProperty(PropertyKey.useSSL.getKeyName(), Boolean.toString(useSSL));
        props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
        if (useSSL) {
            props.setProperty(PropertyKey.requireSSL.getKeyName(), "true");
            props.setProperty(PropertyKey.verifyServerCertificate.getKeyName(), "false");
        }
        props.setProperty(PropertyKey.useServerPrepStmts.getKeyName(), Boolean.toString(useSPS));
        props.setProperty(PropertyKey.useCursorFetch.getKeyName(), Boolean.toString(useCursor));
        if (useCursor) {
            props.setProperty(PropertyKey.defaultFetchSize.getKeyName(), "1");
        }
        props.setProperty(PropertyKey.useCompression.getKeyName(), Boolean.toString(useCompr));
        final JdbcConnection testConn = (JdbcConnection) getConnectionWithProps(props);
        final ExecutorService executor = Executors.newSingleThreadExecutor();
        final Future<Void> future = executor.submit(new Callable<Void>() {

            public Void call() throws Exception {
                final Statement testStmt = testConn.createStatement();
                testStmt.execute("INSERT INTO testBug23201930 (id) VALUES (100)");
                PreparedStatement testPstmt = testConn.prepareStatement("INSERT INTO testBug23201930 (id, fl) VALUES (?, ?)", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
                testPstmt.setObject(1, 101, java.sql.Types.INTEGER);
                testPstmt.setObject(2, longData, java.sql.Types.VARCHAR);
                testPstmt.execute();
                testPstmt.setObject(1, 102, java.sql.Types.INTEGER);
                testPstmt.execute();
                testPstmt.close();
                testPstmt = testConn.prepareStatement("SELECT * FROM testBug23201930 WHERE id >= ? ORDER BY id ASC", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
                testPstmt.setObject(1, 100, java.sql.Types.INTEGER);
                final ResultSet testRs = testPstmt.executeQuery();
                assertTrue(testRs.next());
                assertEquals(100, testRs.getInt(1));
                assertTrue(testRs.next());
                assertEquals(101, testRs.getInt(1));
                assertTrue(testRs.next());
                assertEquals(102, testRs.getInt(1));
                assertFalse(testRs.next());
                testPstmt.close();
                return null;
            }
        });
        try {
            future.get(10, TimeUnit.SECONDS);
        } catch (TimeoutException e) {
            // The connection hung, forcibly closing it releases resources.
            this.stmt.executeQuery("KILL CONNECTION " + testConn.getSession().getThreadId());
            fail(testCase + ": Connection hung!");
        }
        executor.shutdownNow();
        testConn.close();
    } while (// Cycle through all possible combinations.
    (useSSL = !useSSL) || (useSPS = !useSPS) || (useCursor = !useCursor) || (useCompr = !useCompr));
}
Also used : JdbcStatement(com.mysql.cj.jdbc.JdbcStatement) JdbcPreparedStatement(com.mysql.cj.jdbc.JdbcPreparedStatement) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) ServerPreparedStatement(com.mysql.cj.jdbc.ServerPreparedStatement) ClientPreparedStatement(com.mysql.cj.jdbc.ClientPreparedStatement) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) JdbcPreparedStatement(com.mysql.cj.jdbc.JdbcPreparedStatement) PreparedStatement(java.sql.PreparedStatement) ServerPreparedStatement(com.mysql.cj.jdbc.ServerPreparedStatement) ClientPreparedStatement(com.mysql.cj.jdbc.ClientPreparedStatement) Properties(java.util.Properties) WrongArgumentException(com.mysql.cj.exceptions.WrongArgumentException) CJCommunicationsException(com.mysql.cj.exceptions.CJCommunicationsException) BatchUpdateException(java.sql.BatchUpdateException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) MySQLTimeoutException(com.mysql.cj.jdbc.exceptions.MySQLTimeoutException) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) CommunicationsException(com.mysql.cj.jdbc.exceptions.CommunicationsException) ExecutorService(java.util.concurrent.ExecutorService) ResultSet(java.sql.ResultSet) TimeoutException(java.util.concurrent.TimeoutException) MySQLTimeoutException(com.mysql.cj.jdbc.exceptions.MySQLTimeoutException) StatementsTest(testsuite.simple.StatementsTest) Test(org.junit.jupiter.api.Test)

Example 14 with JdbcConnection

use of com.mysql.cj.jdbc.JdbcConnection in project JavaSegundasQuintas by ecteruel.

the class StatementRegressionTest method testBug66430.

/**
 * Tests fix for Bug#66430 - setCatalog on connection leaves ServerPreparedStatement cache for old catalog.
 *
 * @throws Exception
 */
@Test
public void testBug66430() throws Exception {
    createDatabase("testBug66430DB1");
    createTable("testBug66430DB1.testBug66430", "(id INT)");
    this.stmt.executeUpdate("INSERT INTO testBug66430DB1.testBug66430 VALUES (1)");
    createDatabase("testBug66430DB2");
    createTable("testBug66430DB2.testBug66430", "(id INT)");
    this.stmt.executeUpdate("INSERT INTO testBug66430DB2.testBug66430 VALUES (2)");
    boolean useSPS = false;
    boolean cachePS = false;
    do {
        final String testCase = String.format("Case: [useSPS: %s, cachePS: %s ]", useSPS ? "Y" : "N", cachePS ? "Y" : "N");
        Properties props = new Properties();
        props.setProperty(PropertyKey.useSSL.getKeyName(), "false");
        props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
        props.setProperty(PropertyKey.cachePrepStmts.getKeyName(), Boolean.toString(cachePS));
        props.setProperty(PropertyKey.useServerPrepStmts.getKeyName(), Boolean.toString(useSPS));
        Connection testConn = getConnectionWithProps(props);
        if (((JdbcConnection) testConn).getPropertySet().<DatabaseTerm>getEnumProperty(PropertyKey.databaseTerm).getValue() == DatabaseTerm.SCHEMA) {
            testConn.setSchema("testBug66430DB1");
        } else {
            testConn.setCatalog("testBug66430DB1");
        }
        PreparedStatement testPStmt = testConn.prepareStatement("SELECT * FROM testBug66430 WHERE id > ?");
        testPStmt.setInt(1, 0);
        this.rs = testPStmt.executeQuery();
        assertTrue(this.rs.next(), testCase);
        assertEquals(1, this.rs.getInt(1), testCase);
        assertFalse(this.rs.next(), testCase);
        testPStmt.close();
        if (((JdbcConnection) testConn).getPropertySet().<DatabaseTerm>getEnumProperty(PropertyKey.databaseTerm).getValue() == DatabaseTerm.SCHEMA) {
            testConn.setSchema("testBug66430DB2");
        } else {
            testConn.setCatalog("testBug66430DB2");
        }
        testPStmt = testConn.prepareStatement("SELECT * FROM testBug66430 WHERE id > ?");
        testPStmt.setInt(1, 0);
        this.rs = testPStmt.executeQuery();
        assertTrue(this.rs.next(), testCase);
        assertEquals(2, this.rs.getInt(1), testCase);
        assertFalse(this.rs.next(), testCase);
        testPStmt.close();
        // Do it again to make sure cached prepared statements behave correctly.
        if (((JdbcConnection) testConn).getPropertySet().<DatabaseTerm>getEnumProperty(PropertyKey.databaseTerm).getValue() == DatabaseTerm.SCHEMA) {
            testConn.setSchema("testBug66430DB1");
        } else {
            testConn.setCatalog("testBug66430DB1");
        }
        testPStmt = testConn.prepareStatement("SELECT * FROM testBug66430 WHERE id > ?");
        testPStmt.setInt(1, 0);
        this.rs = testPStmt.executeQuery();
        assertTrue(this.rs.next(), testCase);
        assertEquals(1, this.rs.getInt(1), testCase);
        assertFalse(this.rs.next(), testCase);
        testPStmt.close();
        if (((JdbcConnection) testConn).getPropertySet().<DatabaseTerm>getEnumProperty(PropertyKey.databaseTerm).getValue() == DatabaseTerm.SCHEMA) {
            testConn.setSchema("testBug66430DB2");
        } else {
            testConn.setCatalog("testBug66430DB2");
        }
        testPStmt = testConn.prepareStatement("SELECT * FROM testBug66430 WHERE id > ?");
        testPStmt.setInt(1, 0);
        this.rs = testPStmt.executeQuery();
        assertTrue(this.rs.next(), testCase);
        assertEquals(2, this.rs.getInt(1), testCase);
        assertFalse(this.rs.next(), testCase);
        testPStmt.close();
        testConn.close();
    } while ((useSPS = !useSPS) || (cachePS = !cachePS));
}
Also used : DatabaseTerm(com.mysql.cj.conf.PropertyDefinitions.DatabaseTerm) ReplicationConnection(com.mysql.cj.jdbc.ha.ReplicationConnection) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) MysqlConnection(com.mysql.cj.MysqlConnection) JdbcPreparedStatement(com.mysql.cj.jdbc.JdbcPreparedStatement) PreparedStatement(java.sql.PreparedStatement) ServerPreparedStatement(com.mysql.cj.jdbc.ServerPreparedStatement) ClientPreparedStatement(com.mysql.cj.jdbc.ClientPreparedStatement) Properties(java.util.Properties) StatementsTest(testsuite.simple.StatementsTest) Test(org.junit.jupiter.api.Test)

Example 15 with JdbcConnection

use of com.mysql.cj.jdbc.JdbcConnection in project JavaSegundasQuintas by ecteruel.

the class StatementRegressionTest method testBug3557.

/**
 * Test fix for BUG#3557 -- UpdatableResultSet not picking up default values
 *
 * @throws Exception
 */
@Test
public void testBug3557() throws Exception {
    boolean populateDefaults = ((JdbcConnection) this.conn).getPropertySet().getBooleanProperty(PropertyKey.populateInsertRowWithDefaultValues).getValue();
    try {
        ((JdbcConnection) this.conn).getPropertySet().getBooleanProperty(PropertyKey.populateInsertRowWithDefaultValues).setValue(true);
        this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug3557");
        this.stmt.executeUpdate("CREATE TABLE testBug3557 (`a` varchar(255) NOT NULL default 'XYZ', `b` varchar(255) default '123', PRIMARY KEY  (`a`(100)))");
        Statement updStmt = this.conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        this.rs = updStmt.executeQuery("SELECT * FROM testBug3557");
        assertTrue(this.rs.getConcurrency() == ResultSet.CONCUR_UPDATABLE);
        this.rs.moveToInsertRow();
        assertEquals("XYZ", this.rs.getObject(1));
        assertEquals("123", this.rs.getObject(2));
    } finally {
        ((JdbcConnection) this.conn).getPropertySet().getBooleanProperty(PropertyKey.populateInsertRowWithDefaultValues).setValue(populateDefaults);
        this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug3557");
    }
}
Also used : JdbcStatement(com.mysql.cj.jdbc.JdbcStatement) JdbcPreparedStatement(com.mysql.cj.jdbc.JdbcPreparedStatement) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) ServerPreparedStatement(com.mysql.cj.jdbc.ServerPreparedStatement) ClientPreparedStatement(com.mysql.cj.jdbc.ClientPreparedStatement) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) StatementsTest(testsuite.simple.StatementsTest) Test(org.junit.jupiter.api.Test)

Aggregations

JdbcConnection (com.mysql.cj.jdbc.JdbcConnection)280 Properties (java.util.Properties)216 Test (org.junit.jupiter.api.Test)207 Connection (java.sql.Connection)175 MysqlConnection (com.mysql.cj.MysqlConnection)149 XAConnection (javax.sql.XAConnection)100 SQLException (java.sql.SQLException)97 ReplicationConnection (com.mysql.cj.jdbc.ha.ReplicationConnection)96 PooledConnection (javax.sql.PooledConnection)90 MysqlPooledConnection (com.mysql.cj.jdbc.MysqlPooledConnection)81 MysqlXAConnection (com.mysql.cj.jdbc.MysqlXAConnection)79 SuspendableXAConnection (com.mysql.cj.jdbc.SuspendableXAConnection)78 Statement (java.sql.Statement)77 ServerPreparedStatement (com.mysql.cj.jdbc.ServerPreparedStatement)66 PreparedStatement (java.sql.PreparedStatement)66 ResultSet (java.sql.ResultSet)65 ClientPreparedStatement (com.mysql.cj.jdbc.ClientPreparedStatement)60 CommunicationsException (com.mysql.cj.jdbc.exceptions.CommunicationsException)60 IOException (java.io.IOException)54 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)45