Java Code Examples for com.mysql.cj.log.StandardLogger

The following examples show how to use com.mysql.cj.log.StandardLogger. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: FoxTelem   Source File: ConnectionTest.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Tests whether or not the configuration 'useLocalSessionState' actually
 * prevents non-needed 'set autocommit=', 'set session transaction isolation
 * ...' and 'show variables like tx_isolation' queries.
 * 
 * @throws Exception
 *             if the test fails.
 */
public void testUseLocalSessionState() throws Exception {
    Properties props = new Properties();

    props.setProperty(PropertyKey.useLocalSessionState.getKeyName(), "true");
    props.setProperty(PropertyKey.profileSQL.getKeyName(), "true");
    props.setProperty(PropertyKey.logger.getKeyName(), StandardLogger.class.getName());

    Connection conn1 = getConnectionWithProps(props);
    conn1.setAutoCommit(true);
    conn1.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);

    StandardLogger.startLoggingToBuffer();

    conn1.setAutoCommit(true);
    conn1.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
    conn1.getTransactionIsolation();

    String logAsString = StandardLogger.getBuffer().toString();

    String s = versionMeetsMinimum(8, 0, 3) ? "transaction_isolation" : "tx_isolation";

    assertTrue(logAsString.indexOf("SET SESSION") == -1 && logAsString.indexOf("SHOW VARIABLES LIKE '" + s + "'") == -1
            && logAsString.indexOf("SET autocommit=") == -1);
}
 
Example 2
public void testBatch() throws Exception {
    Connection batchedConn = null;

    try {
        createTable("testBatchTable", "(field1 INT)");
        createProcedure("testBatch", "(IN foo VARCHAR(15))\nbegin\nINSERT INTO testBatchTable VALUES (foo);\nend\n");

        executeBatchedStoredProc(this.conn);

        batchedConn = getConnectionWithProps("logger=StandardLogger,rewriteBatchedStatements=true,profileSQL=true");

        StandardLogger.startLoggingToBuffer();
        executeBatchedStoredProc(batchedConn);
        String[] log = StandardLogger.getBuffer().toString().split(";");
        assertTrue(log.length > 20);
    } finally {
        StandardLogger.dropBuffer();

        if (batchedConn != null) {
            batchedConn.close();
        }
    }
}
 
Example 3
Source Project: quarkus   Source File: MySQLJDBCReflections.java    License: Apache License 2.0 5 votes vote down vote up
@BuildStep
void registerDriverForReflection(BuildProducer<ReflectiveClassBuildItem> reflectiveClass) {

    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, Driver.class.getName()));
    reflectiveClass.produce(
            new ReflectiveClassBuildItem(false, false, FailoverDnsSrvConnectionUrl.class.getName()));
    reflectiveClass.produce(
            new ReflectiveClassBuildItem(false, false, FailoverConnectionUrl.class.getName()));
    reflectiveClass
            .produce(new ReflectiveClassBuildItem(false, false, SingleConnectionUrl.class.getName()));
    reflectiveClass.produce(
            new ReflectiveClassBuildItem(false, false, LoadBalanceConnectionUrl.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false,
            LoadBalanceDnsSrvConnectionUrl.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false,
            ReplicationDnsSrvConnectionUrl.class.getName()));
    reflectiveClass.produce(
            new ReflectiveClassBuildItem(false, false, ReplicationConnectionUrl.class.getName()));
    reflectiveClass.produce(
            new ReflectiveClassBuildItem(false, false, XDevApiConnectionUrl.class.getName()));
    reflectiveClass.produce(
            new ReflectiveClassBuildItem(false, false, XDevApiDnsSrvConnectionUrl.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false,
            com.mysql.cj.jdbc.ha.LoadBalancedAutoCommitInterceptor.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, StandardLogger.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, Wrapper.class.getName()));
}
 
Example 4
Source Project: FoxTelem   Source File: ConnectionTest.java    License: GNU General Public License v3.0 5 votes vote down vote up
public void testServerConfigurationCache() throws Exception {
    Properties props = new Properties();

    props.setProperty(PropertyKey.cacheServerConfiguration.getKeyName(), "true");
    props.setProperty(PropertyKey.profileSQL.getKeyName(), "true");
    props.setProperty(PropertyKey.logger.getKeyName(), StandardLogger.class.getName());

    Connection conn1 = null;
    Connection conn2 = null;
    try {
        conn1 = getConnectionWithProps(props);

        try {
            // eliminate side-effects when not run in isolation
            StandardLogger.startLoggingToBuffer();

            conn2 = getConnectionWithProps(props);

            assertTrue("Configuration wasn't cached", StandardLogger.getBuffer().toString().indexOf("SHOW VARIABLES") == -1);

            assertTrue("Configuration wasn't cached", StandardLogger.getBuffer().toString().indexOf("SHOW COLLATION") == -1);
        } finally {
            StandardLogger.dropBuffer();
        }
    } finally {
        if (conn1 != null) {
            conn1.close();
        }
        if (conn2 != null) {
            conn2.close();
        }
    }
}
 
Example 5
Source Project: FoxTelem   Source File: ConnectionTest.java    License: GNU General Public License v3.0 5 votes vote down vote up
public void testUsageAdvisorTooLargeResultSet() throws Exception {
    Connection uaConn = null;

    PrintStream stderr = System.err;

    StandardLogger.startLoggingToBuffer();

    try {
        Properties props = new Properties();
        props.setProperty(PropertyKey.useUsageAdvisor.getKeyName(), "true");
        props.setProperty(PropertyKey.resultSetSizeThreshold.getKeyName(), "4");
        props.setProperty(PropertyKey.logger.getKeyName(), "StandardLogger");

        uaConn = getConnectionWithProps(props);
        this.rs = uaConn.createStatement().executeQuery("SHOW VARIABLES");
        this.rs.close();

        assertTrue("Result set threshold message not present",
                StandardLogger.getBuffer().toString().indexOf("larger than \"resultSetSizeThreshold\" of 4 rows") != -1);
    } finally {
        StandardLogger.dropBuffer();
        System.setErr(stderr);

        if (uaConn != null) {
            uaConn.close();
        }
    }
}
 
Example 6
Source Project: FoxTelem   Source File: ConnectionTest.java    License: GNU General Public License v3.0 4 votes vote down vote up
public void testUseLocalSessionStateRollback() throws Exception {
    if (!versionMeetsMinimum(5, 5, 0)) {
        return;
    }

    Properties props = new Properties();
    props.setProperty(PropertyKey.useLocalSessionState.getKeyName(), "true");
    props.setProperty(PropertyKey.useLocalTransactionState.getKeyName(), "true");
    props.setProperty(PropertyKey.profileSQL.getKeyName(), "true");

    StandardLogger.startLoggingToBuffer();

    createTable("testUseLocalSessionState", "(field1 varchar(32))", "InnoDB");

    Connection localStateConn = null;
    Statement localStateStmt = null;
    String searchIn = "";

    try {
        localStateConn = getConnectionWithProps(props);
        localStateStmt = localStateConn.createStatement();

        localStateConn.setAutoCommit(false);
        localStateStmt.executeUpdate("INSERT INTO testUseLocalSessionState VALUES ('abc')");
        localStateConn.rollback();
        localStateConn.rollback();
        localStateStmt.executeUpdate("INSERT INTO testUseLocalSessionState VALUES ('abc')");
        localStateConn.commit();
        localStateConn.commit();
        localStateStmt.close();
    } finally {
        searchIn = StandardLogger.getBuffer().toString();
        StandardLogger.dropBuffer();

        if (localStateStmt != null) {
            localStateStmt.close();
        }

        if (localStateConn != null) {
            localStateConn.close();
        }
    }

    int rollbackCount = 0;
    int rollbackPos = 0;

    // space is important here, we don't want to count occurrences in stack traces
    while (rollbackPos != -1) {
        rollbackPos = searchIn.indexOf(" rollback", rollbackPos);

        if (rollbackPos != -1) {
            rollbackPos += "rollback".length();
            rollbackCount++;
        }
    }

    assertEquals(1, rollbackCount);

    int commitCount = 0;
    int commitPos = 0;

    // space is important here, we don't want to count "autocommit" nor occurrences in stack traces
    while (commitPos != -1) {
        commitPos = searchIn.indexOf(" commit", commitPos);

        if (commitPos != -1) {
            commitPos += " commit".length();
            commitCount++;
        }
    }

    assertEquals(1, commitCount);
}
 
Example 7
Source Project: FoxTelem   Source File: ConnectionTest.java    License: GNU General Public License v3.0 4 votes vote down vote up
public void testReadOnly56() throws Exception {
    if (!versionMeetsMinimum(5, 6, 5)) {
        return;
    }
    try {
        Connection notLocalState = getConnectionWithProps("profileSQL=true");

        for (int i = 0; i < 2; i++) {
            StandardLogger.startLoggingToBuffer();
            notLocalState.setReadOnly(true);
            assertTrue(StandardLogger.getBuffer().toString().indexOf("set session transaction read only") != -1);
            notLocalState.createStatement().execute("set session transaction read write");
            assertFalse(notLocalState.isReadOnly());
        }

        for (int i = 0; i < 2; i++) {
            StandardLogger.startLoggingToBuffer();
            notLocalState.setReadOnly(false);
            assertTrue(StandardLogger.getBuffer().toString().indexOf("set session transaction read write") != -1);
            notLocalState.createStatement().execute("set session transaction read only");
            assertTrue(notLocalState.isReadOnly());
        }

        Connection localState = getConnectionWithProps("profileSQL=true,useLocalSessionState=true");

        String s = versionMeetsMinimum(8, 0, 3) ? "@@session.transaction_read_only" : "@@session.tx_read_only";

        for (int i = 0; i < 2; i++) {
            StandardLogger.startLoggingToBuffer();
            localState.setReadOnly(true);
            if (i == 0) {
                assertTrue(StandardLogger.getBuffer().toString().indexOf("set session transaction read only") != -1);
            } else {
                assertTrue(StandardLogger.getBuffer().toString().indexOf("set session transaction read only") == -1);
            }
            StandardLogger.startLoggingToBuffer();
            localState.isReadOnly();
            assertTrue(StandardLogger.getBuffer().toString().indexOf("select @@session." + s) == -1);
        }

        Connection noOptimization = getConnectionWithProps("profileSQL=true,readOnlyPropagatesToServer=false");

        for (int i = 0; i < 2; i++) {
            StandardLogger.startLoggingToBuffer();
            noOptimization.setReadOnly(true);
            assertTrue(StandardLogger.getBuffer().toString().indexOf("set session transaction read only") == -1);
            StandardLogger.startLoggingToBuffer();
            noOptimization.isReadOnly();
            assertTrue(StandardLogger.getBuffer().toString().indexOf("select @@session." + s) == -1);
        }
    } finally {
        StandardLogger.dropBuffer();
    }
}