Java Code Examples for java.sql.Connection.setCatalog()

The following are Jave code examples for showing how to use setCatalog() of the java.sql.Connection class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: tomcat7   File: TestConnectionState.java   View Source Code Vote up 7 votes
@Test
public void testDefaultCatalog() throws Exception {
    DataSourceProxy d1 = this.createDefaultDataSource();
    d1.setMaxActive(1);
    d1.setJdbcInterceptors(ConnectionState.class.getName());
    d1.setDefaultCatalog("information_schema");
    d1.setMinIdle(1);
    Connection c1 = d1.getConnection();
    Assert.assertEquals("Catalog should be information_schema",c1.getCatalog(),"information_schema");
    c1.close();
    c1 = d1.getConnection();
    Assert.assertEquals("Catalog should be information_schema",c1.getCatalog(),"information_schema");
    c1.setCatalog("mysql");
    Assert.assertEquals("Catalog should be information_schema",c1.getCatalog(),"mysql");
    c1.close();
    c1 = d1.getConnection();
    Assert.assertEquals("Catalog should be information_schema",c1.getCatalog(),"information_schema");
}
 
Example 2
Project: calcite-avatica   File: JdbcMeta.java   View Source Code Vote up 6 votes
protected void apply(Connection conn, ConnectionProperties connProps)
    throws SQLException {
  if (connProps.isAutoCommit() != null) {
    conn.setAutoCommit(connProps.isAutoCommit());
  }
  if (connProps.isReadOnly() != null) {
    conn.setReadOnly(connProps.isReadOnly());
  }
  if (connProps.getTransactionIsolation() != null) {
    conn.setTransactionIsolation(connProps.getTransactionIsolation());
  }
  if (connProps.getCatalog() != null) {
    conn.setCatalog(connProps.getCatalog());
  }
  if (connProps.getSchema() != null) {
    conn.setSchema(connProps.getSchema());
  }
}
 
Example 3
Project: the-vigilantes   File: CallableStatementRegressionTest.java   View Source Code Vote up 6 votes
/**
 * Tests fix for Bug#12417 - stored procedure catalog name is case-sensitive
 * on Windows (this is actually a server bug, but we have a workaround in
 * place for it now).
 * 
 * @throws Exception
 *             if the test fails.
 */
public void testBug12417() throws Exception {
    if (serverSupportsStoredProcedures() && isServerRunningOnWindows()) {

        createProcedure("testBug12417", "()\nBEGIN\nSELECT 1;end\n");

        Connection ucCatalogConn = null;

        try {
            ucCatalogConn = getConnectionWithProps((Properties) null);
            ucCatalogConn.setCatalog(this.conn.getCatalog().toUpperCase());
            ucCatalogConn.prepareCall("{call testBug12417()}");
        } finally {
            if (ucCatalogConn != null) {
                ucCatalogConn.close();
            }
        }
    }
}
 
Example 4
Project: mongosql-auth-java   File: MongoSqlAuthenticationPluginFunctionalTest.java   View Source Code Vote up 6 votes
@Test
public void testSuccessfulAuthentication() throws SQLException {
    assumeTrue(!"".equals(user));

    Connection connection = DriverManager.getConnection(url, user, password);

    try {
        connection.setCatalog(database);

        Statement stmt = connection.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        rs.next();
    } finally {
        connection.close();
    }

}
 
Example 5
Project: apache-tomcat-7.0.73-with-comment   File: TestConnectionState.java   View Source Code Vote up 6 votes
@Test
public void testDefaultCatalog() throws Exception {
    DataSourceProxy d1 = this.createDefaultDataSource();
    d1.setMaxActive(1);
    d1.setJdbcInterceptors(ConnectionState.class.getName());
    d1.setDefaultCatalog("information_schema");
    d1.setMinIdle(1);
    Connection c1 = d1.getConnection();
    Assert.assertEquals("Catalog should be information_schema",c1.getCatalog(),"information_schema");
    c1.close();
    c1 = d1.getConnection();
    Assert.assertEquals("Catalog should be information_schema",c1.getCatalog(),"information_schema");
    c1.setCatalog("mysql");
    Assert.assertEquals("Catalog should be information_schema",c1.getCatalog(),"mysql");
    c1.close();
    c1 = d1.getConnection();
    Assert.assertEquals("Catalog should be information_schema",c1.getCatalog(),"information_schema");
}
 
Example 6
Project: s-store   File: ConnectionDefaults.java   View Source Code Vote up 5 votes
public void setDefaults(Connection connection) throws SQLException {

        connection.setHoldability(this.holdability);

        if (this.transactionIsolation != Connection.TRANSACTION_NONE) {
            connection.setTransactionIsolation(this.transactionIsolation);
        }
        connection.setAutoCommit(this.isAutoCommit);
        connection.setReadOnly(this.isReadOnly);
        connection.setCatalog(this.catalog);
    }
 
Example 7
Project: BibliotecaPS   File: MetaDataRegressionTest.java   View Source Code Vote up 4 votes
/**
 * Tests fix for BUG#61150 - First call to SP
 * fails with "No Database Selected"
 * The workaround introduced in DatabaseMetaData.getCallStmtParameterTypes
 * to fix the bug in server where SHOW CREATE PROCEDURE was not respecting
 * lower-case table names is misbehaving when connection is not attached to
 * database and on non-casesensitive OS.
 * 
 * @throws Exception
 *             if the test fails.
 */
public void testBug61150() throws Exception {
    NonRegisteringDriver driver = new NonRegisteringDriver();
    Properties oldProps = driver.parseURL(BaseTestCase.dbUrl, null);

    String host = driver.host(oldProps);
    int port = driver.port(oldProps);
    StringBuilder newUrlToTestNoDB = new StringBuilder("jdbc:mysql://");
    if (host != null) {
        newUrlToTestNoDB.append(host);
    }
    newUrlToTestNoDB.append(":").append(port).append("/");

    Statement savedSt = this.stmt;

    Properties props = getHostFreePropertiesFromTestsuiteUrl();
    props.remove(NonRegisteringDriver.DBNAME_PROPERTY_KEY);
    Connection conn1 = DriverManager.getConnection(newUrlToTestNoDB.toString(), props);

    this.stmt = conn1.createStatement();
    createDatabase("TST1");
    createProcedure("TST1.PROC", "(x int, out y int)\nbegin\ndeclare z int;\nset z = x+1, y = z;\nend\n");

    CallableStatement cStmt = null;
    cStmt = conn1.prepareCall("{call `TST1`.`PROC`(?, ?)}");
    cStmt.setInt(1, 5);
    cStmt.registerOutParameter(2, Types.INTEGER);

    cStmt.execute();
    assertEquals(6, cStmt.getInt(2));
    cStmt.clearParameters();
    cStmt.close();

    conn1.setCatalog("TST1");
    cStmt = null;
    cStmt = conn1.prepareCall("{call TST1.PROC(?, ?)}");
    cStmt.setInt(1, 5);
    cStmt.registerOutParameter(2, Types.INTEGER);

    cStmt.execute();
    assertEquals(6, cStmt.getInt(2));
    cStmt.clearParameters();
    cStmt.close();

    conn1.setCatalog("mysql");
    cStmt = null;
    cStmt = conn1.prepareCall("{call `TST1`.`PROC`(?, ?)}");
    cStmt.setInt(1, 5);
    cStmt.registerOutParameter(2, Types.INTEGER);

    cStmt.execute();
    assertEquals(6, cStmt.getInt(2));
    cStmt.clearParameters();
    cStmt.close();

    this.stmt = savedSt;
}
 
Example 8
Project: the-vigilantes   File: ConnectionRegressionTest.java   View Source Code Vote up 4 votes
/**
 * Tests if the driver configures character sets correctly for 4.1.x
 * servers. Requires that the 'admin connection' is configured, as this test
 * needs to create/drop databases.
 * 
 * @throws Exception
 *             if an error occurs
 */
public void testCollation41() throws Exception {
    if (versionMeetsMinimum(4, 1) && isAdminConnectionConfigured()) {
        Map<String, String> charsetsAndCollations = getCharacterSetsAndCollations();
        charsetsAndCollations.remove("latin7"); // Maps to multiple Java
        // charsets
        charsetsAndCollations.remove("ucs2"); // can't be used as a
        // connection charset

        for (String charsetName : charsetsAndCollations.keySet()) {
            Connection charsetConn = null;
            Statement charsetStmt = null;

            try {
                //String collationName = charsetsAndCollations.get(charsetName);

                Properties props = new Properties();
                props.put("characterEncoding", charsetName);

                System.out.println("Testing character set " + charsetName);

                charsetConn = getAdminConnectionWithProps(props);

                charsetStmt = charsetConn.createStatement();

                charsetStmt.executeUpdate("DROP DATABASE IF EXISTS testCollation41");
                charsetStmt.executeUpdate("DROP TABLE IF EXISTS testCollation41");

                charsetStmt.executeUpdate("CREATE DATABASE testCollation41 DEFAULT CHARACTER SET " + charsetName);
                charsetConn.setCatalog("testCollation41");

                // We've switched catalogs, so we need to recreate the
                // statement to pick this up...
                charsetStmt = charsetConn.createStatement();

                StringBuilder createTableCommand = new StringBuilder("CREATE TABLE testCollation41(field1 VARCHAR(255), field2 INT)");

                charsetStmt.executeUpdate(createTableCommand.toString());

                charsetStmt.executeUpdate("INSERT INTO testCollation41 VALUES ('abc', 0)");

                int updateCount = charsetStmt.executeUpdate("UPDATE testCollation41 SET field2=1 WHERE field1='abc'");
                assertTrue(updateCount == 1);
            } finally {
                if (charsetStmt != null) {
                    charsetStmt.executeUpdate("DROP TABLE IF EXISTS testCollation41");
                    charsetStmt.executeUpdate("DROP DATABASE IF EXISTS testCollation41");
                    charsetStmt.close();
                }

                if (charsetConn != null) {
                    charsetConn.close();
                }
            }
        }
    }
}
 
Example 9
Project: the-vigilantes   File: ConnectionRegressionTest.java   View Source Code Vote up 4 votes
/**
 * Tests fix for BUG#15570 - ReplicationConnection incorrectly copies state,
 * doesn't transfer connection context correctly when transitioning between
 * the same read-only states.
 * 
 * (note, this test will fail if the test user doesn't have permission to
 * "USE 'mysql'".
 * 
 * @throws Exception
 *             if the test fails.
 */
public void testBug15570() throws Exception {
    Connection replConn = null;

    try {
        replConn = getMasterSlaveReplicationConnection();

        int masterConnectionId = Integer.parseInt(getSingleIndexedValueWithQuery(replConn, 1, "SELECT CONNECTION_ID()").toString());

        replConn.setReadOnly(false);

        assertEquals(masterConnectionId, Integer.parseInt(getSingleIndexedValueWithQuery(replConn, 1, "SELECT CONNECTION_ID()").toString()));

        String currentCatalog = replConn.getCatalog();

        replConn.setCatalog(currentCatalog);
        assertEquals(currentCatalog, replConn.getCatalog());

        replConn.setReadOnly(true);

        int slaveConnectionId = Integer.parseInt(getSingleIndexedValueWithQuery(replConn, 1, "SELECT CONNECTION_ID()").toString());

        // The following test is okay for now, as the chance of MySQL wrapping the connection id counter during our testsuite is very small.
        // As per Bug#21286268 fix a Replication connection first initializes the Slaves sub-connection, then the Masters.
        assertTrue("Master id " + masterConnectionId + " is not newer than slave id " + slaveConnectionId, masterConnectionId > slaveConnectionId);

        assertEquals(currentCatalog, replConn.getCatalog());

        String newCatalog = "mysql";

        replConn.setCatalog(newCatalog);
        assertEquals(newCatalog, replConn.getCatalog());

        replConn.setReadOnly(true);
        assertEquals(newCatalog, replConn.getCatalog());

        replConn.setReadOnly(false);
        assertEquals(masterConnectionId, Integer.parseInt(getSingleIndexedValueWithQuery(replConn, 1, "SELECT CONNECTION_ID()").toString()));
    } finally {
        if (replConn != null) {
            replConn.close();
        }
    }
}
 
Example 10
Project: the-vigilantes   File: StatementRegressionTest.java   View Source Code Vote up 4 votes
/**
 * Tests fix for Bug#66430 - setCatalog on connection leaves ServerPreparedStatement cache for old catalog.
 */
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("cachePrepStmts", Boolean.toString(cachePS));
        props.setProperty("useServerPrepStmts", Boolean.toString(useSPS));

        Connection testConn = getConnectionWithProps(props);

        testConn.setCatalog("testBug66430DB1");
        PreparedStatement testPStmt = testConn.prepareStatement("SELECT * FROM testBug66430 WHERE id > ?");
        testPStmt.setInt(1, 0);
        this.rs = testPStmt.executeQuery();
        assertTrue(testCase, this.rs.next());
        assertEquals(testCase, 1, this.rs.getInt(1));
        assertFalse(testCase, this.rs.next());
        testPStmt.close();

        testConn.setCatalog("testBug66430DB2");
        testPStmt = testConn.prepareStatement("SELECT * FROM testBug66430 WHERE id > ?");
        testPStmt.setInt(1, 0);
        this.rs = testPStmt.executeQuery();
        assertTrue(testCase, this.rs.next());
        assertEquals(testCase, 2, this.rs.getInt(1));
        assertFalse(testCase, this.rs.next());
        testPStmt.close();

        // Do it again to make sure cached prepared statements behave correctly.
        testConn.setCatalog("testBug66430DB1");
        testPStmt = testConn.prepareStatement("SELECT * FROM testBug66430 WHERE id > ?");
        testPStmt.setInt(1, 0);
        this.rs = testPStmt.executeQuery();
        assertTrue(testCase, this.rs.next());
        assertEquals(testCase, 1, this.rs.getInt(1));
        assertFalse(testCase, this.rs.next());
        testPStmt.close();

        testConn.setCatalog("testBug66430DB2");
        testPStmt = testConn.prepareStatement("SELECT * FROM testBug66430 WHERE id > ?");
        testPStmt.setInt(1, 0);
        this.rs = testPStmt.executeQuery();
        assertTrue(testCase, this.rs.next());
        assertEquals(testCase, 2, this.rs.getInt(1));
        assertFalse(testCase, this.rs.next());
        testPStmt.close();

        testConn.close();
    } while ((useSPS = !useSPS) || (cachePS = !cachePS));
}
 
Example 11
Project: the-vigilantes   File: MetaDataRegressionTest.java   View Source Code Vote up 4 votes
/**
 * Tests fix for BUG#61150 - First call to SP
 * fails with "No Database Selected"
 * The workaround introduced in DatabaseMetaData.getCallStmtParameterTypes
 * to fix the bug in server where SHOW CREATE PROCEDURE was not respecting
 * lower-case table names is misbehaving when connection is not attached to
 * database and on non-casesensitive OS.
 * 
 * @throws Exception
 *             if the test fails.
 */
public void testBug61150() throws Exception {
    NonRegisteringDriver driver = new NonRegisteringDriver();
    Properties oldProps = driver.parseURL(BaseTestCase.dbUrl, null);

    String host = driver.host(oldProps);
    int port = driver.port(oldProps);
    StringBuilder newUrlToTestNoDB = new StringBuilder("jdbc:mysql://");
    if (host != null) {
        newUrlToTestNoDB.append(host);
    }
    newUrlToTestNoDB.append(":").append(port).append("/");

    Statement savedSt = this.stmt;

    Properties props = getHostFreePropertiesFromTestsuiteUrl();
    props.remove(NonRegisteringDriver.DBNAME_PROPERTY_KEY);
    Connection conn1 = DriverManager.getConnection(newUrlToTestNoDB.toString(), props);

    this.stmt = conn1.createStatement();
    createDatabase("TST1");
    createProcedure("TST1.PROC", "(x int, out y int)\nbegin\ndeclare z int;\nset z = x+1, y = z;\nend\n");

    CallableStatement cStmt = null;
    cStmt = conn1.prepareCall("{call `TST1`.`PROC`(?, ?)}");
    cStmt.setInt(1, 5);
    cStmt.registerOutParameter(2, Types.INTEGER);

    cStmt.execute();
    assertEquals(6, cStmt.getInt(2));
    cStmt.clearParameters();
    cStmt.close();

    conn1.setCatalog("TST1");
    cStmt = null;
    cStmt = conn1.prepareCall("{call TST1.PROC(?, ?)}");
    cStmt.setInt(1, 5);
    cStmt.registerOutParameter(2, Types.INTEGER);

    cStmt.execute();
    assertEquals(6, cStmt.getInt(2));
    cStmt.clearParameters();
    cStmt.close();

    conn1.setCatalog("mysql");
    cStmt = null;
    cStmt = conn1.prepareCall("{call `TST1`.`PROC`(?, ?)}");
    cStmt.setInt(1, 5);
    cStmt.registerOutParameter(2, Types.INTEGER);

    cStmt.execute();
    assertEquals(6, cStmt.getInt(2));
    cStmt.clearParameters();
    cStmt.close();

    this.stmt = savedSt;
}
 
Example 12
Project: BibliotecaPS   File: StatementRegressionTest.java   View Source Code Vote up 4 votes
/**
 * Tests fix for Bug#66430 - setCatalog on connection leaves ServerPreparedStatement cache for old catalog.
 */
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("cachePrepStmts", Boolean.toString(cachePS));
        props.setProperty("useServerPrepStmts", Boolean.toString(useSPS));

        Connection testConn = getConnectionWithProps(props);

        testConn.setCatalog("testBug66430DB1");
        PreparedStatement testPStmt = testConn.prepareStatement("SELECT * FROM testBug66430 WHERE id > ?");
        testPStmt.setInt(1, 0);
        this.rs = testPStmt.executeQuery();
        assertTrue(testCase, this.rs.next());
        assertEquals(testCase, 1, this.rs.getInt(1));
        assertFalse(testCase, this.rs.next());
        testPStmt.close();

        testConn.setCatalog("testBug66430DB2");
        testPStmt = testConn.prepareStatement("SELECT * FROM testBug66430 WHERE id > ?");
        testPStmt.setInt(1, 0);
        this.rs = testPStmt.executeQuery();
        assertTrue(testCase, this.rs.next());
        assertEquals(testCase, 2, this.rs.getInt(1));
        assertFalse(testCase, this.rs.next());
        testPStmt.close();

        // Do it again to make sure cached prepared statements behave correctly.
        testConn.setCatalog("testBug66430DB1");
        testPStmt = testConn.prepareStatement("SELECT * FROM testBug66430 WHERE id > ?");
        testPStmt.setInt(1, 0);
        this.rs = testPStmt.executeQuery();
        assertTrue(testCase, this.rs.next());
        assertEquals(testCase, 1, this.rs.getInt(1));
        assertFalse(testCase, this.rs.next());
        testPStmt.close();

        testConn.setCatalog("testBug66430DB2");
        testPStmt = testConn.prepareStatement("SELECT * FROM testBug66430 WHERE id > ?");
        testPStmt.setInt(1, 0);
        this.rs = testPStmt.executeQuery();
        assertTrue(testCase, this.rs.next());
        assertEquals(testCase, 2, this.rs.getInt(1));
        assertFalse(testCase, this.rs.next());
        testPStmt.close();

        testConn.close();
    } while ((useSPS = !useSPS) || (cachePS = !cachePS));
}
 
Example 13
Project: OpenVertretung   File: ConnectionRegressionTest.java   View Source Code Vote up 4 votes
/**
 * Tests if the driver configures character sets correctly for 4.1.x
 * servers. Requires that the 'admin connection' is configured, as this test
 * needs to create/drop databases.
 * 
 * @throws Exception
 *             if an error occurs
 */
public void testCollation41() throws Exception {
    if (versionMeetsMinimum(4, 1) && isAdminConnectionConfigured()) {
        Map<String, String> charsetsAndCollations = getCharacterSetsAndCollations();
        charsetsAndCollations.remove("latin7"); // Maps to multiple Java
        // charsets
        charsetsAndCollations.remove("ucs2"); // can't be used as a
        // connection charset

        for (String charsetName : charsetsAndCollations.keySet()) {
            Connection charsetConn = null;
            Statement charsetStmt = null;

            try {
                //String collationName = charsetsAndCollations.get(charsetName);

                Properties props = new Properties();
                props.put("characterEncoding", charsetName);

                System.out.println("Testing character set " + charsetName);

                charsetConn = getAdminConnectionWithProps(props);

                charsetStmt = charsetConn.createStatement();

                charsetStmt.executeUpdate("DROP DATABASE IF EXISTS testCollation41");
                charsetStmt.executeUpdate("DROP TABLE IF EXISTS testCollation41");

                charsetStmt.executeUpdate("CREATE DATABASE testCollation41 DEFAULT CHARACTER SET " + charsetName);
                charsetConn.setCatalog("testCollation41");

                // We've switched catalogs, so we need to recreate the
                // statement to pick this up...
                charsetStmt = charsetConn.createStatement();

                StringBuilder createTableCommand = new StringBuilder("CREATE TABLE testCollation41(field1 VARCHAR(255), field2 INT)");

                charsetStmt.executeUpdate(createTableCommand.toString());

                charsetStmt.executeUpdate("INSERT INTO testCollation41 VALUES ('abc', 0)");

                int updateCount = charsetStmt.executeUpdate("UPDATE testCollation41 SET field2=1 WHERE field1='abc'");
                assertTrue(updateCount == 1);
            } finally {
                if (charsetStmt != null) {
                    charsetStmt.executeUpdate("DROP TABLE IF EXISTS testCollation41");
                    charsetStmt.executeUpdate("DROP DATABASE IF EXISTS testCollation41");
                    charsetStmt.close();
                }

                if (charsetConn != null) {
                    charsetConn.close();
                }
            }
        }
    }
}
 
Example 14
Project: OpenVertretung   File: ConnectionRegressionTest.java   View Source Code Vote up 4 votes
/**
 * Tests fix for BUG#15570 - ReplicationConnection incorrectly copies state,
 * doesn't transfer connection context correctly when transitioning between
 * the same read-only states.
 * 
 * (note, this test will fail if the test user doesn't have permission to
 * "USE 'mysql'".
 * 
 * @throws Exception
 *             if the test fails.
 */
public void testBug15570() throws Exception {
    Connection replConn = null;

    try {
        replConn = getMasterSlaveReplicationConnection();

        int masterConnectionId = Integer.parseInt(getSingleIndexedValueWithQuery(replConn, 1, "SELECT CONNECTION_ID()").toString());

        replConn.setReadOnly(false);

        assertEquals(masterConnectionId, Integer.parseInt(getSingleIndexedValueWithQuery(replConn, 1, "SELECT CONNECTION_ID()").toString()));

        String currentCatalog = replConn.getCatalog();

        replConn.setCatalog(currentCatalog);
        assertEquals(currentCatalog, replConn.getCatalog());

        replConn.setReadOnly(true);

        int slaveConnectionId = Integer.parseInt(getSingleIndexedValueWithQuery(replConn, 1, "SELECT CONNECTION_ID()").toString());

        // The following test is okay for now, as the chance of MySQL wrapping the connection id counter during our testsuite is very small.
        // As per Bug#21286268 fix a Replication connection first initializes the Slaves sub-connection, then the Masters.
        assertTrue("Master id " + masterConnectionId + " is not newer than slave id " + slaveConnectionId, masterConnectionId > slaveConnectionId);

        assertEquals(currentCatalog, replConn.getCatalog());

        String newCatalog = "mysql";

        replConn.setCatalog(newCatalog);
        assertEquals(newCatalog, replConn.getCatalog());

        replConn.setReadOnly(true);
        assertEquals(newCatalog, replConn.getCatalog());

        replConn.setReadOnly(false);
        assertEquals(masterConnectionId, Integer.parseInt(getSingleIndexedValueWithQuery(replConn, 1, "SELECT CONNECTION_ID()").toString()));
    } finally {
        if (replConn != null) {
            replConn.close();
        }
    }
}
 
Example 15
Project: OpenVertretung   File: StatementRegressionTest.java   View Source Code Vote up 4 votes
/**
 * Tests fix for Bug#66430 - setCatalog on connection leaves ServerPreparedStatement cache for old catalog.
 */
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("cachePrepStmts", Boolean.toString(cachePS));
        props.setProperty("useServerPrepStmts", Boolean.toString(useSPS));

        Connection testConn = getConnectionWithProps(props);

        testConn.setCatalog("testBug66430DB1");
        PreparedStatement testPStmt = testConn.prepareStatement("SELECT * FROM testBug66430 WHERE id > ?");
        testPStmt.setInt(1, 0);
        this.rs = testPStmt.executeQuery();
        assertTrue(testCase, this.rs.next());
        assertEquals(testCase, 1, this.rs.getInt(1));
        assertFalse(testCase, this.rs.next());
        testPStmt.close();

        testConn.setCatalog("testBug66430DB2");
        testPStmt = testConn.prepareStatement("SELECT * FROM testBug66430 WHERE id > ?");
        testPStmt.setInt(1, 0);
        this.rs = testPStmt.executeQuery();
        assertTrue(testCase, this.rs.next());
        assertEquals(testCase, 2, this.rs.getInt(1));
        assertFalse(testCase, this.rs.next());
        testPStmt.close();

        // Do it again to make sure cached prepared statements behave correctly.
        testConn.setCatalog("testBug66430DB1");
        testPStmt = testConn.prepareStatement("SELECT * FROM testBug66430 WHERE id > ?");
        testPStmt.setInt(1, 0);
        this.rs = testPStmt.executeQuery();
        assertTrue(testCase, this.rs.next());
        assertEquals(testCase, 1, this.rs.getInt(1));
        assertFalse(testCase, this.rs.next());
        testPStmt.close();

        testConn.setCatalog("testBug66430DB2");
        testPStmt = testConn.prepareStatement("SELECT * FROM testBug66430 WHERE id > ?");
        testPStmt.setInt(1, 0);
        this.rs = testPStmt.executeQuery();
        assertTrue(testCase, this.rs.next());
        assertEquals(testCase, 2, this.rs.getInt(1));
        assertFalse(testCase, this.rs.next());
        testPStmt.close();

        testConn.close();
    } while ((useSPS = !useSPS) || (cachePS = !cachePS));
}
 
Example 16
Project: BibliotecaPS   File: ConnectionRegressionTest.java   View Source Code Vote up 4 votes
/**
 * Tests fix for BUG#15570 - ReplicationConnection incorrectly copies state,
 * doesn't transfer connection context correctly when transitioning between
 * the same read-only states.
 * 
 * (note, this test will fail if the test user doesn't have permission to
 * "USE 'mysql'".
 * 
 * @throws Exception
 *             if the test fails.
 */
public void testBug15570() throws Exception {
    Connection replConn = null;

    try {
        replConn = getMasterSlaveReplicationConnection();

        int masterConnectionId = Integer.parseInt(getSingleIndexedValueWithQuery(replConn, 1, "SELECT CONNECTION_ID()").toString());

        replConn.setReadOnly(false);

        assertEquals(masterConnectionId, Integer.parseInt(getSingleIndexedValueWithQuery(replConn, 1, "SELECT CONNECTION_ID()").toString()));

        String currentCatalog = replConn.getCatalog();

        replConn.setCatalog(currentCatalog);
        assertEquals(currentCatalog, replConn.getCatalog());

        replConn.setReadOnly(true);

        int slaveConnectionId = Integer.parseInt(getSingleIndexedValueWithQuery(replConn, 1, "SELECT CONNECTION_ID()").toString());

        // The following test is okay for now, as the chance of MySQL wrapping the connection id counter during our testsuite is very small.
        // As per Bug#21286268 fix a Replication connection first initializes the Slaves sub-connection, then the Masters.
        assertTrue("Master id " + masterConnectionId + " is not newer than slave id " + slaveConnectionId, masterConnectionId > slaveConnectionId);

        assertEquals(currentCatalog, replConn.getCatalog());

        String newCatalog = "mysql";

        replConn.setCatalog(newCatalog);
        assertEquals(newCatalog, replConn.getCatalog());

        replConn.setReadOnly(true);
        assertEquals(newCatalog, replConn.getCatalog());

        replConn.setReadOnly(false);
        assertEquals(masterConnectionId, Integer.parseInt(getSingleIndexedValueWithQuery(replConn, 1, "SELECT CONNECTION_ID()").toString()));
    } finally {
        if (replConn != null) {
            replConn.close();
        }
    }
}
 
Example 17
Project: ProyectoPacientes   File: MetaDataRegressionTest.java   View Source Code Vote up 4 votes
/**
 * Tests fix for BUG#61150 - First call to SP
 * fails with "No Database Selected"
 * The workaround introduced in DatabaseMetaData.getCallStmtParameterTypes
 * to fix the bug in server where SHOW CREATE PROCEDURE was not respecting
 * lower-case table names is misbehaving when connection is not attached to
 * database and on non-casesensitive OS.
 * 
 * @throws Exception
 *             if the test fails.
 */
public void testBug61150() throws Exception {
    NonRegisteringDriver driver = new NonRegisteringDriver();
    Properties oldProps = driver.parseURL(BaseTestCase.dbUrl, null);

    String host = driver.host(oldProps);
    int port = driver.port(oldProps);
    StringBuilder newUrlToTestNoDB = new StringBuilder("jdbc:mysql://");
    if (host != null) {
        newUrlToTestNoDB.append(host);
    }
    newUrlToTestNoDB.append(":").append(port).append("/");

    Statement savedSt = this.stmt;

    Properties props = getHostFreePropertiesFromTestsuiteUrl();
    props.remove(NonRegisteringDriver.DBNAME_PROPERTY_KEY);
    Connection conn1 = DriverManager.getConnection(newUrlToTestNoDB.toString(), props);

    this.stmt = conn1.createStatement();
    createDatabase("TST1");
    createProcedure("TST1.PROC", "(x int, out y int)\nbegin\ndeclare z int;\nset z = x+1, y = z;\nend\n");

    CallableStatement cStmt = null;
    cStmt = conn1.prepareCall("{call `TST1`.`PROC`(?, ?)}");
    cStmt.setInt(1, 5);
    cStmt.registerOutParameter(2, Types.INTEGER);

    cStmt.execute();
    assertEquals(6, cStmt.getInt(2));
    cStmt.clearParameters();
    cStmt.close();

    conn1.setCatalog("TST1");
    cStmt = null;
    cStmt = conn1.prepareCall("{call TST1.PROC(?, ?)}");
    cStmt.setInt(1, 5);
    cStmt.registerOutParameter(2, Types.INTEGER);

    cStmt.execute();
    assertEquals(6, cStmt.getInt(2));
    cStmt.clearParameters();
    cStmt.close();

    conn1.setCatalog("mysql");
    cStmt = null;
    cStmt = conn1.prepareCall("{call `TST1`.`PROC`(?, ?)}");
    cStmt.setInt(1, 5);
    cStmt.registerOutParameter(2, Types.INTEGER);

    cStmt.execute();
    assertEquals(6, cStmt.getInt(2));
    cStmt.clearParameters();
    cStmt.close();

    this.stmt = savedSt;
}
 
Example 18
Project: ProyectoPacientes   File: ConnectionRegressionTest.java   View Source Code Vote up 4 votes
/**
 * Tests if the driver configures character sets correctly for 4.1.x
 * servers. Requires that the 'admin connection' is configured, as this test
 * needs to create/drop databases.
 * 
 * @throws Exception
 *             if an error occurs
 */
public void testCollation41() throws Exception {
    if (versionMeetsMinimum(4, 1) && isAdminConnectionConfigured()) {
        Map<String, String> charsetsAndCollations = getCharacterSetsAndCollations();
        charsetsAndCollations.remove("latin7"); // Maps to multiple Java
        // charsets
        charsetsAndCollations.remove("ucs2"); // can't be used as a
        // connection charset

        for (String charsetName : charsetsAndCollations.keySet()) {
            Connection charsetConn = null;
            Statement charsetStmt = null;

            try {
                //String collationName = charsetsAndCollations.get(charsetName);

                Properties props = new Properties();
                props.put("characterEncoding", charsetName);

                System.out.println("Testing character set " + charsetName);

                charsetConn = getAdminConnectionWithProps(props);

                charsetStmt = charsetConn.createStatement();

                charsetStmt.executeUpdate("DROP DATABASE IF EXISTS testCollation41");
                charsetStmt.executeUpdate("DROP TABLE IF EXISTS testCollation41");

                charsetStmt.executeUpdate("CREATE DATABASE testCollation41 DEFAULT CHARACTER SET " + charsetName);
                charsetConn.setCatalog("testCollation41");

                // We've switched catalogs, so we need to recreate the
                // statement to pick this up...
                charsetStmt = charsetConn.createStatement();

                StringBuilder createTableCommand = new StringBuilder("CREATE TABLE testCollation41(field1 VARCHAR(255), field2 INT)");

                charsetStmt.executeUpdate(createTableCommand.toString());

                charsetStmt.executeUpdate("INSERT INTO testCollation41 VALUES ('abc', 0)");

                int updateCount = charsetStmt.executeUpdate("UPDATE testCollation41 SET field2=1 WHERE field1='abc'");
                assertTrue(updateCount == 1);
            } finally {
                if (charsetStmt != null) {
                    charsetStmt.executeUpdate("DROP TABLE IF EXISTS testCollation41");
                    charsetStmt.executeUpdate("DROP DATABASE IF EXISTS testCollation41");
                    charsetStmt.close();
                }

                if (charsetConn != null) {
                    charsetConn.close();
                }
            }
        }
    }
}
 
Example 19
Project: BibliotecaPS   File: ConnectionRegressionTest.java   View Source Code Vote up 4 votes
/**
 * Tests if the driver configures character sets correctly for 4.1.x
 * servers. Requires that the 'admin connection' is configured, as this test
 * needs to create/drop databases.
 * 
 * @throws Exception
 *             if an error occurs
 */
public void testCollation41() throws Exception {
    if (versionMeetsMinimum(4, 1) && isAdminConnectionConfigured()) {
        Map<String, String> charsetsAndCollations = getCharacterSetsAndCollations();
        charsetsAndCollations.remove("latin7"); // Maps to multiple Java
        // charsets
        charsetsAndCollations.remove("ucs2"); // can't be used as a
        // connection charset

        for (String charsetName : charsetsAndCollations.keySet()) {
            Connection charsetConn = null;
            Statement charsetStmt = null;

            try {
                //String collationName = charsetsAndCollations.get(charsetName);

                Properties props = new Properties();
                props.put("characterEncoding", charsetName);

                System.out.println("Testing character set " + charsetName);

                charsetConn = getAdminConnectionWithProps(props);

                charsetStmt = charsetConn.createStatement();

                charsetStmt.executeUpdate("DROP DATABASE IF EXISTS testCollation41");
                charsetStmt.executeUpdate("DROP TABLE IF EXISTS testCollation41");

                charsetStmt.executeUpdate("CREATE DATABASE testCollation41 DEFAULT CHARACTER SET " + charsetName);
                charsetConn.setCatalog("testCollation41");

                // We've switched catalogs, so we need to recreate the
                // statement to pick this up...
                charsetStmt = charsetConn.createStatement();

                StringBuilder createTableCommand = new StringBuilder("CREATE TABLE testCollation41(field1 VARCHAR(255), field2 INT)");

                charsetStmt.executeUpdate(createTableCommand.toString());

                charsetStmt.executeUpdate("INSERT INTO testCollation41 VALUES ('abc', 0)");

                int updateCount = charsetStmt.executeUpdate("UPDATE testCollation41 SET field2=1 WHERE field1='abc'");
                assertTrue(updateCount == 1);
            } finally {
                if (charsetStmt != null) {
                    charsetStmt.executeUpdate("DROP TABLE IF EXISTS testCollation41");
                    charsetStmt.executeUpdate("DROP DATABASE IF EXISTS testCollation41");
                    charsetStmt.close();
                }

                if (charsetConn != null) {
                    charsetConn.close();
                }
            }
        }
    }
}
 
Example 20
Project: ProyectoPacientes   File: StatementRegressionTest.java   View Source Code Vote up 4 votes
/**
 * Tests fix for Bug#66430 - setCatalog on connection leaves ServerPreparedStatement cache for old catalog.
 */
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("cachePrepStmts", Boolean.toString(cachePS));
        props.setProperty("useServerPrepStmts", Boolean.toString(useSPS));

        Connection testConn = getConnectionWithProps(props);

        testConn.setCatalog("testBug66430DB1");
        PreparedStatement testPStmt = testConn.prepareStatement("SELECT * FROM testBug66430 WHERE id > ?");
        testPStmt.setInt(1, 0);
        this.rs = testPStmt.executeQuery();
        assertTrue(testCase, this.rs.next());
        assertEquals(testCase, 1, this.rs.getInt(1));
        assertFalse(testCase, this.rs.next());
        testPStmt.close();

        testConn.setCatalog("testBug66430DB2");
        testPStmt = testConn.prepareStatement("SELECT * FROM testBug66430 WHERE id > ?");
        testPStmt.setInt(1, 0);
        this.rs = testPStmt.executeQuery();
        assertTrue(testCase, this.rs.next());
        assertEquals(testCase, 2, this.rs.getInt(1));
        assertFalse(testCase, this.rs.next());
        testPStmt.close();

        // Do it again to make sure cached prepared statements behave correctly.
        testConn.setCatalog("testBug66430DB1");
        testPStmt = testConn.prepareStatement("SELECT * FROM testBug66430 WHERE id > ?");
        testPStmt.setInt(1, 0);
        this.rs = testPStmt.executeQuery();
        assertTrue(testCase, this.rs.next());
        assertEquals(testCase, 1, this.rs.getInt(1));
        assertFalse(testCase, this.rs.next());
        testPStmt.close();

        testConn.setCatalog("testBug66430DB2");
        testPStmt = testConn.prepareStatement("SELECT * FROM testBug66430 WHERE id > ?");
        testPStmt.setInt(1, 0);
        this.rs = testPStmt.executeQuery();
        assertTrue(testCase, this.rs.next());
        assertEquals(testCase, 2, this.rs.getInt(1));
        assertFalse(testCase, this.rs.next());
        testPStmt.close();

        testConn.close();
    } while ((useSPS = !useSPS) || (cachePS = !cachePS));
}