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

The following are Jave code examples for showing how to use hashCode() 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: the-vigilantes   File: ConnectionRegressionTest.java   View Source Code Vote up 4 votes
public void testBug63284() throws Exception {
    Properties props = new Driver().parseURL(BaseTestCase.dbUrl, null);
    props.setProperty("autoReconnect", "true");
    props.setProperty("socketFactory", "testsuite.UnreliableSocketFactory");

    Properties urlProps = new NonRegisteringDriver().parseURL(BaseTestCase.dbUrl, null);

    String host = urlProps.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY);
    String port = urlProps.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY);

    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.NUM_HOSTS_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY + ".1");
    props.remove(NonRegisteringDriver.PORT_PROPERTY_KEY + ".1");

    props.setProperty("queriesBeforeRetryMaster", "50");
    props.setProperty("maxReconnects", "1");

    UnreliableSocketFactory.mapHost("master", host);
    UnreliableSocketFactory.mapHost("slave", host);

    Connection failoverConnection1 = null;
    Connection failoverConnection2 = null;

    try {
        failoverConnection1 = getConnectionWithProps("jdbc:mysql://master:" + port + ",slave:" + port + "/", props);

        failoverConnection2 = getConnectionWithProps("jdbc:mysql://master:" + port + ",slave:" + port + "/", props);

        assertTrue(((com.mysql.jdbc.Connection) failoverConnection1).isMasterConnection());

        // Two different Connection objects should not equal each other:
        assertFalse(failoverConnection1.equals(failoverConnection2));

        int hc = failoverConnection1.hashCode();

        UnreliableSocketFactory.downHost("master");

        for (int i = 0; i < 3; i++) {
            try {
                failoverConnection1.createStatement().execute("SELECT 1");
            } catch (SQLException e) {
                // do nothing, expect SQLException when failing over initially goal here is to ensure valid connection against a slave
            }
        }
        // ensure we're now connected to the slave
        assertFalse(((com.mysql.jdbc.Connection) failoverConnection1).isMasterConnection());

        // ensure that hashCode() result is persistent across failover events when proxy state changes
        assertEquals(hc, failoverConnection1.hashCode());
    } finally {
        if (failoverConnection1 != null) {
            failoverConnection1.close();
        }
        if (failoverConnection2 != null) {
            failoverConnection2.close();
        }
    }
}
 
Example 2
Project: OpenVertretung   File: ConnectionRegressionTest.java   View Source Code Vote up 4 votes
public void testBug63284() throws Exception {
    Properties props = new Driver().parseURL(BaseTestCase.dbUrl, null);
    props.setProperty("autoReconnect", "true");
    props.setProperty("socketFactory", "testsuite.UnreliableSocketFactory");

    Properties urlProps = new NonRegisteringDriver().parseURL(BaseTestCase.dbUrl, null);

    String host = urlProps.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY);
    String port = urlProps.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY);

    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.NUM_HOSTS_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY + ".1");
    props.remove(NonRegisteringDriver.PORT_PROPERTY_KEY + ".1");

    props.setProperty("queriesBeforeRetryMaster", "50");
    props.setProperty("maxReconnects", "1");

    UnreliableSocketFactory.mapHost("master", host);
    UnreliableSocketFactory.mapHost("slave", host);

    Connection failoverConnection1 = null;
    Connection failoverConnection2 = null;

    try {
        failoverConnection1 = getConnectionWithProps("jdbc:mysql://master:" + port + ",slave:" + port + "/", props);

        failoverConnection2 = getConnectionWithProps("jdbc:mysql://master:" + port + ",slave:" + port + "/", props);

        assertTrue(((com.mysql.jdbc.Connection) failoverConnection1).isMasterConnection());

        // Two different Connection objects should not equal each other:
        assertFalse(failoverConnection1.equals(failoverConnection2));

        int hc = failoverConnection1.hashCode();

        UnreliableSocketFactory.downHost("master");

        for (int i = 0; i < 3; i++) {
            try {
                failoverConnection1.createStatement().execute("SELECT 1");
            } catch (SQLException e) {
                // do nothing, expect SQLException when failing over initially goal here is to ensure valid connection against a slave
            }
        }
        // ensure we're now connected to the slave
        assertFalse(((com.mysql.jdbc.Connection) failoverConnection1).isMasterConnection());

        // ensure that hashCode() result is persistent across failover events when proxy state changes
        assertEquals(hc, failoverConnection1.hashCode());
    } finally {
        if (failoverConnection1 != null) {
            failoverConnection1.close();
        }
        if (failoverConnection2 != null) {
            failoverConnection2.close();
        }
    }
}
 
Example 3
Project: ProyectoPacientes   File: ConnectionRegressionTest.java   View Source Code Vote up 4 votes
public void testBug63284() throws Exception {
    Properties props = new Driver().parseURL(BaseTestCase.dbUrl, null);
    props.setProperty("autoReconnect", "true");
    props.setProperty("socketFactory", "testsuite.UnreliableSocketFactory");

    Properties urlProps = new NonRegisteringDriver().parseURL(BaseTestCase.dbUrl, null);

    String host = urlProps.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY);
    String port = urlProps.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY);

    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.NUM_HOSTS_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY + ".1");
    props.remove(NonRegisteringDriver.PORT_PROPERTY_KEY + ".1");

    props.setProperty("queriesBeforeRetryMaster", "50");
    props.setProperty("maxReconnects", "1");

    UnreliableSocketFactory.mapHost("master", host);
    UnreliableSocketFactory.mapHost("slave", host);

    Connection failoverConnection1 = null;
    Connection failoverConnection2 = null;

    try {
        failoverConnection1 = getConnectionWithProps("jdbc:mysql://master:" + port + ",slave:" + port + "/", props);

        failoverConnection2 = getConnectionWithProps("jdbc:mysql://master:" + port + ",slave:" + port + "/", props);

        assertTrue(((com.mysql.jdbc.Connection) failoverConnection1).isMasterConnection());

        // Two different Connection objects should not equal each other:
        assertFalse(failoverConnection1.equals(failoverConnection2));

        int hc = failoverConnection1.hashCode();

        UnreliableSocketFactory.downHost("master");

        for (int i = 0; i < 3; i++) {
            try {
                failoverConnection1.createStatement().execute("SELECT 1");
            } catch (SQLException e) {
                // do nothing, expect SQLException when failing over initially goal here is to ensure valid connection against a slave
            }
        }
        // ensure we're now connected to the slave
        assertFalse(((com.mysql.jdbc.Connection) failoverConnection1).isMasterConnection());

        // ensure that hashCode() result is persistent across failover events when proxy state changes
        assertEquals(hc, failoverConnection1.hashCode());
    } finally {
        if (failoverConnection1 != null) {
            failoverConnection1.close();
        }
        if (failoverConnection2 != null) {
            failoverConnection2.close();
        }
    }
}
 
Example 4
Project: BibliotecaPS   File: ConnectionRegressionTest.java   View Source Code Vote up 4 votes
public void testBug63284() throws Exception {
    Properties props = new Driver().parseURL(BaseTestCase.dbUrl, null);
    props.setProperty("autoReconnect", "true");
    props.setProperty("socketFactory", "testsuite.UnreliableSocketFactory");

    Properties urlProps = new NonRegisteringDriver().parseURL(BaseTestCase.dbUrl, null);

    String host = urlProps.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY);
    String port = urlProps.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY);

    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.NUM_HOSTS_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY + ".1");
    props.remove(NonRegisteringDriver.PORT_PROPERTY_KEY + ".1");

    props.setProperty("queriesBeforeRetryMaster", "50");
    props.setProperty("maxReconnects", "1");

    UnreliableSocketFactory.mapHost("master", host);
    UnreliableSocketFactory.mapHost("slave", host);

    Connection failoverConnection1 = null;
    Connection failoverConnection2 = null;

    try {
        failoverConnection1 = getConnectionWithProps("jdbc:mysql://master:" + port + ",slave:" + port + "/", props);

        failoverConnection2 = getConnectionWithProps("jdbc:mysql://master:" + port + ",slave:" + port + "/", props);

        assertTrue(((com.mysql.jdbc.Connection) failoverConnection1).isMasterConnection());

        // Two different Connection objects should not equal each other:
        assertFalse(failoverConnection1.equals(failoverConnection2));

        int hc = failoverConnection1.hashCode();

        UnreliableSocketFactory.downHost("master");

        for (int i = 0; i < 3; i++) {
            try {
                failoverConnection1.createStatement().execute("SELECT 1");
            } catch (SQLException e) {
                // do nothing, expect SQLException when failing over initially goal here is to ensure valid connection against a slave
            }
        }
        // ensure we're now connected to the slave
        assertFalse(((com.mysql.jdbc.Connection) failoverConnection1).isMasterConnection());

        // ensure that hashCode() result is persistent across failover events when proxy state changes
        assertEquals(hc, failoverConnection1.hashCode());
    } finally {
        if (failoverConnection1 != null) {
            failoverConnection1.close();
        }
        if (failoverConnection2 != null) {
            failoverConnection2.close();
        }
    }
}